How to Move TFS 2010 Build Definition between Proj

2020-08-14 05:04发布

问题:

I have some TFS 2010 build definitions that were created under ProjectX. Now the source code has moved to a folder subordinate to ProjectY. How can I move the build definitions to ProjectY so they display under the Builds node of the Team Explorer for ProjectY?

回答1:

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.



回答2:

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


回答3:

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



回答4:

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.



回答5:

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.



回答6:

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