Using build counters in Azure DevOps


We recently migrated some builds from TeamCity to Azure DevOps at my client, and couldn't find a feature analog to TeamCity's %build.counter% which we've been using to get automatically incremented version numbers for artifacts.

Since we couldn't find the corresponding feature in Azure DevOps we settled on using the $(Build.BuildId) variable which is the canonical ID for a build. Not exactly ideal but it solved the immediate issue.

Today I found a way of accomplishing the same thing in Azure DevOps, but sadly it's not properly documented even if it's briefly mentioned in the official documentation. The following lines in your azure-devops.yml will set the build number to an incrementing number that's scoped to the build definition.

name: 1.0.$(myBuildCounter)
  myBuildCounter: $[counter('buildCounter',1)]

The example above will create a build definition counter called buildCounter that will be initialized to the value 1. This value will be incremented for each new build. If you ever want to reset or reseed this value you'll have to use the Azure DevOps HTTP API.

If you have an option, consider using something like GitVersion or MinVer to dynamically calculate a version number from a git repository.