如何从代码运行 Azure DevOps 管道? [英] How to run an Azure DevOps pipeline from code?

查看:42
本文介绍了如何从代码运行 Azure DevOps 管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been working with an Azure DevOps release definition and triggering it from some C# code:

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# 触发该管道的运行?

I've now converted that release definition to a pure YAML pipeline - is there any way to trigger the running of that pipeline from C#?

谢谢!

推荐答案

你可以这样触发 YAML 构建:

You can trigger YAML build in this way:

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();

这篇关于如何从代码运行 Azure DevOps 管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆