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!
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”.
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.
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.
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.
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.
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.
As an alternative, look at my yaml for Azure DevOps integrated with Github
https://github.com/ignatandrei/stankins/blob/master/azure-pipelines.yml
The job : SetProps
shows how to do it from the powershell command line
good job