在C#中克隆VSTS构建定义

时间:2019-02-04 21:03:39

标签: c# tfs build azure-devops tfs-sdk

我正在使用BuildHttpClient的.GetDefinitionAsync和.CreateDefinitionAsync来克隆VSTS构建定义。这可以正常工作,但我想在项目的根文件夹以外的其他文件夹中创建构建定义。我可以通过“管理文件夹”链接网站添加文件夹,但是如何以编程方式做到这一点?

我尝试了以下操作:

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

Collection版本是路径: CollectionReleaseURL = @“ http://my.tfs.server8080/tfs/DefaultCollection/Project/Product/Release”;

但是当我运行程序时,它只是将新的构建定义放在以下文件夹中: @“ http://my.tfs.server8080/tfs/DefaultCollection/Project

如何将构建定义克隆到../Product/Release文件夹?

更新:我正在使用以下内容克隆构建定义,但它没有复制构建步骤!有人知道为什么吗?

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

似乎可以复制除构建步骤以外的所有内容,其他所有内容均可以克隆并完美更新: enter image description here

3 个答案:

答案 0 :(得分:2)

如果使用Microsoft.TeamFoundationServer.Client,则只需设置路径:

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 :(得分:1)

您要将Path property设置为所需的文件夹。

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'

创建一个构建文件夹结构,如下所示:

enter image description here

答案 2 :(得分:0)

这是我用来完成此任务的方法:

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