How to Move TFS 2010 Build Definition between Projects?

拟墨画扇 提交于 2019-12-05 00:57:43

I don't think there is something out of the box to copy the build definitions from one project to another. However you should be able to do it using the TFS API. You will want to move the build process templates, which is what Pete is referring to, into the Build Process Template folder for the new project. After that you would do something like:

var server = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("<server uri>"));

IBuildServer buildServer = server.GetService<IBuildServer>();
var buildDetails = buildServer.QueryBuildDefinitions("Project X");
foreach(var build in buildDetails)
{
        var buildDefinition = buildServer.CreateBuildDefinition("Project Y");
        buildDefinition.Name = "Copy of " + build.Name;
        buildDefinition.BuildController = build.BuildController;
        // This finds the template to use
        buildDefinition.Process = buildServer.QueryProcessTemplates("Project Y")[0]; 
        buildDefinition.ProcessParameters = build.ProcessParameters;
        buildDefinition.Save();                
}

A couple of things to note. You will need deal with converting the workspace mappings from one project to the other. You will also need to change the buildDefinition.Process line to find the specific template.

A powershell version of Sean's answer above

# Copy some TFS build defintions from one project collection to another

[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")

$tfsUrl = "http://lontfs_at:8080/tfs/XXX"
$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($tfsUrl)
$vcs = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$buildServer = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])

$buildDetails = $buildServer.QueryBuildDefinitions("Project X");
foreach( $build in $buildDetails)
{
        $buildDefinition = $buildServer.CreateBuildDefinition("Project Y");
        $buildDefinition.Name = "Copy of " + $build.Name;
        $buildDefinition.BuildController = $build.BuildController;
        # This finds the template to use
        $buildDefinition.Process = $buildServer.QueryProcessTemplates("Project Y")[0]; 
        $buildDefinition.ProcessParameters = $build.ProcessParameters;
        $buildDefinition.Save();                
}
ckkkitty

In VS2010, the TFS Power Tools can move a build definition from one project to another as demonstrated in the 2nd answer in this link: Is it possible to Export TFS 2010 Build Definitions?, and as shown below.

c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>tfpt 
builddefinition /collection:"http://ServerX:8080/tfs/Collection X" /clone "Project 1\Build 
Definition X" "Project 2\Copy of Build Definition X"

The TFS Power Tools for VS2010 can be downloaded from: http://visualstudiogallery.msdn.microsoft.com/c255a1e4-04ba-4f68-8f4e-cd473d6b971f

Uggly but very efficient way to move (not to duplicate) only one or two Build Definitions:

  • Open SQL Server Management Studio,
  • Open your Collection DB
  • Edit the table tbl_BuildDefinition
  • Replace the current GroupId with the target Team Project's GroupId

That's it ;)

To determine the GroupId of the target Team Project, simply find any BuildDefinition of that Team Project in tbl_BuildDefinition and take its GroupId.

For sure, you have next to update the BuildDefinition's workspace, Items to build, ... to use the server path of the new Team Project !

If you get an error like "GroupItem cannot be move accross Team Project" when updating your BuildDefinition, it was most probably already open before updating the DB. Close and reopen it.

If you don't intend to repeat this operation too often, it's IMO the fastest solution.

V.

Build definitions are just another source controled file in TFS, you should be able to open the build definition in ProjectX and save it as a new file to projectY's build definitions folder.

Edit
In the above post I am assuming ProjectX and ProjectY are TFS projects, in which case their workflow build definition(s) are simply in the builddfinitions folder of their respective source control roots.

Sean's answer helped me out, but in my case, I have only one repository for my build templates and custom activities assemblies. So I don't need to edit settings, I just want to copy one build definition from TFS Project A to TFS Project B. Here is my 'short' version of Sean's code:

var server = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("TFS URL"));
IBuildServer buildServer = server.GetService<IBuildServer>();
var buildDetails = buildServer.QueryBuildDefinitions("Proj_A");
foreach(var build in buildDetails)
{
    var buildDefinition = buildServer.CreateBuildDefinition("Proj_B");
    buildDefinition.CopyFrom(build);
    buildDefinition.Save();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!