TFS 2012 Backup and Restore BuildDefinitions only

自闭症网瘾萝莉.ら 提交于 2019-12-08 07:19:09

问题


I installed a TFS2012 as a test system and doing some tests before we go productive. This includes to define many BuildDefinitions which was a lot of work.

After the tests are successful, an new server will be installed with TFS2012 on it.

For this new server - which operates then as the productive system - i would like to restore the BuildDefinitions from the test system. But only the BuildDefinitions, not the whole TeamCollections. Because i ran test checkins and i don`t want these on my productive server.

Now, is it possible to backup and restore BuildDefinitions only?

Maybe it is possible directly throught the Sql database?, but i`am a little affraid of references there, pointing on some other tables.

Best Regards, Peter Bucher


回答1:


Build definitions are not source controlled. The only option is relying on the TFS database backup where can restore or view the tbl_BuildDefinition* tables in the Tfs_DefaultCollection database.

There is a user voice for this feature and also you can use TFS API to do it.

Add a vote on uservoice:

provide a way to version-control build definitions

Using TFS API

How can I copy a TFS 2010 Build Definition?




回答2:


Finally i decided not to touch the database, because there are references to a lot of other tables.

I used the TFS API v11 (TFS2012) and a bit C# Code, which i fitted to my needs from this base: How can I copy a TFS 2010 Build Definition?

It copies all Build Definitions from one TFS2012 Server to another. For both servers there is the need to specifiy a TeamCollection and a TeamProject.

So, the copy-task has to be done per TeamProject.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TFSBuildDefinitionCreator
{

internal class Program
{
    private static void Main(string[] args)
    {
        // Copies build definitions from one server to another.
        // Uses the TeamFoundation API V11 (TFS2012).
        // Code was used to copy b uild definitions from a test server to a productive.

        string sourceServer = "http://testTfs:8080/tfs/MyTeamCollection";
        string sourceTeamProject = "MyTeamProject";

        string targetServer = "https://productiveTfs:8080/tfs/MyTeamCollection";
        string targetTeamProject = "MyTeamProject";

        // DropLocation for defininitions: Share on which the build should be dropped.
        string defaultDropLocation = "\\\\MyBuildserver\\Builds$";

        // Change the DefaultProcessTemplate in the following method below:         GetDefaultProcessTemplateByServerPathFromBuildServer.

        CopyBuildDefinitions(sourceServer, sourceTeamProject, targetServer, targetTeamProject, defaultDropLocation);

        Console.Read();
    }

    private static IBuildServer GetBuildServerFromServerUrl(string serverUrl)
    {
        var tfs = TeamFoundationServerFactory.GetServer(serverUrl);
        return (IBuildServer)tfs.GetService(typeof(IBuildServer));
    }

    private static IBuildController GetDefaultBuildControllerFromBuildServer(IBuildServer buildServer)
    {
        return buildServer.QueryBuildControllers()[0];
    }

    private static IProcessTemplate GetDefaultProcessTemplateByServerPathFromBuildServer(IBuildServer buildServer, string teamProject)
    {
        var processTemplates = buildServer.QueryProcessTemplates(teamProject);
        var result = processTemplates.First(t => t.ServerPath.Contains("/BuildProcessTemplates/MyDefaultTemplate.xaml"));

        return result;
    }

    private static void CopyBuildDefinitions(string sourceServer, string sourceTeamProject, string targetServer,
                                             string targetTeamProject, string defaultDropLocation)
    {
        var sourceBuildServer = GetBuildServerFromServerUrl(sourceServer);
        var sourceBuildDetails = sourceBuildServer.QueryBuildDefinitions(sourceTeamProject);

        foreach (var sourceBuildDetail in sourceBuildDetails)
        {
            CopyBuildDefinition(sourceBuildDetail, targetServer, targetTeamProject, defaultDropLocation);
        }
    }

    private static void CopyBuildDefinition(IBuildDefinition buildDefinition, string targetServer, string targetTeamProject, string defaultDropLocation)
    {
        var targetBuildServer = GetBuildServerFromServerUrl(targetServer);

        var buildDefinitionClone = targetBuildServer.CreateBuildDefinition(targetTeamProject);

        buildDefinitionClone.BuildController = GetDefaultBuildControllerFromBuildServer(targetBuildServer);
        buildDefinitionClone.ContinuousIntegrationType = buildDefinition.ContinuousIntegrationType;
        buildDefinitionClone.ContinuousIntegrationQuietPeriod = buildDefinition.ContinuousIntegrationQuietPeriod;

        // Noch ändern.
        //buildDefinitionClone.DefaultDropLocation = buildDefinition.DefaultDropLocation;
        buildDefinitionClone.DefaultDropLocation = defaultDropLocation;

        buildDefinitionClone.Description = buildDefinition.Description;
        buildDefinitionClone.Enabled = buildDefinition.Enabled;

        //buildDefinitionClone.Name = String.Format("Copy of {0}", buildDefinition.Name);
        buildDefinitionClone.Name = buildDefinition.Name;

        //buildDefinitionClone.Process = buildDefinition.Process;
        buildDefinitionClone.Process = GetDefaultProcessTemplateByServerPathFromBuildServer(targetBuildServer, targetTeamProject);

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

        buildDefinitionClone.Save();
    }
}

}

Hope that helps others.



来源:https://stackoverflow.com/questions/14601766/tfs-2012-backup-and-restore-builddefinitions-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!