API for automating Azure DevOps Pipelines?

前端 未结 2 785
孤街浪徒
孤街浪徒 2021-01-02 04:28

I want to automate the queue-ing of Azure Pipelines with an API call, get information on the pipeline/build/job status,

  1. Azure Pipelines docs only mention \"

相关标签:
2条回答
  • 2021-01-02 04:35

    Seems I was bad at googling:

    Trigger Azure Pipelines build via API and Start a build and passing variables through VSTS Rest API (found via the searching for [azure-pipelines] apihere on StackOverflow) point me to the Azure DevOps Services REST API that I had mentioned above.

    0 讨论(0)
  • 2021-01-02 04:53

    I too have been working on automating DevOps pipelines and keep winding up back here. Some of this information appears to be outdated. As of the time of my writing this, I believe this article in the Microsoft Docs is the most recent. I did have to scratch my head a bit to make it work, but wound up with this code

    public static async Task InitiatePipeline(CancellationToken cancellationToken = default)
    {
        using(HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var token = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", AppSettings.DevOpsPAT)));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token);
    
    var repoGuid = "Put GUID Here"; // You can get GUID for repo from the URL when you select the rpo of interest under  Repos is Project Settings
    var bodyJson = @"{
        ""parameters"": {
            ""parameterName"": ""parameterValue""
        },
        ""variables"": {},
        ""resources"": {
            ""repositories"": {
                ""self"": {
                    ""repository"": {
                        ""id"": """ + repoGuid + @""",
                        ""type"": ""azureReposGit""
                    },
                    ""refName"": ""refs/heads/master""
                }
            }
        }
    }";
    
            var bodyContent = new StringContent(bodyJson, Encoding.UTF8, "application/json");
            var pipeLineId = 61; // Can get this from URL when you open the pipeline of interest in Azure DevOps
            var response = await client.PostAsync($"https://dev.azure.com/ORG_NAME/PROJECT_NAME/_apis/pipelines/{pipeLineId}/runs?api-version=6.0-preview.1", bodyContent, cancellationToken);
            response.EnsureSuccessStatusCode();
        }
    }
    
    0 讨论(0)
提交回复
热议问题