Menu

MSBuild, TeamCity & Versioning

November 1, 2009 - .NET / C#, Continuous Integration, Geek Bits
MSBuild, TeamCity & Versioning

I have only recently start using TeamCity at home for my own major project SampleSort.  It is a pretty complex application all in all and the build process is also complex due to the modular nature of its internal structure.  Now this is all controlled by TeamCity it makes proper versioning a real proposition rather than incrementing a version number by hand.  Even though it is possible to version assemblies using the 1.0.* pattern in the version attributes in the AssemblyInfo.cs file this isn’t the best solution for my particular scenario.

Now if you are using source control it is useful to have the version number of the source incorporated into the overall application version.

Using TeamCity I have manually set the Major and Minor so that I have some control over it but the build number comes from TeamCity and the revision is taken from my Subversion repository.  To do this you need to use a pattern like this in the ‘Build Number Format’ setting on the General Settings page of your build configuration:

0.6.{0}.{build.vcs.number}

The 0.6 section is the portion I retain control over, the {0} is filled in by TeamCity an the {build.vcs.number} is the Subversion repository number.

Now to make use of this in the build process you need to access this information and update the AssemblyInfo.cs files BEFORE the build system performs the Release build. I looked at the community tasks for the AssemblyInfoTask but was a little put off.  For one thing it creates the file rather than edits the existing one, which is a 20+ project scenario would result in a fairly hefty build script.  All I wanted to so was update the existing AssemblyInfo files.  So, my MSBuild target is as follows:

<PropertyGroup>
<Version>$(BUILD_NUMBER)</Version>
</PropertyGroup>

<Target Name="VersionAssemblies">
<Attrib Files="@(AssemblyInfoFiles)" Normal="true"/>
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyVersions*(s*&quot;.*&quot;s*)s*]"
ReplacementText="AssemblyVersion(&quot;$(Version)&quot;)]"/>
<FileUpdate Files="@(AssemblyInfoFiles)"
Regex="AssemblyFileVersions*(s*&quot;.*&quot;s*)s*]"
ReplacementText="AssemblyFileVersion(&quot;$(Version)&quot;)]"/>
</Target>

So here we are using RegEx to perform a find and replace on the AssemblyVersion and AssemblyFileVersion attributes within the AssemblyInfo.cs files. Nice!

Share

One thought on “MSBuild, TeamCity & Versioning

avatar
Tim Long

That’s one solution, there is a simpler approach though. Create a GlobalAssemblyInfo.cs file and put it in the root solution folder. The use Add Existing Item to add that file to each of your projects. Now here is the nugget of wisdom.. on the file dialog the Add button has a drop-down next to it, pull it down and there is an Add As Link option, which creates a shortcut to the original file. So all your projects will reference the single copy of GlobalAsseblyInfo.cs in the solution folder.

Then you can use the AssemblyInfoTask to simply generate the one file on the build server and all your projects will use it. I do this in a seperate build step in TeamCity.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*