仅TFS 2012备份和还原BuildDefinitions [英] TFS 2012 Backup and Restore BuildDefinitions only

查看:85
本文介绍了仅TFS 2012备份和还原BuildDefinitions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了TFS2012作为测试系统,并在进行生产之前进行了一些测试. 这包括定义许多BuildDefinitions,这是很多工作.

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.

测试成功后,将安装新服务器,上面装有TFS2012.

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

对于这台新服务器-该服务器随后将作为生产系统运行-我想从测试系统中还原BuildDefinitions.但是只有BuildDefinitions,而不是整个TeamCollections.因为我运行了测试签到,所以我不希望这些在我的生产服务器上出现.

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.

现在,是否可以仅备份和还原BuildDefinitions?

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

也许可以直接通过Sql数据库吗?但是我对那里的引用有些偏爱,指向其他一些表.

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

最诚挚的问候,彼得·布彻(Peter Bucher)

Best Regards, Peter Bucher

推荐答案

最后,我决定不使用数据库,因为其中引用了许多其他表.

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

我使用了TFS API v11(TFS2012)和一些C#代码,我从以下基础上满足了我的需求:

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?

它将所有内部版本定义从一台TFS2012服务器复制到另一台.对于这两个服务器,都需要指定一个TeamCollection和一个TeamProject.

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.

因此,必须根据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();
    }
}

}

希望对别人有帮助.

这篇关于仅TFS 2012备份和还原BuildDefinitions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆