I’m currently using Visual Studio Online for my tooling and I was searching for a way to increment the build version for my solution. After searching the internet I found the following blog post which I applied, but for some reason it was not working as expected. I’m not using a default version format and this was causing the build to fail. In this blog post I’m going to share how I fixed it.
Before I start, I want to point out the best practices regarding .NET assembly versioning which can be found here.
The problem and solution
I created the variables as the blog post described and set the build format number “$(BuildDefinitionName)_$(MajorVersion).$(MinorVersion)$(Rev:.r)” which is like 1.0.1.
Before running a build I have added the “ApplyVersionToAssemblies.ps1” script and included the PowerShell script in a build step. Next step is starting a build but it failed with the following PowerShell error:
Could not find version number data in BUILD_BUILDNUMBER.
Looking into the script this the error is caused by the the RegEx check which is for finding the version number from the build format property. Because I’m not using a standard formatting number the script is expecting a build number like 0.0.0.0. In order to fixes the error we need to modify the “ApplyVersionToAssemblies.ps1”. On line 16 in the regular expression is defined and I modified to the following:
# Regular expression pattern to find the version in the build number
# and then apply it to the assemblies
$VersionRegex = "\d+\.\d+\.\d+"
After the modification the build was successful and the version was applied correctly.
With some small modification in the “ApplyVersionToAssemblies.ps1” PowerShell script it is possible to modify the build format. I took me some time to figure it out and get it working so I hope it helps a bit.