Skip to main content

Github - Deploy a Nuget Package on a new release

I have a small library that I created a few years ago to use PostgreSQL as a distributed cache in .NET core.

As I wanted to get more acquainted with Github and all it possibilities, I thought it was a good occasion to make some improvements and optimize the deployment process. (I manually pushed the first version of the package to NuGet.org).

Let’s see how we can automate this by combining the Release functionality in Github with Github actions.  

Create the Github actions workflow

Let’s first create a workflow that does the following:

  • Build the library
  • Create the NuGet package
  • Upload the NuGet package to NuGet.org

Therefore we go to our Github page and select Actions.

Choose New workflow.

We will not use one the suggested workflows but want to create our workflow from scratch so click on set up a workflow yourself.

Now we’ve finally arrived on the Edit workflow screen.

Replace the existing workflow definition with the following:

What is important to notice in the workflow above:

  • The workflow is triggered when we publish a new release(more about this later)
  • We use the tag_name value that can be specified during a release to update the package version:

dotnet pack --configuration Release --no-restore /p:Version=${{ github.event.release.tag_name }}

  • The api key required to push our package to NuGet.org is managed outside the workflow and stored as a Github secret using ‘NUGET_API_SECRET’ as its name:

dotnet nuget push **/*.nupkg --api-key ${{ secrets.NUGET_API_SECRET }} --source https://api.nuget.org/v3/index.json

Set the secret

To set the secret we need to go to Settings.

There we need to go the Secrets section and create a new secret using the New repository secret button.

Specify a name(in this case NUGET_API_SECRET) and a value:

Create a release

Now we can finally create a release. On the home page of your repo click on Releases on the right.

On the Releases page, click on the Draft a new release button.

Hit Choose a tag and enter a new release number here e.g. 2.0.2. What is important here is that this number is a valid NuGet package version number because it is this tag value that will be used inside our workflow.

Click on Publish release.

This should trigger the workflow and after a few minutes, you should see a succesfull run of the workflow on the Actions tab.

Popular posts from this blog

.NET 8–Keyed/Named Services

A feature that a lot of IoC container libraries support but that was missing in the default DI container provided by Microsoft is the support for Keyed or Named Services. This feature allows you to register the same type multiple times using different names, allowing you to resolve a specific instance based on the circumstances. Although there is some controversy if supporting this feature is a good idea or not, it certainly can be handy. To support this feature a new interface IKeyedServiceProvider got introduced in .NET 8 providing 2 new methods on our ServiceProvider instance: object? GetKeyedService(Type serviceType, object? serviceKey); object GetRequiredKeyedService(Type serviceType, object? serviceKey); To use it, we need to register our service using one of the new extension methods: Resolving the service can be done either through the FromKeyedServices attribute: or by injecting the IKeyedServiceProvider interface and calling the GetRequiredKeyedServic...

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

Kubernetes–Limit your environmental impact

Reducing the carbon footprint and CO2 emission of our (cloud) workloads, is a responsibility of all of us. If you are running a Kubernetes cluster, have a look at Kube-Green . kube-green is a simple Kubernetes operator that automatically shuts down (some of) your pods when you don't need them. A single pod produces about 11 Kg CO2eq per year( here the calculation). Reason enough to give it a try! Installing kube-green in your cluster The easiest way to install the operator in your cluster is through kubectl. We first need to install a cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.5/cert-manager.yaml Remark: Wait a minute before you continue as it can take some time before the cert-manager is up & running inside your cluster. Now we can install the kube-green operator: kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml Now in the namespace where we want t...