Linking .Net software versions to repository versions using TeamCity

I tried to link the version numbers of our compiled files to the revision number of the SVN when the source that the file was built on was committed. My goal is to have a version number for every binary that looks like MAJOR.MINOR.TEAMCITY_BUILD.SVN_REVISION.

TeamCity has a handy tool on the Build Steps page of the project's build configuration. At the bottom where it says "Additional Build Features", you can add "AssemblyInfo patcher" which will set the whole version number in every AssemblyInfo.* file in your project. Very handy... unless you want to be Mr. Special-case like myself.

You see, I have several .Net projects within this one TeamCity project, and they need different major and minor versions. Several of these projects have already gone through multiple development iterations, so I have a 4.X version of one program, and a 1.X version of another. I have a need to increment the Major and Minor versions of several different projects separately, but I still want to apply the TEAMCITY_BUILD.SVN_REVISION numbers too. I don't much like the idea of syncing up all the major and minor version numbers, nor do I really want to figure out the best way to break all the projects up into different TeamCity projects linking them via dependencies or whatever.

Peter von Lochow posted about this very need, and solves my problem in his comments section.

So here's my slightly modified solution based on Peter von Lochow's comment:

Set the Build number format in the General Settings section of the project's configuration to {0}.%build.vcs.number% - in this case, that is the build counter followed by the SVN revision number.

Add a Powershell build step to the TeamCity project, and reorder the build steps so that this new step is the first to run.

Set Powersehll run mode to x86, set Script to Source code, and set Script execution mode to Put script into PowerShell stdin with "-Command -" arguments.

Script source:

(Get-childitem -include AssemblyInfo.* -recurse) | Foreach-Object { sc $_ ((Get-content $_) -replace '^(.*AssemblyVersion\(\"\d\.\d)\..+(\"\).*)$', '$1.%build.number%$2' -replace '^(.*AssemblyFileVersion\(\"\d\.\d)\..+(\"\).*)$', '$1.%build.number%$2')}

That regular expression will replace whatever falls between the major.minor version numbers and ") with whatever we set the Build number format in Teamcity - in this case we set it to the build counter and the SVN revision number.

Now Teamcity will compile with build numbers and SVN revision numbers in the file versions. This should make it much easier when trying to track bugs found in deployed files now that you can match up a snapshot of the SVN to that compile.

Next up: get the setup projects (MSI projects) to increment their version numbers to match the TEAMCITY_BUILD number.