在 C# 中克隆 VSTS 生成定义
Clone VSTS Build Definition in C#
我正在使用 BuildHttpClient 的 .GetDefinitionAsync 和 .CreateDefinitionAsync 来克隆 VSTS 构建定义。这工作正常,但我想在项目的根文件夹以外的其他文件夹中创建构建定义。我可以通过 "Manage Folders" link 网络添加文件夹,但我如何以编程方式执行此操作?
我试过以下方法:
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);
合集发布路径为:
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);
}
似乎复制了除构建步骤以外的所有内容,其他所有内容均已完美克隆和更新:
您想将 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'
创建这样的构建文件夹结构:
如果您使用 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;
这是我用来完成此操作的方法:
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
我正在使用 BuildHttpClient 的 .GetDefinitionAsync 和 .CreateDefinitionAsync 来克隆 VSTS 构建定义。这工作正常,但我想在项目的根文件夹以外的其他文件夹中创建构建定义。我可以通过 "Manage Folders" link 网络添加文件夹,但我如何以编程方式执行此操作?
我试过以下方法:
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);
合集发布路径为: 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);
}
似乎复制了除构建步骤以外的所有内容,其他所有内容均已完美克隆和更新:
您想将 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'
创建这样的构建文件夹结构:
如果您使用 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;
这是我用来完成此操作的方法:
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