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?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With