API 用于自动化 Azure DevOps 管道?

API for automating Azure DevOps Pipelines?

我想通过 API 调用自动排队 Azure Pipelines,获取有关 pipeline/build/job 状态的信息,

  1. Azure Pipelines 文档只提到 "API" 用于 "Invoke HTTP Rest API" 任务:https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/http-rest-api?view=vsts 这可能会派上用场,但不是我想要的。

  2. 有一个"Azure DevOps Services REST API": https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-5.1 但我在那里找不到任何关于 "Pipeline" 的提及,所以这似乎也不正确。

    Whosebug 标签 azure-devops-rest-api 也只提到了 VSTS 和 TFS:

    Visual Studio Team Services REST APIs is a set of APIs allowing management of a Visual Studio Team Services accounts as well as TFS 2015 and 2017 servers.

除了这两个结果,我只找到其他版本或这些的各种副本的翻译 - 以及许多与 Azure 相关的一般文档。

我是不是用错了搜索词?


Azure DevOps Pipelines 有实际的 API 吗?
它有可用的 API 资源管理器吗?
它是否有适合 JavaScript、Ruby 或 PHP 等语言的客户端?

看来我不擅长谷歌搜索:

and (通过在 Whosebug 上搜索 [azure-pipelines] api此处找到)指向我上面提到的 Azure DevOps Services REST API。

我也一直致力于自动化 DevOps 管道,并继续回到这里。其中一些信息似乎已过时。在我撰写本文时,我相信 Microsoft Docs 中的 this article 是最新的。我确实需要挠头才能让它工作,但最终得到了这段代码

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

我 运行 也遇到了这些问题,最后制作了 API 的 powershell 包装器,然后将其包装到 Azure DevOps 管道模板中。我刚刚发布它供任何人使用。我希望找到此主题的任何人都能发现 this template 有用。