Automated date based versioning for ASP.NET Core assemblies using Azure DevOps

I needed automatic version numbering based on current date when web application is built. It was wish by some customers and in their projects it’s okay with it. As their code is covered with automated tests and other diagnostics I’m using Azure DevOps as build server and this is where I made automated date based versioning work.

Before religious wars about versioning I mention that this kind of versioning doesn’t expose absolute truth or best practices. It comes with possible consequences and obstacles and one shouldn’t apply it to every project possible. I just happened to have few situations where date based versioning makes sense and is safe to use.

I tried few Azure DevOps tasks by third-parties and I got things running like expeced with Manifest Versioning Build Tasks by Richard Fennell. It’s free to use, no hidden or additional expenses!

Manifest Versioning Build Tasks by Richard Fennell

Adding and configuring versioning task

Installation was easy. I installed it to my Azure DevOps account through adding new task to build pipeline and then added .NET Core version step. Build pipeline for my demo application is here and the name of versioning task is “Version .NET Core assemblies”.

Version .NET Core Assemblies build task

To get it work few additional steps are needed. Under build pipeline options I changed the format of version number like shown on the following screenshot.

Setting build number format

For those who want to repeat my steps the number format string is here for copy-paste: $(Build.DefinitionName)_$(date:yyyy).$(date:MM).$(date:dd)$(Rev:.r).

Next step is to conigure version number task.

Configuring Version .NET Core Assemblies task

These settings should be enough to get automatic versioning work. No need to change anything in project files.

Build output of versioning task

Here is the fragment of versioning task output when build happened. I made red rectangles around important parts of versioning task output.

Version .NET Core Asseblies task output

As a first thing task detects current build number. This is built based on options I set before. Then regular expression (Version Filter) is applied to build number and as a result date based version number is extracted. This version number is automatically set to all projects I have in repository because I made no more specific rules.

Displaying version number in ASP.NET Core application

To display version number at footer of web page I use the following code in layout page.

<div class="col-md-4 col-sm-4">
     <p class="powered">Version: @System.Reflection.Assembly.GetAssembly(GetType()).GetName().Version.ToString()</p> </div>

Here is how it looks on web page footer.

Version number on web page

Wrapping up

With Azure DevOps service it’s possible to move versioning of release assemblies to build pipeline keeping source code and developers machines clean of additional ballast. With right tools it’s easy to play with version numbers and automate versioning in build server. This blog post wen through steps to take to enable date based versioning of .NET Core assemblies. I spent around 15 minutes to get things work with task mentioned above and I think it’s stong enough argument to blog about it.

Liked this post? Empower your friends by sharing it!

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    2 thoughts on “Automated date based versioning for ASP.NET Core assemblies using Azure DevOps

    Leave a Reply

    Your email address will not be published. Required fields are marked *