Question

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?

Was it helpful?

Solution

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;
}

OTHER TIPS

You can download the new TFS 2010 power tools. It has the option to clone a build definition.

See http://msmvps.com/blogs/molausson/archive/2010/10/21/clone-a-build-definition.aspx for an example

Note: Be aware that the Clone only works when you did NOT pop out the Build window.

The following tool (VS Addin) will satisfy your requirement>

Community TFS Build Manager

http://visualstudiogallery.msdn.microsoft.com/16bafc63-0f20-4cc3-8b67-4e25d150102c

I just had a need to copy build definitions, and found Jim's answer above to be helpful. However, being new to the TFS API, I needed help connecting to the server and getting the existing build definition through code. These two links helped fill in the gaps:

http://msdn.microsoft.com/en-us/library/bb286958.aspx

http://geekswithblogs.net/jakob/archive/2010/04/26/creating-a-build-definition-using-the-tfs-2010-api.aspx

You can right click the build definition and select 'clone build definition' to copy the definition file. You can then edit it from there.

Here is the soltion if you want to move the Build definition from one Team Project to other Team project.

    public void MoveBuild(string fromTeamProject, string toTeamProject, string buildName, string newBuildName)
    {

        var _server = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new

        Uri("http://Mytfs:8080/defaultcollection"));

        IBuildServer _buildServer = _server.GetService<IBuildServer>();

        var buildDetails = _buildServer.QueryBuildDefinitions(fromTeamProject);

        foreach (var fromBuild in buildDetails)
        {
            if (fromBuild.Name != buildName) continue;
            var newBuildDefinition = _buildServer.CreateBuildDefinition(toTeamProject);
            newBuildDefinition.Name = !string.IsNullOrEmpty(newBuildName) ? newBuildName : fromBuild.Name;

            newBuildDefinition.BuildController = fromBuild.BuildController;

            // This finds the template to use 
            foreach (var mapping in fromBuild.Workspace.Mappings)
            {
                newBuildDefinition.Workspace.AddMapping(
                    mapping.ServerItem, mapping.LocalItem, mapping.MappingType, mapping.Depth);
            }
            newBuildDefinition.DefaultDropLocation = fromBuild.DefaultDropLocation;
            newBuildDefinition.Description = fromBuild.Description;
            // buildDefinition.Workspace.AddMapping(build.Workspace.);
            newBuildDefinition.Process = _buildServer.QueryProcessTemplates(fromBuild)[2];

            newBuildDefinition.ProcessParameters = fromBuild.ProcessParameters;
            newBuildDefinition.Enabled = false;
            newBuildDefinition.Save();
        }//end of for each loop 

    }

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top