3
votes

I'm using BuildHttpClient's .GetDefinitionAsync and .CreateDefinitionAsync to clone a VSTS Build Definition. This works fine but I'd like to create the build defs in a different folder, other than the root folder of the project. I can add a folders via the "Manage Folders" link web but how can I do this programatically?

I've tried the following:

    buildDef = JsonConvert.DeserializeObject<BuildDefinition>(json);
    buildDef.BuildNumberFormat = buildNumFormat;
    buildDef.Name = newBuildDefName;

    Uri tfsURI = new Uri(CollectionReleaseURL);
    buildDef.Url = tfsURI.ToString();
    await buildClient.CreateDefinitionAsync(buildDef, project);

Where the Collection release is the path: CollectionReleaseURL = @"http://my.tfs.server8080/tfs/DefaultCollection/Project/Product/Release";

but when I run the program it just puts the new build def in the following folder: @"http://my.tfs.server8080/tfs/DefaultCollection/Project"

How can i clone the build def to the ../Product/Release folder?

UPDATE: I'm using the following to Clone the build def but it is NOT copying the Build Steps! Anyone know why that is?

private static async Task CloneBuildDefAsync(int buildDefId, string newBuildDefName, string buildNumFormat, string sourceControlPath, string URLpath)
{
    var buildClient = createClient();
    var buildDef = (await buildClient.GetDefinitionAsync(project, buildDefId)) as BuildDefinition;

    buildDef.Project = null;

    var json = JsonConvert.SerializeObject(buildDef);
    json = json.Replace(sourceControlMainline, sourceControlPath);
    buildDef = JsonConvert.DeserializeObject<BuildDefinition>(json);

    buildDef.BuildNumberFormat = buildNumFormat;
    buildDef.Name = newBuildDefName;
    buildDef.Path = URLpath;

    await buildClient.CreateDefinitionAsync(buildDef, project);
}

Seems to copy everything but the build step, everything else is cloned and updated perfectly: enter image description here

3

3 Answers

5
votes

You want to set the Path property to the folder you want.

An example of the clone in PowerShell:

$uri = 'https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}'

$result = Invoke-RestMethod -Method Get -Uri $uri -UseDefaultCredentials
$result.path = '\NewFolder\Location'
$result.name = "Testing"

$body = $result | ConvertTo-Json -Depth 7

Invoke-RestMethod -Method POST -uri 'https://dev.azure.com/{organization}/{project}/_apis/build/definitions?api-version=4.0' -UseDefaultCredentials -Body $body -ContentType 'application/json'

Creates a build folder structure like this:

enter image description here

4
votes

If you use Microsoft.TeamFoundationServer.Client you may just set Path:

BuildDefinition cpBuild = BuildClient.GetDefinitionAsync(teamProjectName, 8).Result;

BuildDefinition build = new BuildDefinition();
build.Path = "New Build Folder";
build.Name = "new Build";
build.BuildNumberFormat = cpBuild.BuildNumberFormat;
build.Repository = cpBuild.Repository;
build.Process = cpBuild.Process;
var newBuild = BuildClient.CreateDefinitionAsync(build, teamProjectName).Result;
1
votes

This is what I used to accomplish this using:

https://gist.github.com/HockeyJustin/c1cb4e543806c16cab6e2c2322f5d830

$thisBuildDef.Name = $Clone_Name
$thisBuildDef.path = $BuildDefURL  # Relative to the Project name; like "Release/2019"
$thisBuildDef.buildNumberFormat = $BuildNumberFormat

# Update source control path to new branch
$defAsJson = $thisBuildDef | ConvertTo-Json -Depth 100
$defAsJson = $defAsJson.Replace($sourceControlMainline, $sourceControlBranch)

$Uri = "$(TFSCollection)/$(TFSProject)/_apis/build/definitions?api-version=2.0"

$newBuildDef = Invoke-RestMethod -Uri $Uri -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -Body $defAsJson -ContentType "application/json" -ErrorAction Stop