Partilhar via


Generating Custom Build Numbers in TFS Build 2010

In TFS Build 2008 you could generate custom build numbers by overriding the BuildNumberOverrideTarget and setting a property called BuildNumber. What does this look like in TFS Build 2010? The default build numbers are generated by the Update Build Number activity towards the beginning of the workflow as shown here:

image

The default activity generates build numbers based on the Build Number Format specified on the Process tab of the Build Definition dialog:

image

So how do we replace this with our own custom build number generation scheme? We simply remove the existing activity from the workflow and replace it with one of our own.

Getting Started

  1. Familiarize yourself with how to create and most importantly deploy custom workflow activities for TFS Build 2010.
  2. If you’re currently using the Default Template (DefaultTemplate.xaml) branch it to a new template that you can modify. This allows you to always refer back to the original template in the future and may simplify applying service packs or upgrading to later versions of TFS Build.

Removing The Existing Build Number Generation Scheme

  1. Edit the template that you branched.
  2. Delete the Update Build Number activity from the workflow.
  3. Open the Arguments window (by clicking the Arguments button at the bottom of the workflow designer) and delete the BuildNumberFormat argument.

Creating A New Build Number Generation Scheme

  1. Create a Code Activity workflow activity in a new (or existing) custom activity library. See Jim Lamb’s post for details.

  2. Apply the BuildActivity attribute (from Microsoft.TeamFoundation.Build.Client) to your custom activity:

    [BuildActivity(HostEnvironmentOption.Controller)]

  3. Override the CacheMetadata method to inform the workflow engine that you require IBuildDetail:

    protected override void CacheMetadata(CodeActivityMetadata metadata)

    {

        base.CacheMetadata(metadata);

        metadata.RequireExtension(typeof(IBuildDetail));

    }

  4. Override the Execute method, set the BuildNumber property on the IBuildDetail object, and call Save:

    protected override void Execute(CodeActivityContext context)

    {

        Random randomGenerator = new Random();

        int buildNumber = randomGenerator.Next();

        var buildDetail = context.GetExtension<IBuildDetail>();

        buildDetail.BuildNumber = String.Format("{0}_{1}", buildDetail.BuildDefinition.Name, buildNumber);

        buildDetail.Save();

    }

  5. Compile and upload your custom activity library. See Jim Lamb’s post for details.

  6. Edit the template you branched.

  7. Drag your new custom activity from the toolbox into the workflow where the original Update Build Number activity was.

  8. Check-in your updated template and test.

Comments

  • Anonymous
    January 21, 2010
    Hi William, Is the override of CacheMetadata really necessary? I have implemented some activities that use the GetExtension<IBuildDetail> method, and it works fine without the override. Cheers /Jakob

  • Anonymous
    January 25, 2010
    The CacheMetadata function provides additional information to the runtime about the dependencies of the activity. As you've found though it's not mandatory to implement it.

  • Anonymous
    April 26, 2011
    Thank you!!!  I needed to add 5000 to the build number and your example helped me.

  • Anonymous
    September 15, 2011
    Hello, thank you for the information, however we want to increment based on the last build number.  How can we get the last build number?  in vs2008 we used to be able to do this w/ a custom task, how can we get $(LastBuildNumber) ? <UsingTask TaskName="MyAssembly.GetBuildNumber" AssemblyFile="MyAssembly.dll" /> <Target Name="BuildNumberOverrideTarget">     <GetBuildNumber BaseBuildName="MyApp_1.0" LastBuildNumber="$(LastBuildNumber)">      <Output TaskParameter="BuildNumber" PropertyName="BuildNumber" />   </GetBuildNumber> </Target>

  • Anonymous
    March 05, 2012
    I am trying to generate costom build numbers for our builds in TSF 2010, and I tried the steps in  your posts based on my needs. (www.ewaldhofman.nl/.../Customize-Team-Build-2010-e28093-Part-4-Create-your-own-activity.aspx. created the code activity wich updates the build number and included that in the branched default template. once I checked in the editd build process template and try to open it, I get the below error:


Error 1 Could not find type 'ActivityPack.UpdateVersion' in assembly ''. C:TFS_POCDADEVBuildProcessTemplatesDefaultTemplate-RF.xaml 51 10

I have check in the activityPack.dll  in the C:UsersrfernandoDocumentsvisual studio 2010ProjectsActivityPackbinRelease folder to $/DADEV/BuildProcessTemplates/CustomActivities in TFS and reference that in build controller properties. I still did not run any builds using this new template since I see this error when opening the template. Please let me know if you have any suggestions. thank you in advance, Rishan.