views:

454

answers:

4

Is there any way to copy a build definition? I work in a mainline source control methodology which utilizes many different branches that live for very short periods (ie. a few days to a week). I'd really like to copy a build template and just change the solution to build. Is there any way to do this?

A: 

From your message it is not clear which template is your build definition using (default, upgrade or lab management). If I understand correctly you would like to easily set up a build definition which builds the same solution but from a different branch.

One thing that you could try instead of copying the definition is to edit it. When the branch dies, rename the build definition (might help with reporting), change the workspace mapping of the build and you should be done.

Thanks, Ladislau

Ladislau Szomoru
+3  A: 

You can write an add-in to do it. Here's the code to copy an existing build definition:

static IBuildDefinition CloneBuildDefinition(IBuildDefinition buildDefinition)
{
    var buildDefinitionClone = buildDefinition.BuildServer.CreateBuildDefinition(
        buildDefinition.TeamProject);

    buildDefinitionClone.BuildController = buildDefinition.BuildController;
    buildDefinitionClone.ContinuousIntegrationType = buildDefinition.ContinuousIntegrationType;
    buildDefinitionClone.ContinuousIntegrationQuietPeriod = buildDefinition.ContinuousIntegrationQuietPeriod;
    buildDefinitionClone.DefaultDropLocation = buildDefinition.DefaultDropLocation;
    buildDefinitionClone.Description = buildDefinition.Description;
    buildDefinitionClone.Enabled = buildDefinition.Enabled;
    buildDefinitionClone.Name = String.Format("Copy of {0}", buildDefinition.Name);
    buildDefinitionClone.Process = buildDefinition.Process;
    buildDefinitionClone.ProcessParameters = buildDefinition.ProcessParameters;

    foreach (var schedule in buildDefinition.Schedules)
    {
        var newSchedule = buildDefinitionClone.AddSchedule();
        newSchedule.DaysToBuild = schedule.DaysToBuild;
        newSchedule.StartTime = schedule.StartTime;
        newSchedule.TimeZone = schedule.TimeZone;
    }

    foreach (var mapping in buildDefinition.Workspace.Mappings)
    {
        buildDefinitionClone.Workspace.AddMapping(
            mapping.ServerItem, mapping.LocalItem, mapping.MappingType, mapping.Depth);
    }

    buildDefinitionClone.RetentionPolicyList.Clear();

    foreach (var policy in buildDefinition.RetentionPolicyList)
    {
        buildDefinitionClone.AddRetentionPolicy(
            policy.BuildReason, policy.BuildStatus, policy.NumberToKeep, policy.DeleteOptions);
    }

    return buildDefinitionClone;
}
Jim Lamb