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!

Comments (6)

  1. Steve Gross says:

    OK, I see how to follow your directions to add BuildNumber to the msbuild command-line args.

    Is there an easy way to access this information inside the C# code itself? For instance, when my app launches, I want it to do something like this:

     MessageBox.show("You're running build #" + BuildNumber);

    Thanks,

    –Steve (mrstevegross@gmail.com)

  2. Terra says:

    You could use reflection and grab the dll version.

    Console.WriteLine("The version of the currently executing assembly is: {0}",

               Assembly.GetExecutingAssembly().GetName().Version);

    Example taken from: msdn.microsoft.com/…/system.reflection.assemblyname.version.aspx

  3. Very helpful says:

    Thank you very much, it is very helpful for me in my project. We are trying to automate the build in your project and wanted to create the build output zip file with build number format. Looked in so many websites, finally we acheived with the help of this article. Once again thank you very much.

  4. Lakshmi Reddy says:

    Hi Jason, it is really helpful for me.

    I hope DropLocation also available in BuildDetail along with BuildNumber. I tried to access it, but getting error. Can you please tell me how to access Droplocation?

  5. Modifying build process Template says:

    Please help.

    Here is what I have added onto my TFS Build Process Template (tfvc Template12.xaml)

    <mtbw:ProcessParameterMetadata ParameterName="IsMajorVersion"     BrowsableWhen="Always" Category="HCSCM"     DisplayName="2. Increment major number"    Description="Set to true to increment the major version number. Setting this property to true will reset the minor, build and revision numbers to zero." />

         <mtbw:ProcessParameterMetadata ParameterName="IsMinorVersion"     BrowsableWhen="Always" Category="HCSCM"     DisplayName="3. Increment minor number"    Description="Set to true to increment the minor version number. Setting this property to true will reset the build and revision numbers to zero." />

         <mtbw:ProcessParameterMetadata ParameterName="IsBuildVersion"     BrowsableWhen="Always" Category="HCSCM"     DisplayName="4. Increment build number"    Description="Set to true to increment the build version number. Setting this property to true will reset the revision number to zero." />

         <mtbw:ProcessParameterMetadata ParameterName="IsRevisionVersion"  BrowsableWhen="Always" Category="HCSCM"     DisplayName="5. Increment revision number" Description="Set to true to increment the revision version number." />

    The question is, how am I be able to control each build version numbers individually?

    For example:

    If I want just to increase only the minor version number (i.e.  by +1), by setting it to TRUE. and leave the other setting at FALSE

    How do I make it works ?

    Thank you

  6. You can change the build number anyway you like. You can update it like any other Build field using the SetBuildProperties activity (msdn.microsoft.com/…/gg265783.aspx)

    The real trick is how you parse it. We don't have any help for that 🙁

    Thanks,

    Jason

Skip to main content