Dynamically changing build numbers by branch in TeamCity

We use feature branches, and build all of them on our TeamCity CI setup. Every build can be deployed on our test servers, but it’s useful to be able to quickly distinguish which ones came from master and which came from other branches. I took a script from here and edited it to simply append -alpha to the end of the version number for any non-master build:

$branch = "%teamcity.build.branch%"

if ($branch.Contains("/")) 
{
  $branch = $branch.substring($branch.lastIndexOf("/") + 1)
}

Write-Host "Building from $branch branch"

if ($branch -ne "master") 
{
  $buildNumber = "%build.number%-alpha"
  Write-Host "Appending alpha to build number"
  Write-Host "##teamcity[buildNumber '$buildNumber']"
}
else
{
  Write-Host "Leaving build number as-is"
}

This makes it really obvious when a build has come from a feature (or other) branch, to avoid accidentally deploying it etc. You could probably easily extend this to pull a version number from the branch name as well, or similar.

To use this across multiple builds, I just set up a build template with the script stored in the first build step, which you can then apply to builds either individually or across a project.

Something to add?

This site uses Akismet to reduce spam. Learn how your comment data is processed.