Ali Haider
Ali Haider

Reputation: 17

How to run azure devops pipelines using RestAPi?

I tried using API endpoint of azure devops to run my pipeline. But when i checked on azure devops portal, the pipeline didn't started to run. I am sharing my code here.

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)

now I am looping the pipeline

response = requests.post(url_of_pipeline,headers=headers)

when i did request.post it should start to run the pipeline on azure devops, but when i checked on azure devops portal. The pipeline was not running. How this can be fixed?

Upvotes: 0

Views: 1692

Answers (2)

Fairy Xu
Fairy Xu

Reputation: 568

Based on your requirement, you are using python to run the rest api to run the pipeline.

You can also try the following Python sample:

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)

Upvotes: 1

Krzysztof Madej
Krzysztof Madej

Reputation: 40849

Here you have an example how it can be done in 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 

However currently you have two endpoints vailable Builds - Queue and Runs - Run Pipeline

Upvotes: 0

Related Questions