I spent an afternoon researching and implementing the use of incremental Version Numbers as part of a TeamCity build process for an ASP.NET application.
The setup was easier than I expected. TeamCity includes an “Additional Build Feature” called AssemblyInfo patcher. The build feature is in the projects configuration settings at the bottom of the build steps page in v8.0.4.
The settings for the AssemblyInfo patcher are straight forward. The Assembly Version, Assembly File Version, and Assembly Informational Version attributes in AssemblyInfo files will be replaced by the values set in this dialog.
My intention was to manually control the major and minor version number but allow an incremented build number. To do this, I created a Configuration parameter called Major.Minor with the value set to “0.1”. I set the Build Number Format, on the General Settings page, to be a combination of the Configuration parameter and the project’s build number (i.e. %Major.Minor%.%build.counter%). At build time, TeamCity will use the build number format to set the value of the system property %system.build.number%.
Back in the AssemblyInfo patcher’s settings, I set the Assembly Version and Assembly File Version values to %system.build.number%. I wanted to add a custom string to the build number to find a larger grouping of features implemented in a release. I could have done this by adding a fourth integer value to the build number but I wanted to use a more informative string instead of an integer. To do this I added the Assembly Informational Version attribute to all of my projects and assigned the TeamCity setting to be “%system.build.number% (%system.version_identifier%)”.
I wanted one last thing from the build process. I wanted to tag the Git revision with the build number to find which revisions were included in a particular build. Fortunately this is as simple as selecting a radio button in TeamCity. On the Version Control settings page, I selected the “Successful only” radio button under “VCS labeling mode” and set the labeling pattern to “build-%system.build.number%”. Then it’s all done, TeamCity will create a build number when the build starts, set the Assembly, Assembly File, and Assembly Informational Version attributes for all projects being built, and if the build is successful it will tag the Git revision at the end.