Deploying .NET templates using GitHub Actions

Once you've created a .NET template, you should automate the updates of the package using GitHub actions to remove the effort required in publishing updates
📅 05 Nov 2023

In the previous post Visual Studio Item Templates vs .NET Templates in 2023 - Gordon Beeming, I showed you how to create a .NET template. A template is great because you can easily share a way of doing something with your friends and the community.

However, if you have to upload the nuget package each time you make any changes manually to nuget.org, this will become tedious, and you'll likely stop updating the package. Let's look at how we'd automate this using GitHub Actions.

You may have noticed in the previous post that I specified a GitHub URL already, where I have uploaded the sample code, and I'll continue from that today to add the workflow to publish to nuget.org.

If we look back at the last post, the only work we had to do to get a package was nuget pack 




        

In that example, we passed in a version number so that we could control the version of our package without having to edit the nuspec file each time we make a change. 

Before creating the workflow to publish our template, we'll need to get an API key from nuget.org. Head over to NuGet Gallery | API Keys and then Click on Create, then there are a couple of settings that you should set for the API key

Make sure you configure the scope of the API Key to only what is required
Figure: Make sure you configure the scope of the API Key to only what is required

Make sure you copy the API Key, as you will only be able to regenerate the key in the future

Copy the API Key, as we'll use this in the next steps
Figure: Copy the API Key, as we'll use this in the next steps

Go to your repository on GitHub and Click on Settings | ASecrets and variables | Actions | New repository secret (if you are using environments in your repository, add an environment variable instead so it's scoped just to where you plan on publishing the package)

Name the variable NUGET_API_KEY and paste the value you copied from nuget.org

Next up, create a new file at .github\workflows\publish-dotnet-template.yml and copy the below contents into it




        

The main thing to take note of in this workflow is that we are creating a version number using the date and the number of times the workflow has executed, then we use that version for our nuget pack and then lastly, we use the NUGET_API_KEY that we've just added to the repo secrets to publish a new version to nuget.org.

Save the file and head over to the Actions tab to watch the workflow run, once done you should see a successful work like below

This success of this workflow means a new version of our package has published to nuget.org
Figure: This success of this workflow means a new version of our package has published to nuget.org

We can now head over to nuget.org and see that our package has published as expected

Version 2023.11.5.1 of the package GordonBeeming.DefaultFiles.CSharp was publish from this workflow
Figure: Version 2023.11.5.1 of the package GordonBeeming.DefaultFiles.CSharp was publish from this workflow

If we check for an update from the dotnet cli, we'll see this version is ready as well

dotnet new update --check-only shows a new update of our package is available
Figure: dotnet new update --check-only shows a new update of our package is available

Conclusion

As you can see, it's effortless to publish .NET templates using GitHub actions, and there's really no reason to publish them manually as it's easy and free 😅.

You can download or view the code from this post on GitHub GordonBeeming/DefaultCSharpRepoFiles