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.