MoonHorse
MoonHorse

Reputation: 2489

how to trigger azure pipelines through rest api

I want to trigger a specific Azure pipeline through REST API. 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.

I have tried to Get Pipeline with REST APIs but the answer doesn't give me pipelineId. 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:

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.

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!

Upvotes: 3

Views: 9034

Answers (2)

Shamrai Aleksander
Shamrai Aleksander

Reputation: 16133

If you want to start a new build, you may try to use this 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

enter image description here

Properties of the classic pipeline:

enter image description here

Then you can start a new build with powershell as example:

$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

Upvotes: 1

Krzysztof Madej
Krzysztof Madej

Reputation: 40839

pipelineId is equal to definitionId

So please go to your pipeline and check url like this 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

Upvotes: 3

Related Questions