I'd like to trigger an Azure devops pipeline via a webhook.
For example, I'd like to be able to send a POST to some endpoint at Azure with some JSON, then have that endpoint trigger a pipeline to invoke, passing it the JSON.
Is this possible?
I'd like to trigger an Azure devops pipeline via a webhook.
For example, I'd like to be able to send a POST to some endpoint at Azure with some JSON, then have that endpoint trigger a pipeline to invoke, passing it the JSON.
Is this possible?
This is now available on Azure DevOps Services: Generic webhook based triggers for YAML pipelines
The request URL will then look something like this:
https://dev.azure.com/<orgName>/_apis/public/distributedtask/webhooks/<WebHook Name>?api-version=6.0-preview
In order to Queue a Build using a REST API call you can send a POST request to the following URI:
POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitionId={definitionId}&api-version=6.0
You can get the definition ID by simply opening that particular pipeline in Azure DevOps. THE URL of the page contains the definitionId like this:
https://dev.azure.com/{organization}/{project}/_build?definitionId=1&_a=summary
. For this example, the definitionId is 1. The header of your request should contain a Personal Access Token with scope vso.build_execute
vso.build_execute - Grants the ability to access build artifacts, including build results, definitions, and requests, and the ability to queue a build, update build properties, and the ability to receive notifications about build events via service hooks.
The following Request in Curl will look like:
curl -X POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitionId=1&api-version=6.0 -H "Authorization: Basic <Personal-Access-Token>" -H "Content-Type: application/json" -H "Content-Length: 0"
The following Request in Python will look like:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitionId=1&api-version=6.0"
headers = CaseInsensitiveDict()
headers["Authorization"] = "Basic <Personal-Access-Token>"
headers["Content-Type"] = "application/json"
headers["Content-Length"] = "0"
resp = requests.post(url, headers=headers)
print(resp.status_code)
The following Request in Java will look like:
URL url = new URL("https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitionId=1&api-version=6.0");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setRequestProperty("Authorization", "Basic <Personal-Access-Token>");
http.setRequestProperty("Content-Type", "application/json");
http.setRequestProperty("Content-Length", "0");
System.out.println(http.getResponseCode() + " " + http.getResponseMessage());
http.disconnect();
The following Request in C#/ .NET will look like:
var url = "https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitionId=1&api-version=6.0";
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.Headers["Authorization"] = "Basic <Personal-Access-Token>";
httpRequest.ContentType = "application/json";
httpRequest.Headers["Content-Length"] = "0";
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
Console.WriteLine(httpResponse.StatusCode);
The following Request in Powershell will look like:
$Header = @{
"authorization" = "Basic <Personal-Access-Token>"
}
$Parameters = @{
Method = "POST"
Uri = "https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitionId=1&api-version=6.0"
Headers = $Header
ContentType = "application/json"
}
Invoke-RestMethod @Parameters
Just in case anyone else is following along what Anirban Saha suggested, it's worth noting that the placeholder <Personal-Access-Token>
actually needs to be your username, concatenated with a colon and the actual PAT and that whole thing needs to be base64 encoded, as described in more detail at https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page#use-a-pat
A slightly easier to use Python example would be:
import json
import requests
from requests.auth import HTTPBasicAuth
url = "https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitionId=1&api-version=6.0"
PAT_USER = "user@example.com"
PAT = "{personal_access_token}"
resp = requests.post(url, json={}, auth=HTTPBasicAuth(PAT_USER, PAT))
print(resp.status_code)
print(json.dumps(resp.json(), indent=4, ensure_ascii=False))
It is possible. You can find the documentation here.
See this answer for more detail: stackoverflow.com/a/59857117/5225577
POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.0
The required fields are the project, organization and api-version. The optional parameters allow you to customize the nature of the build, such as passing in the source of the build or the check-in ticket.
Trigger azure pipeline via webhook?
I agree with 4c74356b41.
I do not think there is real webbhook support that. AFAIK, Webhook normally don't support POST
data, it jut a simple Get
.
You can check the similar thread on the github about this issue for some more details:
Triggering a build from a webhook
Hope this helps.