Reputation: 10922
I want to automate the queue-ing of Azure Pipelines with an API call, get information on the pipeline/build/job status,
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.
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?
Upvotes: 15
Views: 22351
Reputation: 312
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 869
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();
}
}
Upvotes: 5
Reputation: 10922
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] api
here on StackOverflow) point me to the Azure DevOps Services REST API that I had mentioned above.
Upvotes: 16