Mastodon

Deploying NuGet package using GitHub Actions (without any marketplace actions)

Oct 06, 2020 by Kolappan N

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 management 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 single line command that I don’t want to use a another action for it.

Steps

  1. Go the API Keys page in Nuget website.
  2. 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 at least once a year.
  3. Now copy this Key.
  4. Go to the Settings page of your GitHub repo and go to the Secrets setting.
  5. Click on New Secret and paste the previously copied key and provide a name like NUGET_KEY and save it.
GitHub Settings - Secrets
GitHub Settings - Secrets
  1. Create a pipeline CD.yml in the .github/workflows folder.
  2. The the workflow,

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: actions/[email protected]
    - name: Setup .NET Core
      uses: actions/[email protected]
      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 ${{ secrets.NUGET_KEY }}