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.
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
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.
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.
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.
Now each time you trigger or queue a build, the build number will be incremented.