我想通过 C# 代码触发 YAML 发布管道。我可以使用 Postman 的以下 REST API 来实现此目的。但是,Azure DevOps 为此提供了自己的 SDK。如何使用 .NET SDK 触发 YAML 发布管道,而不是从 C# 调用普通 API?
我需要 C#/.NET 替代品:
POST https://dev.azure.com/Dev/modernization/_apis/pipelines/30/runs?api-version=7.2-preview.1
身体:
{
"previewRun": false,
"resources": {
"repositories": {
"self": {
"repository": {
"id": "PartsUnlimited",
"type": "azureReposGit"
},
"refName": "refs/heads/feature/branch4",
"version": "aaaaabbbbbbe6a1b2d7904b110zzzzzaaa400000"
}
},
"pipelines": {
"source_build" : {
"pipeline": {
"name": "tfvc-build"
},
"version": "4972"
}
}
},
"stagesToSkip": [
"Stage_Test1"
],
"templateParameters" : {
"number_A" : "2",
"number_B" : "4"
}
}
下面是使用 .NET 客户端库运行管道的 C# 代码示例。您可以参考它来编写代码。
using Microsoft.Azure.Pipelines.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
. . .
public static void runPipeline(string orgName, string projName, int pipID, string pat)
{
try {
var orgUrl = new Uri($"https://dev.azure.com/{orgName}");
VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, pat));
RepositoryResourceParameters repoResParam = new RepositoryResourceParameters();
repoResParam.Version = "xxxxxx";
repoResParam.RefName = "refs/heads/xxxx";
PipelineResourceParameters pipeResParam = new PipelineResourceParameters();
pipeResParam.Version = "xxxx";
RunPipelineParameters runParam = new RunPipelineParameters();
runParam.PreviewRun = false;
runParam.StagesToSkip.Add("Stage_Test1");
runParam.TemplateParameters.Add("number_A", "2");
runParam.TemplateParameters.Add("number_B", "4");
runParam.Resources.Repositories.Add("self", repoResParam);
runParam.Resources.Pipelines.Add("source_build", pipeResParam);
PipelinesHttpClient pipClient = connection.GetClient<PipelinesHttpClient>();
var result = pipClient.RunPipelineAsync(runParam, projName, pipID).Result;
var runNum = result.Name;
var runID = result.Id.ToString();
Console.Write($"Successfully run pipeline! \r\n Run Name: {runNum} \r\n Run ID: {runID}");
}
catch(Exception ex) {
Console.Write(ex.ToString());
}
}