繁体   English   中英

在C#中克隆VSTS构建定义

[英]Clone VSTS Build Definition in C#

我正在使用BuildHttpClient的.GetDefinitionAsync和.CreateDefinitionAsync来克隆VSTS构建定义。 这工作正常,但我想在不同的文件夹中创建构建defs,而不是项目的根文件夹。 我可以通过“管理文件夹”链接网站添加文件夹但是我该如何以编程方式执行此操作?

我尝试过以下方法:

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

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

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

更新:我正在使用以下内容克隆构建def,但它不是复制构建步骤! 谁知道为什么会这样?

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

似乎复制除了构建步骤之外的所有内容,其他所有内容都被克隆并完美更新: 在此输入图像描述

如果您使用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;

您想要将Path属性设置为所需的文件夹。

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'

创建一个这样的构建文件夹结构:

在此输入图像描述

这是我过去使用以下方法完成此任务:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM