Search code examples
nugetgithub-actions

Github workflow push nuget packages


I am new to GitHub actions. I have created a ci.main workflow. Below is my code.

After running I can't view the package in the repository section.

jobs:
  build:
    runs-on: ubuntu-latest  
    steps:
     
    - name: Checkout
      uses: actions/checkout@v2
     
    - name: Build
       run: >
            dotnet pack package \
                   -p:Version=1.0.0 \
                   -c Release \
                   -o ./location \
                   --include-source  
        
    - name: Push
      run: >
           dotnet nuget push \
           ./location/*.nupkg \
           --source "https://nuget.pkg.github.com/orginization/index.json" \
           --api-key xxxxxxx --skip-duplicate

enter image description here


Solution

  • There are a handful of ways to accomplish this:

    1. Automagically

    If you publish with the generated token GitHub will automatically attach the repo to the package. Just ensure you have added write permissions on the job:

    jobs:
      build:
        runs-on: ubuntu-latest
        permissions:
          packages: write
        steps:
    ...
        - name: Push
          run: >
               dotnet nuget push \
               ./location/*.nupkg \
               --source "https://nuget.pkg.github.com/orginization/index.json" \
               --api-key ${{ secrets.GITHUB_TOKEN }} --skip-duplicate
    

    2. Manually

    This is the easiest method, but will have to be done for every package you publish.

    Go to the package page and connect the desired repository repositories: https://github.com/orgs/{org}/packages/nuget/{package}

    Note that this appears to be a one-time operation, you cannot undo this without deleting your package and re-pushing.

    Docs

    3. Via Configuration

    From docs, by virtue of having 3a, 3b is generated with dotnet pack, but you can manually add to the nuspec if need be.

    3a. .csproj

    Add the RepositoryUrl

    <RepositoryUrl>https://github.com/{org}/{repo}</RepositoryUrl>
    

    3b. .nuspec

    Add the repository

    <repository url="https://github.com/{org}/{repo}" />