Platform Engineering

Matt Corr

How to automate Project Build numbers in VSTS

Posted by Matt Corr on 30 November 2017

CICD, Team City, VSTS, Blog, Technology

If you have migrated over to VSTS (or TFS 2017) from TeamCity, you may have a noticed a useful feature missing.

Now VSTS defaults the build number format to a revision number in the format similar to as shown below.

2017-11-30_09-30-54 (1).png

So whats the problem?

This is fine if you are happy with an incrementing number across all projects. But we have found in the past we prefer an incrementing number for each project. TeamCity has this out of the box with the Build Counter variable.

2017-11-12_11-03-15 (3)

This useful feature allows us to version our projects with something like SemVer 2.0 to give us version numbers like 1.0.234, where the last number is incremented per build of a specific project.

All projects would start out with a 1.0.1 number and then increment from there. Projects build more frequently would have a higher number, projects only built occasionally would have a lower number.

I was puzzled that it is not available by default on VSTS, however we can add something very similar without too much effort.

I had seem some samples on the Internet that refer to modifying static text files to maintain project version numbers, but I would much prefer to use a project variable and use the VSTS REST API to update the counter on each build. This way all the metadata is kept together with the project and without any external file dependencies.

How to implement a Build Counter in VSTS

In your VSTS project, create a variable called ProjectBuildNumber and give it an initial value of 1

2017-11-29_23-18-27.png

In the Options section, set the Build number format to 1.0.$(ProjectBuildNumber)

NOTE: This is just a sample, your numbering standard might be a little different.

2017-11-12_12-16-27

In your Tasks section of your Build definition, include a new step for running a PowerShell script.

NOTE: It is up to you if you want to use a script from a repository or an inline script, for this blog post we will use an inline script for simplicity.

Set the following on the PowerShell script screen:

  • Set the Version to 2.*
  • Set the Display Name to something descriptive
  • Set the Type to Inline
  • For the Script content use this script.
2017-11-29_22-41-48

Update the two fields at the top, the valueName is the name of the variable you added. This example uses “ProjectBuildNumber”

The token is the PAT (Personal Access Token) you can generate from clicking your profile picture at the top right of the VSTS site and going to the Security section.

NOTE: It would be more ideal to use a system account PAT, rather than your own PAT, so that build process activities are always performed by system accounts and not tied in directly with user accounts.

Click the Save and Queue button. In the build logs, you will see the number being incremented from and the build number using the current value.

2017-11-29_23-22-16.png

Now each time you trigger or queue a build, the build number will be incremented.

2017-11-29_22-50-34.png