【问题标题】:API for automating Azure DevOps Pipelines?用于自动化 Azure DevOps Pipelines 的 API?
【发布时间】:2021-05-19 06:37:47
【问题描述】:

我想通过 API 调用自动排队 Azure Pipelines,获取有关管道/构建/作业状态的信息,

  1. Azure Pipelines 文档仅提及“调用 HTTP Rest API”任务的“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 但是我在那里找不到任何提及“管道”的内容,所以这似乎也不是正确的事情。

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

    Visual Studio Team Services REST API 是一组 API,允许管理 Visual Studio Team Services 帐户以及 TFS 2015 和 2017 服务器。

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

我是不是用了错误的词来搜索?


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

【问题讨论】:

标签: azure-devops azure-pipelines azure-devops-rest-api


【解决方案1】:

似乎我不擅长谷歌搜索:

Trigger Azure Pipelines build via APIStart a build and passing variables through VSTS Rest API(通过在 StackOverflow 上搜索 [azure-pipelines] apihere 找到)指向我上面提到的 Azure DevOps Services REST API。

【讨论】:

【解决方案2】:

我也一直致力于实现 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,它应该是“templateParameters”,而不是“parameters”。至少对我来说,它不适用于“参数”。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 2020-05-28
    相关资源
    最近更新 更多