10

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?

0

5 Answers 5

11

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

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

4

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 = "[email protected]"
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))

Comments

2

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.

3 Comments

keep in mind this isnt a webhook, this is a rest api endpoint protected by auth. I dont think a webhook like experience exists
thanks, can anyone point to any examples of this? the documentation is horrible. Whats required in the requestBody? whats not etc? I need to trigger a build for a specifc azure-pipeline.yaml in one specific repo in the project. If only "project" is required, what is the result? it would trigger builds for every repo across the entire project?
See this answer for more detail: stackoverflow.com/a/59857117/5225577
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.