【问题标题】:How to run an Azure DevOps pipeline from code?如何从代码运行 Azure DevOps 管道?
【发布时间】:2020-08-26 22:38:53
【问题描述】:

我一直在使用 Azure DevOps 版本定义并从一些 C# 代码触发它:

var releaseClient = VssConnection.GetClient<ReleaseHttpClient>();

return await releaseClient.CreateReleaseAsync(
    _releaseDefinitionId,
    $"Release run for {build.BuildNumber}",
    build.Definition.Name,
    build.Id,
    new Dictionary<string, string> {
        { "InputVariable1", _value1 },
        { "InputVariable2", _value2 },
        { "DatabaseConnectionString", "xxxxxxx" }
    });

我现在已经将该发布定义转换为纯 YAML 管道 - 有什么方法可以从 C# 触发该管道的运行?

谢谢!

【问题讨论】:

  • Yaml 管道转换为构建。所以我认为你需要排队一个新的构建。

标签: c# azure-devops yaml azure-pipelines


【解决方案1】:

您可以通过这种方式触发 YAML 构建:

var credential = new VssBasicCredential(string.Empty, "PAT");
var connection = new VssConnection(new Uri("https://dev.azure.com/thecodemanual/"), credential);
var buildClient = connection.GetClient<BuildHttpClient>();

var projectClient = connection.GetClient<ProjectHttpClient>();
var project = projectClient.GetProject("DevOps Manual").Result;
Console.WriteLine(project.Name);

var definition = buildClient.GetDefinitionAsync("DevOps Manual", 48).Result;

Console.WriteLine(definition.Name);

var build = new Build()
{
    Definition = definition,
    Project = project
};
string _value1 = "someStr1";
string _value2 = "someStr2";
var dict = new Dictionary<string, string> { { "InputVariable1", _value1 }, { "InputVariable2", _value2 }, { "DatabaseConnectionString", "xxxxx" } };
build.Parameters = JsonConvert.SerializeObject(dict);

buildClient.QueueBuildAsync(build).Wait();

【讨论】:

  • 参数不会传递到 YAML 管道。我得到 Could not queue the build 因为有验证错误或警告。必须提供“InputVariable1”参数的值。知道为什么吗?
  • 如果没有信息,您的管道看起来将很难为您提供帮助。请创建一个新问题,与我们分享您的管道,以便我们能够为您提供帮助。
  • 我创建了一个新问题stackoverflow.com/questions/63933985/…
  • Krzysztof Madej 的帮助将不胜感激
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-15
  • 1970-01-01
  • 2020-04-02
  • 2020-02-23
相关资源
最近更新 更多