Follow us

Azure Pipeline Create Custom Build Number

When you implement CICD for your project then, it is more important, how you manage multiple build versions. So that you know very well, which version is current running on your environment and help you to keep multiple versions of artifact and which you could use either for rollback and deploy to specific version.

In simple scenarios, you must have faced the situation, where some build not good for the environment and you want to rollback and go back to pervious version, so if your pipeline managed well on managing versioning of your each build, then it very much easy to switch maintain and get specific build version artifact.

 

Let say, you have your running build pipeline and when it run, it generate default build number in the format of "yyyyMMdd.<increment-number>".

The above default build number format is actually configured as default option with your pipeline.

Check the Options tab:

Build number: $(date:yyyyMMdd)$(rev:.r)

 

Now I want to use my own build number format as per your sprint development, so that I should know version of my build and able to use and manage multiple versions.

I have created two variable part of my pipeline and configured "VersionNumber" variable initial value to "1.0.0", so my default version will start with it and the other variable i.e. "VersionRevision" where I am doing increment of 1 on each build trigger.

Create two variables and you can put default value

VersionNumber = 1.0.0

VersionRevision = $[counter(variables['VersionNumber'],1)]

 

Then with option tab, you can change to use variable instead of having date format as build number:

Build number: $(VersionNumber).$(VersionNumber)

Now when ran my pipeline, build version generated as:

 

Now actually, I want pick build version from my project source code, so that whenever developer commit code, then, build version should be updated by developer to generate specific version as per sprint development.

So do that, I have created "BuildProperties.xml" file on root location of my project and push that file with below structure:

<CurrentBuildVersion>

<MajorVersion>1</MajorVersion>

<MinorVersion>0</MinorVersion>

<HotfixVersion>0</HotfixVersion>

</CurrentBuildVersion>

 

Now come to your Azure pipeline and add one powershell task before code/solution build task and write below inline script to pick version from above BuildProperties.xml file and populate to VersionNumber variable.

Write-Host "Getting version from project file"

 

$xdoc = new-object System.Xml.XmlDocument

 

$file = "$(Build.SourcesDirectory)\$(ProjectName)\BuildProperties.xml"

Write-Host "BuildProperties file path is: "$file

 

$xdoc.load($file)

 

$majorVersion = $xdoc.SelectSingleNode("CurrentBuildVersion//MajorVersion").InnerText

 

$minorVersion = $xdoc.SelectSingleNode("CurrentBuildVersion//MinorVersion").InnerText

 

$hotfixVersion = $xdoc.SelectSingleNode("CurrentBuildVersion//HotfixVersion").InnerText

 

Write-Host "Major version is: "$majorVersion

Write-Host "Minor version is: "$minorVersion

Write-Host "Hotfix version is: "$hotfixVersion

 

Write-Host "Getting version from project file is completed, version is: "

Write-Host "$($majorVersion).$($minorVersion).$($hotfixVersion)"

 

$fullVersion = "$($majorVersion).$($minorVersion).$($hotfixVersion)"

Write-Host "Complete version: "$fullVersion

 

echo $fullVersion

echo "##vso[task.setvariable variable=VersionNumber]$fullVersion"

 

Add one more powershell task to use VersionNumber and VersionRevision variable to update your project build number and use below incline script with that.

Write-Host "##vso[build.updatebuildnumber]$(VersionNumber).$(VersionRevision)"

 

And then with option tab, probaly you should change build version to use build id, so that when ti start it will show build id and later after above step execution, it will chagne to build version updated.

Build number: $(Build.BuildId)

 

Now when ran my pipeline, build version generated as:

 

So by doing above steps, I able to manage my build version from the source code and then, I can add one more task to my pipeline to place all the build version artifacts to my S3 location, so that I will have all different versions of my project.

Categories/Tags: azure devops~cicd

Recent Articles

1

AWS Saving Plan - Cost optimization tips

2
3

AWS RDS Key Concepts & Why you should use it?

4
5

Open-Search/Kibana - Multi Tenancy Setup

See All Articles