Featured image of post Github Action – own CI/CD with nuget package

Github Action – own CI/CD with nuget package

Since my last article on templates, I've been using them an unusually large amount. The longer I use them, the more tweaks, the more manual steps. With this article I decided to put an end to that repetitiveness: let's automate building templates and publishing them to Nuget.org. Unfortunately, the update on the local machine still has to be done manually.

Table of contents

In this article, I will not go into detail about creating Github actions. Nevertheless, I’d like this to be a place to start, so the following tips are suitable for any type of project that involves publishing nuget. At this point, I’m assuming that you already have a ready to go repository on Github - this article will not cover pushing your changes out to a remote repository.

1. In order to publish to the nuget.org repository, we must first create an account and create an API key

To do this, go to nuget.org - you can even log in there with your Microsoft account

nuget org signin
Ilustracja 1. New account creation view

2. Then go to the API keys page

After logging in, select your account name and from there API Keys.

nuget org apikey pages
Ilustracja 2. View of the menu after logging in

3. Then create a new API key

  1. Enter a key name in the Key Name field that specifies where you used the key. I used the name ZtrTemplates.

  2. Enter a pattern that specifies what packages can be published with this key, such as ZTR.*.

  3. Select Create.

  4. Immediately after creation, copy the key and temporarily do not close the card. The warning clearly states that once this card is closed, it will not be possible to obtain the key again.

nuget org apikeys create
Ilustracja 3. View of creating a new API key
nuget org apikey aftercreation
Ilustracja 4. And the view after creation, when copying the key is still possible

4. Add the API key to your repository

  1. To do this, go to the repository settings, then go to Actions and select New repository secret.

  2. Then enter the key name - try not to make a mistake in it, we will use it later. In my case I used the name NUGET_API_KEY, and paste the API key copied from nuget.org.

github add secrets to repo
Ilustracja 5. Repository secrets page
github add secrets to repo new secret
Ilustracja 6. Adding a new key

5. The next step is to create an action in our repository

The easiest way to do this is by going to the Actions page and then selecting set up a workflow yourself.

github setup first action
Ilustracja 7. View adding the first action

Then paste the following code into the action and validate everything on your branch:

The whole action on Github
name: Publikuj szablon do Nuget.org

on:
  push:
    branches: [ main ] (1)

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: Pack
      run: dotnet pack --no-build --configuration Release
    - name: Publish to nuget.org
      run: dotnet nuget push "./bin/Release/*.nupkg" -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json --skip-duplicate (2)
1 Here we specify that we want our action to be executed only when there are new commits on the main branch.
2 This is the heart of the action, we publish all nuget packages located in the ./bin/Release directory. By default only our package will be there, so this design protects us from renaming it in the future. In addition, in the -k ${{ secrets.NUGET_API_KEY }}, we pass our token to Nuget.org that we created earlier.

Additionally, it is worthwhile to delve into:

  • -s https://api.nuget.org/v3/index.json - the address where we want to publish, it is required in the nuget push command, unless this value is defined separately in the project file (more information in the documentation)

  • --skip-duplicate - causes the attempt to push the same package (with the same version) to be skipped and the whole action to complete successfully (without returning an error code).

github set action
Ilustracja 8. The whole should look similar

The code you see below is a cluster of elements from the setup-dotnet repository and the dotnet nuget push documentation.

6. Now make any changes to your code (template) and upload to the repository.

One of the most important changes will definitely be the version update. If you are working on a project file similar to the one in the previous article, just make a minor update to the project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <PackageType>Template</PackageType>

    <PackageVersion>1.1</PackageVersion> (1)
    <PackageId>ZTR.Utilities.Templates</PackageId>
    <Title>Zaprogramuj to raz! Core template</Title>
    <Authors>Zaprogramuj to raz!</Authors>
    <Description>Template for creating core projects</Description>
    <PackageTags>dotnet-new;ztr;templates</PackageTags>

    <IncludeContentInPack>true</IncludeContentInPack>
    <IncludeBuildOutput>false</IncludeBuildOutput>
    <ContentTargetFolders>content</ContentTargetFolders>

    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**" />
    <Compile Remove="**\*" />
  </ItemGroup>
</Project>

7. Wait for a while and see if there is already an update.

If so, update!

As a reminder, you can install the template as follows - it will be automatically downloaded from Nuget.org.

Installing templates from Nuget.org
dotnet new --install ZTR.Utilities.Templates

You can then check for possible updates and apply them using the following two commands:

Checking for updates and updating templates
PS C:\Users\dalec> dotnet new --update-check
Updates are available for the following:
An update is available for the template package ZTR.Utilities.Templates::1.0.0.
    command to install: dotnet new -i ZTR.Utilities.Templates::1.1.0

PS C:\Users\dalec> dotnet new --update-apply
An update to the ZTR.Utilities.Templates::1.0.0 template package is available.
    command to install: dotnet new -i ZTR.Utilities.Templates::1.1.0
Update in progress...
...Update was successful.

You can always preview the installed version with the dotnet new -u command.

Preview the new version of the templates
PS C:\Users\dalec> dotnet new -u
 ZTR.Utilities.Templates
    Szczegóły:
      NuGetPackageId: ZTR.Utilities.Templates
      Version: 1.1.0
      Author: Zaprogramuj to raz!

After you push your package out, you have to wait a while before it goes through the verification process. When all goes well you may see something like this on the package page:

nuget org template package page
Ilustracja 9. View of the nuget package page after the update

Done! Nothing like automation to relieve us of doing things that don’t actually add any value to our work.

comments powered by Disqus
Please keep in mind that the blog is in preview form at this point and may contain many errors (but not merit errors!). Nevertheless, I hope you enjoy the blog! Many illustrations appeared on the blog thanks to unsplash.com!
Built with Hugo
Theme Stack designed by Jimmy