簡體   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