Using GitHub Actions to build, publish and deploy NuGet Packages

In my dayjob i’m all about automating tasks that are dumb and repetitive. Now I was creating a NuGet package for a hobby project and it wanted to automate that to. After some looking into this, I found GitHub Actions.

Github actions

With GitHub Actions you can automate all kinds of stuff starting with you’re code on GitHub. This is from the website of GitHub:

Automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you’d like, including CI/CD, and combine actions in a completely customized workflow.

https://docs.github.com/en/actions

What do we need?

The following parts you need before we can start:

  • Source code that you want to package and publish to NuGet
  • A unique name – You can check this by searching for the name you want to use, here!
  • A GitHub account
  • A NuGet.Org accound

Step 1: GitHub Repository

The first step is creating you’re GitHub repository here! When you have done this, you should commit you’re sourcecode to this repository.

Step 2: Create a new GitHub Action

When you are in you’re git hub repository click on “Actions”(1). Here can you select a predefined workflow or start creating a workflow from scratch. In this example, i’m gonna build a .NET Standard project, so i’ll choose the .NET Predefined workflow. Click on “Set up this workflow”(2)

Now a new yaml file is created. It should look like this:

name: .NET

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
    - name: Test
      run: dotnet test --no-build --verbosity normal

We can breakout the script as following:

name: .NET

Name of the workflow

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

When the workflow should start. In this case when there is a push to the “main” branch and when there is a pull request to the “main” branch.

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
    - name: Test
      run: dotnet test --no-build --verbosity normal

These are the build steps of the workflow.
It starts with setting the dotnet version, restores dependencies, build the software and at lasts runs the unit tests.

Step 3: Updating the yaml workflow file

We start with updating the name to a better description of this workflow:

name: .NET
name: Publish Package

Now we remove the functionality that the workflow starts when a pullrequest is started:

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
on:
  push:
    branches: [ main ]

When you did this add the following code to the end, as a new step:

- name: Publish
      uses: brandedoutcast/publish-nuget@v2.5.2  
      with: 
       PROJECT_FILE_PATH: [[PROJECT_LOCATION]]  
       NUGET_KEY: ${{secrets.NUGET_API_KEY}}
       PACKAGE_NAME: [[PROJECT_NAME]]   

The following properties you should add yourself:

  • “[[PROJECT_LOCATION]]” – You should fill in the location of the project you want to build. For example: “example/example.csproj”
  • “[[PROJECT_NAME]]” – You should fill here the unique name you have chosen for you’re NuGet package.

For the “secrets.NUGET_API_KEY”, we come back in the following steps.

Warning

If you’re solution file is not in the root directory, you should the following to every step:

working-directory: ./solutiondirectory/

Now we should have the following file:

name: Publish Package

on:
  push:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
    - name: Test
      run: dotnet test --no-build --verbosity normal
    - name: Publish
      uses: brandedoutcast/publish-nuget@v2.5.2  
      with: 
       PROJECT_FILE_PATH: [[PROJECT_LOCATION]]  
       NUGET_KEY: ${{secrets.NUGET_API_KEY}}
       PACKAGE_NAME: [[PROJECT_NAME]]   

Now we can click on “Start commit”(1) and then “Commit new file”(2)

When you click again on “Actions” then we can see the workflows is already started. If we click on the build we can see the steps. When we wait, the following steps are completed:

  • Setup .NET
  • Restore dependencies
  • Build
  • Test

And the Publish package has failed because we first need need to create a API key on nuget.org and fill the NUGET_API_KEY in GitHub Secrets.

Step 4: Create NuGet Api Key

We are hosting our package at NuGet.org. So open the webpage and login. Then click your accountname and API Keys.

Now click on create and fill in the “Key Name”(1). I named it “Github Nuget API Key”. Then you should fill a asterix(*) with “Glob Pattern”(2). Then you can create the key with “Create”(3).

Now the API-key is created and you can click on “Copy” to copy the API-Key to the clipboard.

Step 5: Adding NuGet API Key as a GitHub secret

In this step we put the NuGet API Key as a Github secret. We start with opening the repository. Then click on “Settings”(1), “Secrets”(2) and last “New repository secret”(3)

Now we are going to fill the fields. First we fill “NUGET_API_KEY” in the “Name”(1) field. In the “Value”(2) field you fill the NuGet Api Key, generated in the previous step. And click on “Add secret”(3).

Now you created the secret that can be used in the yaml workflow file.

Step 6: Re-run GitHub Action

Now we completely configured the workflow. Now we can re-run the GitHub Action. Yo can do this by opening “Actions” and click on the latest run workflow run result.
In this screen you can click “Re-run jobs”.

If you have a green check mark, then the build and deploy is finished correctly.

Now you can find the NuGet package in you’re NuGet package manager and/or on NuGet.Org.

Versioning

Without changing versions, the next run will fail. Every time you want a new version on NuGet, you have to update the version of you’re project.