简体   繁体   English

如何通过rest api触发azure管道

[英]how to trigger azure pipelines through rest api

I want to trigger a specific Azure pipeline through REST API.我想通过 REST API 触发特定的 Azure 管道。 I have checked this documentation .我已经检查了这个文档 So the core message will be:所以核心信息将是:

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

Here, i cannot get find the $pipelineId anywhere in azure devops.在这里,我无法在 azure devops 中的任何地方找到$pipelineId

I have tried to Get Pipeline with REST APIs but the answer doesn't give me pipelineId.我尝试使用 REST API 获取管道,但答案没有给我管道 ID。 I have tried to create pipeline with az pipelines create to get the pipelineId in the result but it keeps asking me to az login although i do az login succesfully at first:我尝试使用az pipelines create创建管道以获取结果中的pipelineId ,但它一直要求我 az login 尽管我首先成功登录了 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.在运行 Azure DevOps 命令之前,需要运行登录命令(如果使用 AAD/MSA 身份,则为 az login,如果使用 PAT 令牌,则为 az devops login)来设置凭据。 Please see https://aka.ms/azure-devops-cli-auth for more information.有关详细信息,请参阅https://aka.ms/azure-devops-cli-auth

I have checked this post but it doesn't give the answer.我已经检查了这篇文章,但它没有给出答案。

Honestly, if you have any idea me on how to find the pipelineId or any other method to trigger pipeline with REST API, you are more than welcome!老实说,如果您对如何找到 pipelineId 或任何其他方法来使用 REST API 触发管道有任何想法,我们非常欢迎您!

pipelineId is equal to definitionId pipelineId等于definitionId

So please go to your pipeline and check url like this https://dev.azure.com/thecodemanual/DevOps%20Manual/_build?definitionId=177 .所以请转到您的管道并检查这样的https://dev.azure.com/thecodemanual/DevOps%20Manual/_build?definitionId=177

Or use this endpoint to get list of deifnitions:或使用此端点获取定义列表:

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

If you want to start a new build, you may try to use this API: Builds - Queue如果你想开始一个新的构建,你可以尝试使用这个 API: Builds - Queue

The definition id you can find in the URL of your build definition (as Krzysztof mentioned) or in the pipeline properties (in classic builds).您可以在构建定义的 URL(如 Krzysztof 提到的)或管道属性(在经典构建中)中找到定义 id。

URL网址

在此处输入图片说明

Properties of the classic pipeline:经典管道的属性:

在此处输入图片说明

Then you can start a new build with powershell as example:然后,您可以使用 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM