The bash equivalent for @shayki-abramczyk 's script (thanks btw!)
- bash: |
echo "Cancel all jobs..."
url="${SYSTEM_TEAMFOUNDATIONCOLLECTIONURI}${SYSTEM_TEAMPROJECTID}/_apis/build/builds/${BUILD_BUILDID}?api-version=2.0"
curl -X "PATCH" "$url" \
-H "Authorization: Bearer ${SYSTEM_ACCESSTOKEN}" \
-H 'Content-Type: application/json' \
-d $'{"status":"Cancelling"}'
displayName: Cancel the pipeline
condition: failed()
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
You will have to also go into your project settings in azure devops, Permissions, Groups, edit the Build Administrators group and then add the Project Collection Build Service (yourcompanyhere) user to it.
Also a quick note on Failed over Cancelled. According to the docs there is a "result" field which has a failed option, but the "status" field does not. I have tried many permutations of just updating one and not the other, but the status=cancelling one is the only reliable one and in no cases could I get it to show the build as "failed" :(
Docs for the build api update method are here:
https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/update%20build?view=azure-devops-rest-6.0