如何使用 Rest API 运行 azure devops 管道?

How to run azure devops pipelines using RestAPi?

我尝试将 azure devops 的 API 端点用于 运行 我的管道。但是当我检查 azure devops 门户时,管道没有开始 运行。我在这里分享我的代码。

personal_access_token='PAT',
organization_url='url'
authorization = str(base64.b64encode(bytes(':'+personal_access_token, 'ascii')), 'ascii')
headers={'Accept':'application/json', 'Authorization': 'Basic '+authorization}
credentials=BasicAuthentication('',personal_access_token)
connect=Connection(base_url=organization_url,creds=credentials)

现在我正在循环管道

响应 = requests.post(url_of_pipeline,headers=headers)

当我执行 request.post 时,它应该开始 运行 azure devops 上的管道,但是当我检查 azure devops 门户时。管道不是 运行ning。如何解决?

这里有一个如何在 powershell 中完成的示例:

$AzureDevOpsPAT = "ocd2rrtds7bj6mff6jcxjllmaaXXXXXXXXXXXXXXXXXXXXXXXX"
$OrganizationName = "DEMOXXXXXXXXXXXXXXX"
$ProjectName = "SOMEPROJECT"
$buildId = 177

$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }

$uri = "https://dev.azure.com/$($OrganizationName)/$($ProjectName)/_build?definitionId=$($buildId)" 

Invoke-RestMethod -Uri $uri -Method post -Headers $AzureDevOpsAuthenicationHeader 

但是目前您有两个端点可用Builds - Queue and Runs - Run Pipeline

根据您的要求,您正在使用 python 到 运行 其余的 api 到 运行 管道。

您也可以尝试以下 Python 示例:

import requests
import json
import base64

pat = 'PAT'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')

print(authorization )

url = "https://dev.azure.com/{ORG}/{PROJECT}/_apis/pipelines/{pipelineid}/runs?api-version=6.1-preview.1"



payload = json.dumps({})
headers = {
  'Authorization': 'Basic '+authorization,
  'Content-Type': 'application/json',
  
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)