Ali Haider
Ali Haider

Reputation: 17

How to cancel running pipelines using azure devops restapi?

I tried to cancel the running pipeline using api call, pipelines are configured using yaml file on azure devops , but when i run my code the pipeline was still running and it didn't cancelled. I used api call using python to azure devops pipeline to cancel the running pipelines. Below url is the end point that i have used for api call. I have used definition id in build id which i guess is same.

url_cancel= https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=6.0

Python code

data = {"status":"Cancelling"}
                    a = json.dumps(data)

                    # Post request to rest API
                    response = requests.request("PATCH", url_cancel, headers=headers, data=a)

Upvotes: 1

Views: 1831

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35414

To cancel the running build, you can use the Rest API: Builds - Update Build

PATCH https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=6.0

Here is the Python sample:

import requests
import json
import base64

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



url = "https://dev.azure.com/{ORG}/{PROJECT}/_apis/build/builds/{BUILDID}?api-version=6.0"



payload = json.dumps({
  "status": "Cancelling"
})

headers = {
  'Authorization': 'Basic '+authorization,
  'Content-Type': 'application/json',
  
}

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

print(response.text)

Update:

My Result:

enter image description here

Upvotes: 1

Related Questions