Team Build–Enhancing the Build Deploy Test Workflow

4/25/2011

Microsoft provided an unofficial unsupported xaml to enable automation of the build-deploy-test cycle for physical machines.  This mimics the functionality provided by Lab Manager for Hyper-V virtual machines.  I used this in a previous post to Integrate with FitNesse.

One of the limitations of this build is picking the latest successful build given a certain build definition.  The xaml file supports queuing a new build or picking the build results  from a share, but not picking the latest build, given a build definition.

Well I decided that I wanted to use the latest build from a given definition so I have updated the xaml to do that for us.  Here is how I went about it.

The Approach

We will create a custom code activity that retrieves the Last Good Build of a passed in Build Definition name.

We can then update the BuildDeployTest_Physical.xaml to use our new activity.  We can pass this build into the rest of the workflow.

Getting Started

Make a copy / branch of the BuildDeployTest_Physical.xaml file.

Create a new build definition that uses your new xaml file.

Create a new C# Project and include your xaml file as a link.  Add the references needed in order to get the project to compile.

Create a new C# Class Library – this will be our custom code activity.  My code for the class looks like this:-

[BuildActivity(HostEnvironmentOption.All)]
   public sealed class RetrieveLastGoodBuildNumber : CodeActivity<string>
   {
       private IBuildServer buildServer;

       public InArgument<IBuildDetail> CurrentBuild { get; set; }
       public InArgument<string> BuildDefinitionName { get; set; }
       public OutArgument<string> BuildDropLocation { get; set; }

       protected override string Execute(CodeActivityContext context)
       {
           buildServer = this.CurrentBuild.Get(context).BuildServer;
           string projectName = this.CurrentBuild.Get(context).TeamProject;
           string BuildDefName = this.BuildDefinitionName.Get(context);
           IBuildDefinition def = buildServer.GetBuildDefinition(projectName,BuildDefName);
           IBuildDetail buildDetail = buildServer.GetBuild(def.LastGoodBuildUri);
           this.BuildDropLocation.Set(context,buildDetail.DropLocation);
           return buildDetail.BuildNumber;
       }
   }

Note the BuildActivity attribute at the top of the class.  This ensures that this gets loaded as part of the build.  I have used two arguments that get passed in.

CurrentBuild is a reference to the IBuildDetail of the currently executing build.  This enables us to work out the current Project.

BuildDefinitionName is the Build that we want to find that last good build to deploy and test against.

There is one out paramater:-

BuildDropLocation is an out parameter that gives us the drop location of our last good build.

The return string value is the Build Number of the last good build that matches the build definition passed in.

Putting it all Together

Open your xaml file, go to the Build section and insert an IF shape where you see the red arrow below.

image

We will add a new argument called GetLatestGoodBuild (bool) to the build workflow and metadata

 image

This will be the trigger to use our new code activity.   (If both DoBuild is True and this parameter is True then it will use the last Good build of the Build Definition Name parameter.

We set the IF Condition to be GetLatestGoodBuild = False.  Then we can add the following:-

image

The Then branch does the Build as in the original xaml.  The Else branch implements our new behaviour.  Add a sequence called Find Latest Good Build and then drag our new custom code activity (which should be already in the toolbox.)  Set the parameters like this:-

image

Finally I have added a WriteBuildMessage shape to output the build number that we have found.  NB I have set the Importance to Microsoft.TeamFoundation.Build.Client.BuildMessageImportance.High so that it always displays in the log file.

Deployment

Check in the xaml file and deploy the custom code activity.  More info on how to do that here.  Check out the “Deploying the Activity” Section.

The Results

Fill in the parameters (make sure to set both the Queue New Build and Get Latest Good Build to True) and queue a new build.  My result looks like:-

image

Here is the log file

image

Success, by only specifying a build definition we have found the latest good build, deployed the application from the build drop folder and run our tests (with the test run being associated with the correct build)

As usual all code from here.

4/25/2011

No comments :

Post a Comment