【发布时间】:2021-10-11 19:18:58
【问题描述】:
我想通过 webhook 触发 Azure devops 管道。
例如,我希望能够使用一些 JSON 向 Azure 的某个端点发送 POST,然后让该端点触发一个管道来调用,并将 JSON 传递给它。
这可能吗?
【问题讨论】:
标签: azure-pipelines
我想通过 webhook 触发 Azure devops 管道。
例如,我希望能够使用一些 JSON 向 Azure 的某个端点发送 POST,然后让该端点触发一个管道来调用,并将 JSON 传递给它。
这可能吗?
【问题讨论】:
标签: azure-pipelines
现在可在 Azure DevOps Services 上使用:Generic webhook based triggers for YAML pipelines
然后请求 URL 将如下所示:
https://dev.azure.com/<orgName>/_apis/public/distributedtask/webhooks/<WebHook Name>?api-version=6.0-preview
【讨论】:
这是可能的。 You can find the documentation here。
更多详情请参阅此答案:stackoverflow.com/a/59857117/5225577
POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.0
必填字段是项目、组织和 api 版本。可选参数允许您自定义构建的性质,例如传入构建源或签入票。
【讨论】:
为了使用 REST API 调用对构建进行排队,您可以向以下 URI 发送 POST 请求:
POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?definitionId={definitionId}&api-version=6.0
您只需在 Azure DevOps 中打开该特定管道即可获取定义 ID。页面的 URL 包含如下定义 ID:
https://dev.azure.com/{organization}/{project}/_build?definitionId=1&_a=summary。对于此示例,定义 ID 为 1。您的请求标头应包含范围为 vso.build_execute 的个人访问令牌
vso.build_execute - 授予访问构建工件的能力,包括构建结果、定义和请求,以及将构建排队、更新构建属性以及通过服务挂钩接收有关构建事件的通知的能力。
Curl 中的以下请求将如下所示:
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"
Python 中的以下请求将如下所示:
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)
Java 中的以下请求将如下所示:
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();
C#/.NET 中的以下请求将如下所示:
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);
Powershell 中的以下请求如下所示:
$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
【讨论】:
以防万一其他人遵循 Anirban Saha 的建议,值得注意的是,占位符 <Personal-Access-Token> 实际上需要是您的用户名,并与冒号和实际 PAT 连接,并且整个内容需要进行 base64 编码,如https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page#use-a-pat 中更详细的描述
一个更容易使用的 Python 示例是:
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))
【讨论】:
通过 webhook 触发 azure 管道?
我同意 4c74356b41。
我认为没有真正的 webbhook 支持。 AFAIK,Webhook 通常不支持POST 数据,它只是一个简单的Get。
您可以在 github 上查看有关此问题的类似帖子以获取更多详细信息:
Triggering a build from a webhook
希望这会有所帮助。
【讨论】: