How to Move TFS 2010 Build Definition between Projects?
Asked Answered
L

6

11

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?

Leninist answered 21/2, 2011 at 16:59 Comment(0)
I
13

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.

Imalda answered 21/2, 2011 at 21:51 Comment(0)
I
7

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();                
}
Iolanthe answered 2/8, 2012 at 9:13 Comment(0)
A
5

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

Arman answered 17/1, 2014 at 17:11 Comment(0)
C
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.

Carrell answered 14/8, 2012 at 9:21 Comment(2)
That's great - it moved the build definition, and so retained all the build history, etc. :)Socorrosocotra
This worked for me. Just had to update the source control folder and the project location once it was moved over.Unreadable
H
1

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.

Hattiehatton answered 21/2, 2011 at 18:23 Comment(2)
In VS 2010, that doesn't seem to be the case, or - if it is - I do not know where to look. It is true that some old TFS 2005 build types are hanging around in TFS; however, the newest build definitions built in 2010 are nowhere to be found in the ProjectX folder or its chidren.Leninist
Ah, a bit of confusion between TFS projects and VisualStudio projects I think. In TFS2010 the build definitons are workflows in (and this is from memory; I'm at home and don't have TFS here) the $builddefintions folder so all build definitions for the TFS build servers for all VS 2010 projects live in this TFS project folder.Trici
C
1

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();
}
Contravallation answered 6/9, 2013 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.