在C#中克隆VSTS构建定义

问题描述 投票:1回答:3

我正在使用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);
}

似乎复制除了构建步骤之外的所有内容,其他一切都被克隆并完美更新:enter image description here

c# tfs build azure-devops tfs-sdk
3个回答
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
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.