TFS2010 – Where is $(BuildNumber)?

So, I have been asked this very question a couple of times now which means it should be searchable for all :).

In TFS Build 2008, all of the logic for building on the server was tucked away in a targets file for MSBuild. All Team Build did was to call MSBuild on a special TfsBuild.proj file and pass in on the command line a ton of information.

When we switched everything to use Windows Workflow, we stopped using a tfsBuild.proj file, got rid of the targets file, and stopped passing so much information to MSBuild. Unfortunately, one of the bits that used to be configured within MSBuild is now configured outside in the workflow and we don’t pass it in. That’s right, it’s the BuildNumber.

The BuildNumber for the build is determined as one of the first actions in the workflow and the build detail is updated. So, if you are changing the process template for the build (that’s the defaultTemplate.XAML file), you can access the buildnumber really easily – buildDetail.BuildNumber. However, from within the proj files or even your own custom targets files, it is not available. That is unless you change the workflow to pass it in.

The change is relatively simple.

Here is what the MSBuild line looks like in our default template (this may be slightly different than what you have – just an example):

<mtbwa:MSBuild CommandLineArguments="[String.Format(&quot;/p:SkipInvalidConfigurations=true {0}&quot;, MSBuildArguments)]" Configuration="[platformConfiguration.Configuration]" DisplayName="Run MSBuild for Project" GenerateVSPropsFile="[True]" sap:VirtualizedContainerService.HintSize="269,100" OutDir="[BinariesDirectory]" Platform="[platformConfiguration.Platform]" Project="[localBuildProjectItem]" Targets="[New String() { &quot;Clean&quot; }]" TargetsNotLogged="[New String() {&quot;GetNativeManifest&quot;, &quot;GetCopyToOutputDirectoryItems&quot;, &quot;GetTargetPath&quot;}]" ToolPlatform="[MSBuildPlatform]" Verbosity="[Verbosity]" />

You just need to change it to something like this (the highlighted parts were added):

<mtbwa:MSBuild CommandLineArguments="[String.Format(&quot;/p:SkipInvalidConfigurations=true /p:BuildNumber={1} {0}&quot;, MSBuildArguments, BuildDetail.BuildNumber)]" Configuration="[platformConfiguration.Configuration]" DisplayName="Run MSBuild for Project" GenerateVSPropsFile="[True]" sap:VirtualizedContainerService.HintSize="269,100" OutDir="[BinariesDirectory]" Platform="[platformConfiguration.Platform]" Project="[localBuildProjectItem]" Targets="[New String() { &quot;Clean&quot; }]" TargetsNotLogged="[New String() {&quot;GetNativeManifest&quot;, &quot;GetCopyToOutputDirectoryItems&quot;, &quot;GetTargetPath&quot;}]" ToolPlatform="[MSBuildPlatform]" Verbosity="[Verbosity]" />

You could also do something similar to pass in whatever information you have in the workflow. Note that MSBuild will treat everything as a string, so only strings should be passed in. Also note that for values that won’t change, you can use the Process Parameter MSBuildArguments which is already passed in above.

Happy Building!