如何通过 rest api 触发 azure pipelines

how to trigger azure pipelines through rest api

我想通过 REST API 触发特定的 Azure 管道。 我检查了 this documentation 。所以核心信息将是:

curl -x POST  $username:$personalaccesstoken https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.0-preview.1

在这里,我无法在 azure devops 的任何地方找到 $pipelineId

我尝试使用 REST APIs 获取管道,但答案没有给我 pipelineId。我尝试使用 az pipelines create 创建管道以获取结果中的 pipelineId 但它一直要求我进行 az 登录,尽管我一开始就成功地进行了 az 登录:

Before you can run Azure DevOps commands, you need to run the login command(az login if using AAD/MSA identity else az devops login if using PAT token) to setup credentials. Please see https://aka.ms/azure-devops-cli-auth for more information.

我检查过 this post 但没有给出答案。

老实说,如果您对如何找到 pipelineId 或任何其他使用 REST 触发管道的方法有任何想法 API,非常欢迎您!

pipelineId 等于 definitionId

所以请转到您的管道并像这样检查 url https://dev.azure.com/thecodemanual/DevOps%20Manual/_build?definitionId=177

或使用此端点获取定义列表:

https://dev.azure.com/{{organization}}/{{project}}/_apis/build/definitions?api-version=5.1

如果你想开始一个新的构建,你可以尝试使用这个API:Builds - Queue

您可以在构建定义的 URL(如 Krzysztof 所述)或管道属性(在经典构建中)中找到的定义 ID。

URL

经典管道的属性:

然后您可以使用 powershell 开始新的构建,例如:

$user = ""
$token = "your_token"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$org = "org_name"
$teamProject = "team_project_name"
$defId = "build_definition_id"

$restApiRunBuild = "https://dev.azure.com/$org/$teamProject/_apis/build/builds?definitionId=$defId&api-version=6.1-preview.6"


function InvokePostReques ($PostUrl, $body)
{   
    return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}

$result = InvokePostReques $restApiRunBuild ""

$result