Deploying NuGet package using GitHub Actions (without any marketplace actions)
GitHub Actions is one of the things I came to love about GitHub. Managing CI/CD within your code repo is a great thing. I started using GitHub Actions for many things from label managemnet to publishing this website. So, I decided to try out publishing a Nuget package using GitHub Actions.
It turned out to be quite easy and dotnet cli has support for publishing Nuget package using the
dotnet nuget push command. It was actually quite surprising that some of the top search results for “Publishing Nuget package using GitHub actions” turned out to use a third party action. The problem with this is that the third party actions might not be updated regularly and this is a sigle line command that I don’t want to use a another action for it.
- Go the API Keys page in Nuget website.
- Create a new API Key by Entering a name, time-period and selecting packages. It is best to create a separate API Key for each package. Also note that the maximum duration of a API Key is 1 year, so you will have to update the key atleast once a year.
- Now copy this Key.
- Go to the Settings page of your GitHub repo and go to the Secrets setting.
- Click on New Secret and paste the previously copied key and provide a name like
NUGET_KEYand save it.
- Create a pipeline
- The the workflow,
- build the Nuget package
- Naviagate to the bin/release directory
- Use the following command
dotnet nuget push "*.nupkg" -s https://api.nuget.org/v3/index.json -k $
Here is my CD.yml file,
name: CD on: # Trigger CD when a release is published release: types: [published] jobs: deploy: runs-on: windows-latest steps: - name: Checkout uses: firstname.lastname@example.org - name: Setup .NET Core uses: email@example.com with: dotnet-version: 3.1.402 - name: Install dependencies run: | cd ./src dotnet restore - name: Build run: | cd ./src dotnet build --configuration Release --no-restore - name: Test run: | cd ./src dotnet test --no-restore --verbosity normal - name: Publish Nuget run: | cd ./src/nk.logger.csv/bin/Release/ dotnet nuget push "*.nupkg" -s https://api.nuget.org/v3/index.json -k $