Renaming Azure DevOps build/release
azure devops

How to rename a build with information known only during the process using ADO logging commands.
July 14, 2020

Sometimes (almost always) it becomes handy to modify the name automatically assigned to Azure DevOps build or release. For instance, when the application version is defined in code (appsettings.json or AssemblyInfo.cs) it’s useful to see it in the build name.

Renamed CI build for a change branch

Azure DevOps has an easy but not very flexible way of specifying the build name with Build number format:

Default build number format

There are a few variables available to use here, but it doesn’t help in a situation where you need a value which is not known at the time of build creation.

Logging commands

Is there an alternative? Of course: Azure DevOps logging commands, and particularly ##vso[build.updatebuildnumber].

Just create a PowerShell or Bash task and print the new name string to stdout.

Example: Let’s assume that in the previous step we set the _VERSIONNUMBER variable so that it can be used here:

# _versionNumber was set in the previous step. Since build ID is part of version, there's no naming conflict when running builds repeatedly.
[string] $buildName = "$env:_VERSIONNUMBER" + "_$(Build.SourceBranchName)"

Write-Host "Setting the name of the build to '$buildName'."
Write-Host "##vso[build.updatebuildnumber]$buildName"

This script renames the current build to contain the version and source branch name. Since in this case we know that versionNumber is unique (it’s being changed on every run to something like 4.109.0.XXX), this is fine.

Adding uniqueness

What if we don’t have such unique number available? We can use the $(Rev.r) variable in the build number format and then include $(Build.BuildNumber) when overwriting it. Bash example:

full_commit_id=$(Build.SourceVersion)
commit_id=${full_commit_id:0:8}
echo "##vso[build.updatebuildnumber]${commit_id}$(Build.BuildNumber)_$(Build.SourceBranchName)"

This produces build name which consists of a commit ID, unique number and source branch name.

Build with commit id, unique number and branch name

Connecting build and release

To close the loop it’s also useful to reflect the source build in a corresponding release name. That’s easier, because triggering build name is known in the time of release creation, so the standard Release name format can be used here:

Release name format

$(Build.BuildNumber)-$(rev:r)

comments powered by Disqus