Custom Build Numbers in Team Build

The Team Build service in Team Foundation Server includes the current date in the build number by default. This is really difficult to understand especially when there are lot of builds and you have single team build type for all your environments.

How about having the build numbers some thing like ...

CI_2.5.1
CI_2.5.2

CI_2.5.176

In order to do this, we'll customize the Team Build script (TFSBuild.proj) for the project, hooking in to the default Team Build BeforeCompile event. Check TFSBuild.proj out via the Source Control Explorer, and open it for editing. Add these lines to the bottom of the build script

 <!-- customized extensibility event BeforeCompile -->
<Target Name="AfterGet" DependsOnTargets="VersionAssemblies">
  <Message Text="AfterGet is firing!"/>
</Target>

<!-- add build number to AssemblyDescription field -->
<Target Name="VersionAssemblies" DependsOnTargets ="GetAssemblyInfos">
  <Attrib Files="@(AssemblyInfos)" ReadOnly="false"/>
  <FileUpdate Files="@(AssemblyInfos)" Regex="AssemblyDescription\(&quot;[^&quot;]*&quot;\)" 
              ReplacementText ="AssemblyDescription(&quot;$(BuildNumber)&quot;)" />
</Target>

<!-- get a list of all AssemblyInfo files -->
<Target Name="GetAssemblyInfos">
  <CreateItem Include ="..\**\AssemblyInfo.cs">
    <Output ItemName ="AssemblyInfos" TaskParameter="Include"/>
  </CreateItem>
  <Message Text="These AssemblyInfo.cs files were found:"/>
  <Message Text ="@(AssemblyInfos)"/>
</Target>

You'll also need to download the MSBuild Community tasks, and install those on your build server. Add a reference to the MSBuild Community tasks near the top of your TFSBuild.proj file, like so:

 <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />

Check in the TFSBuild.proj file, then perform a build and you will be seeing the build numbers that is custom generated.

What we're really doing is popping the $(BuildNumber) variable into the AssemblyInfo.cs file. Specifically, we're putting it in the AssemblyDescription field.