Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run an Azure DevOps pipeline from code?

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" }
    });

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#?

Thanks!

like image 252
user833115xxx Avatar asked Jan 17 '26 21:01

user833115xxx


1 Answers

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();
like image 75
Krzysztof Madej Avatar answered Jan 20 '26 12:01

Krzysztof Madej