2

I want to trigger a YAML release pipeline via C# code. I am able to achieve this using the below REST API from Postman. However, Azure DevOps provides its own SDK for such purposes. How can I trigger a YAML release pipeline using the .NET SDK instead of calling the plain API from C#?

I need C#/.NET alternative to:

POST https://dev.azure.com/Dev/modernization/_apis/pipelines/30/runs?api-version=7.2-preview.1

BODY:

{
    "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"
    }
}
3
  • 1
    Have you tried the .NET client libraries for Azure DevOps? Commented May 6, 2024 at 17:46
  • Yeah, I've really tried to find the appropriate method to run the pipeline that supports passing resources, parameters, and stagesToSkip, similar to how it's done in the above REST API. Tbh, I can't seem to find it. I'm able to find the BuildHttpClient.QueueBuildAsync Method, but I can't see any Resources property where I can provide the artifact pipeline build number Commented May 6, 2024 at 18:52
  • BuildHttpClient.QueueBuildAsync Method Commented May 6, 2024 at 18:53

1 Answer 1

4

Below is a sample of C# code to run pipeline using .NET client libraries. You can reference it to write your code.

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());
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I'll try it.. How did you get it? :) I'm finding it difficult to locate this class in the SDK documentation...
This worked!! Could you please share link to documentation about this method?
@osim_ans search for PipelinesHttpClient
Yep, tried already, It looks like there is no documentation for this method specifically :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.