45

I am trying to use GitHub actions to generate a NuGet package from my project and push it to the (private) GitHub registry.

My script ([NAME] fields redacted):

name: Update NuGet

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    name: Update NuGet 
    steps:
      - uses: actions/checkout@master
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '2.2.105'
      - name: Package Release
        run: |  
          cd [SOLUTION_FOLDER]
          dotnet pack -c Release -o out
      - name: Publish Nuget to GitHub registry
        run: dotnet nuget push ./[SOLUTION_FOLDER]/[PROJECT_FOLDER]/out/$(ls ./[SOLUTION_FOLDER]/[PROJECT_FOLDER]/out) -s https://nuget.pkg.github.com/[USERNAME]/index.json -k ${GITHUB_TOKEN}  
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 

Log output:

info : Pushing [PROJECT_FOLDER].3.4.23.nupkg to 'https://nuget.pkg.github.com/[USERNAME]'...
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
info : An error was encountered when fetching 'PUT https://nuget.pkg.github.com/[USERNAME]/'. The request will now be retried.
info : An error occurred while sending the request.
info :   The server returned an invalid or unrecognized response.
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
info : An error was encountered when fetching 'PUT https://nuget.pkg.github.com/[USERNAME]/'. The request will now be retried.
info : An error occurred while sending the request.
info :   The server returned an invalid or unrecognized response.
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
error: An error occurred while sending the request.
error:   The server returned an invalid or unrecognized response.
##[error]Process completed with exit code 1.

This is the coresponding GitHub issue (with a workaround option): https://github.com/NuGet/Home/issues/8580

5
  • Please add the description on how did you fixed error: Please specify the path to the package. with dotnet nuget push command.
    – Konard
    Sep 12, 2019 at 18:00
  • @Konard That was my bad. I did not get the file name in the env var. I solved it by adding the command directly dotnet nuget push /path/$(ls ..)
    – jwillmer
    Sep 12, 2019 at 19:29
  • Did you manage to get this working? I'm trying the same thing but struggling. Sep 13, 2019 at 12:37
  • @JustusBurger I'm in contact with GitHub support. If they can help me fix it I will post an answer.
    – jwillmer
    Sep 13, 2019 at 15:37
  • why not use Publish NuGet Action?
    – Alexan
    Jan 27, 2020 at 20:28

7 Answers 7

40

Second Update: I got an answer in the GitHub issue from jcansdale that says (haven't tested this):

Support for the dotnet nuget push --api-key option has now been added to GitHub Packages. For some reason this works consistently, but using basic auth (password in nuget.config file) fails randomly!

Example:

  - name: Publish Nuget to GitHub registry
    run: dotnet nuget push ./<project>/out/*.nupkg -k ${GITHUB_TOKEN} -s https://nuget.pkg.github.com/<organization>/index.json --skip-duplicate --no-symbols 
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Update: Based on Dids answer on GitHub my configuration works now like this:

name: NuGet Generation

on:
  push:
    branches:
      - master
  pull_request:
    types: [closed]
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-18.04
    name: Update NuGet package
    steps:

      - name: Checkout repository
        uses: actions/checkout@v1

      - name: Setup .NET Core @ Latest
        uses: actions/setup-dotnet@v1
        with:
          source-url: https://nuget.pkg.github.com/<organization>/index.json
        env:
          NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}        
          
      - name: Build solution and generate NuGet package
        run: |  
          cd <project>
          dotnet pack -c Release -o out  

      - name: Push generated package to GitHub registry
        run: dotnet nuget push ./<project>/out/*.nupkg --skip-duplicate --no-symbols true

Note: At the time of writing I needed to use --no-symbols true instead of --no-symbols to prevent exceptions in the dotnet NuGet client.


Old answer:

I switched to the Windows image and got it to work based on the example of @anangaur. This is my final code:

name: NuGet Generation

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: windows-latest
    name: Update NuGet 
    steps:

      - name: Checkout repository
        uses: actions/checkout@master

#  latest image has .NET already installed!
#      - name: Setup .NET environment
#        uses: actions/setup-dotnet@v1
#        with:
#          dotnet-version: '2.2.105' 
          
      - name: Build solution and generate NuGet package
        run: |  
          cd SOLUTION_FOLDER
          dotnet pack -c Release -o out  

      - name: Install NuGet client
        uses: warrenbuckley/Setup-Nuget@v1
        
      - name: Add private GitHub registry to NuGet
        run: nuget sources add -name "GPR" -Source https://nuget.pkg.github.com/ORGANIZATION_NAME/index.json -Username ORGANIZATION_NAME -Password ${{ secrets.GITHUB_TOKEN }}
        
      - name: Push generated package to GitHub registry
        run: nuget push .\SOLUTION_FOLDER\PROJECT_FOLDER\out\*.nupkg -Source "GPR" -SkipDuplicate

6
  • 2
    Make sure to set the RepositoryUrl value in your csproj file
    – ctyar
    May 19, 2020 at 9:47
  • I had other comments but deleted them. I am having this problem warn : No destination repository detected. Ensure the source project has a 'RepositoryUrl' property defined. If you're using a nuspec file, ensure that it has a repository element with the required 'type' and 'url' attributes. BadRequest nuget.pkg.github.com/myOrganization 25ms error: Response status code does not indicate success: 400 (Bad Request). I removed my organizations actual name from the url Jun 11, 2020 at 14:21
  • If I use the GitHub Actions source-url, will that stop me also pushing to NuGet.org? Secondly, I believe the --no-symbols flag is now fixed. Jul 10, 2020 at 11:22
  • 1
    Anyone know how to indicate the RepositoryUrl on a .Net Framework project? They did add that element until .Net Standard or Core. Nov 17, 2020 at 22:38
  • --no-symbols true will cause error: File does not exist (true). passing values for --no-symbols is no longer supported. so one should remove the true. Mar 27, 2022 at 10:30
6

Here is a workaround that works on all platforms:

name: prerelease NuGet

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    # also works with windows-latest and macos-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v1
    - name: Build with dotnet
      run: dotnet build --configuration Release --version-suffix prerelease-$(date +%Y%m%d%H%M%S)
      shell: bash
    - name: Publish nuget
      run: |
           for f in ./[repository]/bin/Release/*.nupkg
           do
             curl -vX PUT -u "[user]:${{ secrets.GHPackagesToken }}" -F package=@$f https://nuget.pkg.github.com/[user]/
           done
      shell: bash

Notes:

  • this creates a datestamped prerelease build for every git push and uploads it to nuget
    • for the suffix to work, you need to set <VersionPrefix> instead of <Version> in your .csproj
    • if you don't want the prerelease suffix, remove the --version-suffix parameter
  • the shell is explicitly set as bash in order to allow compatibility with building on windows
  • you will need to replace [user] and [repository] above with your own specific values
    • you will need to create a personal access token with the permissions of write:packages
    • then create a GitHub Secret named GHPackagesToken and put the token created above in there
    • using GitHub Secrets eliminates the need for a separate file containing your token
  • this assumes you're have <GeneratePackageOnBuild>true</GeneratePackageOnBuild> in your .csproj
    • if you don't, then you will need an additional step running dotnet pack
  • make sure to specify <RepositoryUrl>...</RepositoryUrl> in your .csproj
  • for a working example if you can't get the above code working, see https://github.com/vslee/IEXSharp/blob/master/.github/workflows/dotnetcore.yml, which pushes to https://github.com/vslee/IEXSharp/packages (ignore all of my extraneous comments there)
    • I posted this bc I tried both the examples from jwillmer above, as well as @anangaur and @marza91 on the GH issue thread but neither worked for me (on any platform)
  • once GitHub fixes the issue of not being able to use the API key directly in the dotnet nuget push command (see initial post of GH issue), then we won't need this workaround anymore
8
  • I got a strange error, any idea? curl: (26) Failed to open/read local data from file/application ##[error]Process completed with exit code 26.
    – Arda
    Nov 20, 2019 at 6:54
  • Without seeing your repo, it's hard to say. But sounds like curl might be having difficulty reading the file (the nupkg)?
    – vlee
    Nov 20, 2019 at 6:58
  • Uppps, sorry to miss yml; here it is... github.com/ardacetinkaya/Blazor.Console/blob/master/.github/… By the way, I changed platform from windows to ubuntu and that error is gone, but new one appeared curl: (26) read function returned funny value
    – Arda
    Nov 20, 2019 at 7:30
  • 1
    Okay, you're not actually creating a nupkg. So you can either uncomment your Pack step, or you can add <GeneratePackageOnBuild>true</GeneratePackageOnBuild> to your .csproj
    – vlee
    Nov 20, 2019 at 7:59
  • Shame on me :) I had commented to much from my previous action. Thanks... Now everything is fine.
    – Arda
    Nov 20, 2019 at 8:08
6

You can use the dotnet nuget add source command:

    - name: NuGet push
      run: |
        dotnet nuget add source https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json --name github --username ${{ github.repository_owner }} --password ${{ github.token }} --store-password-in-clear-text
        dotnet nuget push **/*.nupkg --source github

The --store-password-in-clear-text option was required for me when running in a linux environment.

With this method, there's no need to modify the actions/setup-dotnet task. Also, this method would allow you to push to multiple NuGet streams if needed.

1
  • 1
    Perfect, thanks, exactly what I've needed. Also could be combined with '--skip-duplicate' at the end.
    – Marfu
    Sep 15, 2020 at 14:00
4

Make sure you project file has the following

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <OutputType>Library</OutputType>
    <PackageId>Example.PackageName</PackageId>
    <Version>1.0.0</Version>
    <Authors>Author Engineering</Authors>
    <Company>Company Inc</Company>
    <PackageDescription>This package for ...!</PackageDescription>
    <RepositoryUrl>
https://github.com/YOUR_ACCOUNT/Example.PackageName</RepositoryUrl>
  </PropertyGroup>

This should be your main.yaml for building, packaging, publishing, and versioning:

name: Continuous Integration

on:
  push:
    branches:
      - master

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Setup Dotnet Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.100
        source-url: https://nuget.pkg.github.com/YOUR_ACCOUNT/index.json
      env:
        NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

    - name: Setup Nuget Config
      run: sed 's/GITHUB_TOKEN/${{ secrets.GITHUB_TOKEN }}/g' .nuget.config > nuget.config

    - name: Build
      run: dotnet build --configuration Release

    - name: Version and Tag
      id: bump_version
      uses: mathieudutour/github-tag-action@v1
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}

    - name: Prep Version String
      run: echo ::set-env name=VERSION_NUMBER::$(echo ${{ steps.bump_version.outputs.new_tag }} | sed 's/[v]//g')

    - name: Define Package Name
      run: echo ::set-env name=PACKAGE_NAME::$"Example.PackageName/bin/Release/Example.PackageName.${{ env.VERSION_NUMBER }}.nupkg"

    - name: Set Nuget Package Version
      uses: roryprimrose/set-vs-sdk-project-version@v1
      with:
        version: ${{ env.VERSION_NUMBER }}

    - name: Pack
      run: dotnet pack --configuration Release Example.PackageName

    - name: Publish Package
      run: dotnet nuget push Example.PackageName/bin/Release/*.nupkg --source https://nuget.pkg.github.com/YOUR_ACCOUNT/index.json

    - name: Create Release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ steps.bump_version.outputs.new_tag }}
        release_name: Release ${{ github.ref }}

2
  • Set env var is changed: docs.github.com/en/actions/reference/…. For example: run: echo "VERSION_NUMBER=$(echo ${{ steps.bump_version.outputs.new_tag }} | sed 's/[v]//g')" >> $GITHUB_ENV and run: echo 'PACKAGE_NAME=$"Example.PackageName/bin/Release/Example.PackageName.${{ env.VERSION_NUMBER }}.nupkg" ' >> $GITHUB_ENV
    – roeland
    Mar 20, 2021 at 13:27
  • It's worth noting that <RepositoryUrl> https://github.com/YOUR_ACCOUNT/Example.PackageName</RepositoryUrl> needs to identify the repository rather than the package, as in <RepositoryUrl> https://github.com/YOUR_ACCOUNT/YOUR_REPOSITORY</RepositoryUrl> It's often the case that the repository name and package id will match, but it's not necessary. Mar 16, 2022 at 19:05
3

The other answers were so long, I don't know why. This is what I do:

For NuGet.org:

- name: Push Package to NuGet.org
  run: dotnet nuget push *.nupkg -k ${{ secrets.NUGET_ORG_API_KEY }} -s https://api.nuget.org/v3/index.json

For GitHub.com:

- name: Push Package to GitHub.com
  run: dotnet nuget push *.nupkg -k ${{ secrets.GITHUB_TOKEN }} -s https://nuget.pkg.github.com/USERNAME/index.json
1
  • If you read the initial issue you will see that your solution was not working in the past. Today this might very well be the case.
    – jwillmer
    Jan 10, 2022 at 8:16
1

My working solution:

  • replace 'usernamecompanyname' with a valid value for your repo
  • I kept the build and pack separated to allow for easier debugging if something goes wrong
  • you can set ACTIONS_RUNNER_DEBUG variable in your github secrets to true to allow for more detailed debuging
  • Change dotnet-version to your desired dotnet-sdk version
  • No need to specify GITHUB_TOKEN in your github repo secrests, this token is present by default

build_and_publish_nuget.yml:

name: Build and publish package

# Controls when the action will run. Triggers the workflow on push or pull request 
# events but only for the master branch
on:
  push:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@master

      - name: Setup .NET environment
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '3.1.102'
          source-url: https://nuget.pkg.github.com/usernamecompanyname/index.json
        env:
          NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

      - name: Build project
        run: dotnet build -c Release

      - name: Generate a NuGet package
        run: dotnet pack --no-build -c Release -o .

      - name: Push to GitHub package registry
        run: dotnet nuget push *.nupkg
1
  • Any idea of how to consume this package from another repo (in the same organization)?
    – Pradeep
    Jan 31, 2021 at 12:02
1

GitHub has been having intermittent issues with publish NuGet Packages to GitHub Packages. I reached out to support and they gave me two options.

Option 1: CURL

curl -vX PUT -u "<username>:<TOKEN>" -F package=@PATH-TO-PKG-FILE.nupkg https://nuget.pkg.github.com/<OWNER>/

Option 2: DOTNET GPR TOOL
https://github.com/jcansdale/gpr

dotnet tool install gpr -g
gpr push PATH-TO-PKG-FILE.nupkg -k <TOKEN>

I went with Option 2 in my GitHub Action Workflow:

$file = Get-ChildItem -Path <where I output my nupkg file to> -Recurse -Filter *.nupkg | Select -First 1
gpr push $file.FullName -k ${{secrets.GITHUB_TOKEN}}          
1
  • Or you can use nuget.exe instead of dotnet nuget to push to GPR. That should work consistently.
    – Pieterjan
    Feb 2, 2021 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.