API for automating Azure DevOps Pipelines?
Asked Answered
I

4

15

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 "API" for the "Invoke HTTP Rest API" task: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/http-rest-api?view=vsts That might come in handy, but is not what I am looking for.

  2. There is a "Azure DevOps Services REST API": https://learn.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-5.1 But I couldn't find any mention of "Pipeline" there, so this doesn't seem to be the right thing as well.

    The StackOverflow tag azure-devops-rest-api also only mentions VSTS and 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.

Besides these two results, I only find other versions or translations of various copies of these - and a lot of unrelated documents that are about Azure in general.

Am I just using the wrong words to search?


Is there an actual API for Azure DevOps Pipelines?
Does it have a usable API Explorer?
Does it have proper clients for languages like JavaScript, Ruby or PHP?

Indecisive answered 11/12, 2018 at 14:45 Comment(1)
Powershell support: powershellgallery.com/packages/AzurePipelinesPSAstrogate
I
16

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.

Indecisive answered 11/12, 2018 at 14:49 Comment(2)
Submitted a review to change the azure-devops-rest-api tag.Indecisive
You can use the AzurePipelinePS powershell module to make the api calls. powershellgallery.com/packages/AzurePipelinesPSAstrogate
U
5

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();
    }
}
Unsex answered 9/12, 2020 at 9:45 Comment(1)
I believe for the pipelines run API, it should be "templateParameters", not "parameters". At least for me, it did not work with "parameters".Chunky
A
0

I ran into these problems as well and wound up making a powershell wrapper of the API and then wrapping that into an Azure DevOps Pipeline Template. I've just published it for anyone to use. I hope that anyone who finds this thread can find this template useful.

Assemblyman answered 18/5, 2021 at 22:37 Comment(0)
T
0

With AzFunc4DevOps this can be done in an event-driven way. And in C#.

E.g. here is how to trigger a build when another build succeeds:

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

Here are some more examples.

Toxoplasmosis answered 29/12, 2022 at 8:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.