自动化Azure DevOps流水线的API?

15
我希望可以通过API调用自动排队Azure Pipelines,获取有关管道/构建/作业状态的信息。
以下是相关内容:
1. Azure Pipelines文档仅提到“Invoke HTTP Rest API”任务的“API”:https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/http-rest-api?view=vsts 这可能会有所帮助,但并不是我要找的东西。
2. 有一个“Azure DevOps Services REST API”: https://learn.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 APIs是一组API,允许管理Visual Studio Team Services帐户以及TFS 2015和2017服务器。

除了这两个结果,我只发现各种副本的其他版本或翻译 - 以及许多与Azure无关的不相关文档。
我是否使用了错误的搜索词?
Azure DevOps Pipelines是否有实际的API? 它是否有可用的API Explorer? 它是否具有适用于JavaScript、Ruby或PHP等语言的适当客户端?

1
PowerShell 支持: https://www.powershellgallery.com/packages/AzurePipelinesPS - Dejulia489
4个回答

16

1
提交了一份评论以更改“azure-devops-rest-api”标签。 - janpio
你可以使用AzurePipelinePS PowerShell模块来进行API调用。https://www.powershellgallery.com/packages/AzurePipelinesPS - Dejulia489

5

我也一直在努力自动化DevOps管道,并且不断回到这里。其中一些信息似乎已经过时。截至我写这篇文章的时间,我认为Microsoft文档中的这篇文章是最新的。我确实需要费点思考才能让它正常工作,但最终得到了这段代码。

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

1
我认为对于管道运行 API,应该是 "templateParameters" 而不是 "parameters"。至少对我来说,使用 "parameters" 没有起作用。 - FH-Inway

0

使用AzFunc4DevOps,可以以事件驱动的方式完成此操作。而且是用C#编写的。

例如,以下是如何在另一个构建成功时触发构建:

[FunctionName(nameof(TriggerBuildWhenAnotherBuildSucceeds))]
public static async Task Run(
    [BuildStatusChangedTrigger
    (
        Project = "%TEAM_PROJECT_NAME%",
        BuildDefinitionIds = "%BUILD_DEFINITION_ID%",
        ToValue = "Completed"
    )] 
    BuildProxy build,

    [BuildClient]
    BuildHttpClient buildClient,

    [BuildDefinition(Project = "%TEAM_PROJECT_NAME%", Id = "%NEXT_BUILD_DEFINITION_ID%")]
    BuildDefinitionProxy nextbuildDefinition
)
{
    await buildClient.QueueBuildAsync(new Build
    {
        Definition = nextbuildDefinition,
        Project = nextbuildDefinition.Project
    });
}

这里有一些更多的例子


0
我也遇到了这些问题,最终我创建了一个API的PowerShell包装器,并将其包装成Azure DevOps Pipeline模板。我刚刚发布了它,供任何人使用。我希望任何发现这个线程的人都能发现this template有用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接