dotnet-core | devops | github

.NET Core - NuGet Packages CI & CD Using GitHub Actions

Build and run tests on every commit. Automatically upload NuGet packages on every release. All using GitHub actions for your .NET Core Class Libraries.

Abhith RajanApril 02, 2020 · 5 min read · Last Updated:

I always likes to automate the repeatative part of my work. At work, Azure DevOps taking care of the most of the CI & CD. I do maintain a small Class Library project in GitHub which has some helpers and stuffs you know like we reuse on most projects. And is been published to NuGet so that it can be consumed easily.

Recently I added one more project to the same repo which has some common stuff for ASP.NET Core projects. Previously I used to manually upload the .nuget pack file to NuGet since the releases were not that frequent. But now, the number of projects doubled, and sometime we need to deploy both packages since the ASP.NET Core project depends on the other library. So I did automated the whole process using GitHub actions. Here is how I did that,

You can check my repo where you can find the GitHub Actions configuration yml files under the .github/workflows/ directory.

👉 Abhith/Code.Library

I have two actions,

  1. CI
  2. Release to NuGet

CI

The CI (Continuous Integration) action basically runs on every commit to the master branch as well as for every PR to master.

And the jobs are pretty self-explanatory,

  • Setting up dotnet on the Linux
  • Builds the solution file
  • Run tests
1# This is a basic workflow to help you get started with Actions
2
3name: CI
4
5# Controls when the action will run. Triggers the workflow on push or pull request
6# events but only for the master branch
7on:
8 push:
9 branches: [master]
10 pull_request:
11 branches: [master]
12
13# A workflow run is made up of one or more jobs that can run sequentially or in parallel
14jobs:
15 # This workflow contains a single job called "build"
16 build:
17 # The type of runner that the job will run on
18 runs-on: ubuntu-latest
19
20 # Steps represent a sequence of tasks that will be executed as part of the job
21 steps:
22 - uses: actions/checkout@v2
23 - name: Setup .NET Core
24 uses: actions/setup-dotnet@v1
25 with:
26 dotnet-version: 3.1.101
27 - name: Build with dotnet
28 run: dotnet build --configuration Release
29 working-directory: ./src
30 - name: Test
31 run: dotnet test
32 working-directory: ./src

Continuous Integration
Continuous Integration

Release to NuGet

This action runs on every new release. And the jobs include,

  • Setting up dotnet on the Linux
  • Create the .nupkg files
  • Uploads artifacts
  • Upload the Nuget packages
1# This is a basic workflow to help you get started with Actions
2
3name: Release to NuGet
4
5# Controls when the action will run. Triggers the workflow on push or pull request
6# events but only for the master branch
7on:
8 release:
9 types: [published]
10
11# A workflow run is made up of one or more jobs that can run sequentially or in parallel
12jobs:
13 # This workflow contains a single job called "build"
14 build:
15 # The type of runner that the job will run on
16 runs-on: ubuntu-latest
17
18 # Steps represent a sequence of tasks that will be executed as part of the job
19 steps:
20 - uses: actions/checkout@v2
21 - name: Setup .NET Core
22 uses: actions/setup-dotnet@v1
23 with:
24 dotnet-version: 3.1.101
25 - name: Create NuGet Package
26 run: dotnet pack -c Release /p:Version=${{ github.event.release.tag_name }} /p:PackageReleaseNotes="See https://github.com/Abhith/Code.Library/releases/tag/${{ github.event.release.tag_name }}"
27 working-directory: ./src
28 - name: Archive NuGet Package
29 uses: actions/upload-artifact@v1
30 with:
31 name: Code.Library
32 path: ./src/Code.Library/bin/Release/Code.Library.${{ github.event.release.tag_name }}.nupkg
33 - name: Archive NuGet Package
34 uses: actions/upload-artifact@v1
35 with:
36 name: Code.Library.AspNetCore
37 path: ./src/Code.Library.AspNetCore/bin/Release/Code.Library.AspNetCore.${{ github.event.release.tag_name }}.nupkg
38 - name: Publish Nuget Package
39 run: dotnet nuget push **/*.nupkg --api-key ${{ secrets.nuget_api_key }} --source https://api.nuget.org/v3/index.json --no-symbols true

Release to NuGet
Release to NuGet

You can see that I am pushing 2 NuGet packages and both have the same version number, which is same as my release tag on GitHub.

During Create NuGet Package stage, initially it was failing with following error

1.dll' to be packed was not found on disk

This was because, in one of the project, it had following Settings in the .csproj file

1<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

Once I removed the above, everything went well. You can see a similar issue posted here.

And the “Publish NuGet Package” job requires an API key from NuGet. You can get the API key from NuGet by logging into your NuGet account, create a new API key.

NuGet API Key
NuGet API Key

And I stored the key under my repo secrets.

Manage Secrets in GitHub Repository

To manage secrets, on your repo go to the “Settings” tab, under that “Secrets”. There I added a secret with name “nuget_api_key” and we can access those secrets inside actions like ${{ secrets.nuget_api_key }}.

Written by Abhith Rajan
Abhith Rajan is an aspiring software engineer with more than 8 years of experience and proven successful track record of delivering technology-based products and services.
Buy me a coffee

Was this helpful?

👈 This is a live react editor.

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub

Related ArticlesView All

Related VideosView All

Distributed .NET Core (DShop) - Episode 4 [Asynchronous microservices integration via events]

.NET Core Debugging with WSL 2 in Visual Studio 2019

Serving election results to an entire nation - Hallstein Brøtan - NDC Oslo 2020

Related StoriesView All

Related Tools & ServicesView All

toptal.com

gitignore.io

Create useful .gitignore files for your project by selecting from 509 Operating System, IDE, and Programming Language .gitignore templates
github-contributions.now.sh

GitHub Contributions Chart Generator

See all of your GitHub contributions in one image!