Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an Azure DevOps build pipeline, is there a way to cancel one pipeline job from another job?

I have an Azure DevOps Build Pipeline which contains two Agent Jobs, which I'll call Job A and Job B. I want these jobs to run simultaneously, but if Job A fails, then I don't need Job B to run to completion.

Is there any way to add a task to Job A which will cancel Job B (or, alternately, terminate the entire pipeline with a "Failed" status) if any of Job A's tasks failed?

like image 770
Sam Hanley Avatar asked Oct 19 '25 00:10

Sam Hanley


1 Answers

Add a PowerShell task that cancel the pipeline when a task failed:

steps:
- powershell: |
   Write-Host "Cancel all jobs..."
   $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds/$($env:BUILD_BUILDID)?api-version=2.0"
    $header = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"  }
    $body = @{ 'status'='Cancelling' } | ConvertTo-Json
    Invoke-RestMethod -Uri $url -Method Patch -Body $body -Headers $header -ContentType application/json
  displayName: Cancel the pipeline
  condition: failed()
  env:
       System_AccessToken: $(System.AccessToken)

Result:

enter image description here

like image 182
Shayki Abramczyk Avatar answered Oct 21 '25 06:10

Shayki Abramczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!