用于自动化Azure DevOps管道的API?

时间:2018-12-11 14:45:03

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

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

  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管道是否有实际的API?
它有可用的API资源管理器吗?
它是否具有适用于JavaScript,Ruby或PHP等语言的客户端?

3 个答案:

答案 0 :(得分:3)

似乎我不擅长使用Google搜索:

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

答案 1 :(得分:0)

我也一直在致力于实现DevOps管道的自动化,并一直回到这里。其中一些信息似乎已过时。在撰写本文时,我相信Microsoft文档中的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();
    }
}

答案 2 :(得分:0)

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