Azure Devops - Automatic NuGet Versioning

NuGet packages are great right? If you’re a .NET developer then you will use them all the time. They offer a fantastic way to easily share your code across projects and build projects faster by using reusable components.

Now we’ve all use packages from Nuget.org, the Microsoft owned NuGet repository but did you know you can host your own package feeds in Azure Devops?

I’ve recently been working on a project where we needed to do just that. In Azure Devops these package feeds are called Artefacts and you can find out more about these here https://azure.microsoft.com/en-us/services/devops/artifacts/

Now I don’t want to go into the ins and outs of creating and managing packets, there’s enough content out there already that covers this. Instead I want to talk about a very specific problem you might face, and offer one possible solution.

That problem is versioning.

NuGet packages are typically versions with Semantic Versioning. That is {Major}.{Minor},{Patch}.

If you look at the Microsoft ASP.Net.MVC Package on Nuget.org you will see that it’s currently (at the time of writing) at version 5.2.7.

Now the packages I’ve been working with are in an Azure Devops repository with continuous integration. Every time I push changes to my develop branch, a new build is triggered which builds the code and publish the packages to our Nuget feed.

The issue though is that I need the version number to automatically increment every time. So how do we achieve this.

Well the first step is to change our .NET pack build task to use the build number for versioning. By default, Automatic Package Versioning is off.

image003.jpg

With that done, we can now configure our build number. In the Options tab, in the Build number format text box we’re going to use some variables to define our version number.

image002.jpg

Finally we need to define the Major, Minor and Patch variables.

In the Variables tab create 3 new variables called Major, Minor and Patch and set their values as shown in the following image

image001.jpg

As you can see, the Patch variable uses a counter to automatically increment the patch value. Once the Major or Minor variables are changes, the patch will reset and start incrementing from zero again.

I hope you find this useful, I’m working on a more complete post to cover package publishing from start to finish so keep an eye out for that over the next few weeks.

Previous
Previous

Building a Quality First Culture

Next
Next

Getting Started with UI Path