azure-devopsazure-pipelinesazure-devops-rest-apiazure-pipelines-yamlazure-sdk-.net

Trigger a YAML pipeline using C# Azure DevOps SDK


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

Solution

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