1

I have a release pipeline which depends on a query to move on. While the query doesn't get results, a stage keeps processing.

like this

What I'd like to do is to find a way to cancel a stage (or the whole release) when another release is created. I've already tried to use an azure devops API and also and az devops cli. But not success. Does anyone have any Idea how to solve it? Thanks

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

to cancel a stage (or the whole release) when another release is created

You can cancel the release with the below PowerShell command

Write-Host "##vso[task.setvariable variable=agent.jobstatus;]canceled"
Write-Host "##vso[task.complete result=Canceled;]DONE"

What you need to do is to add PowerShell Task with the above code and add a custom condition configure to cancel the build when new release is created. after this task all the other tasks will be cancelled.

The Status will show "Succeeded" but the release will be cancelled.

Also, if you want to show the status as cancelled, use the Rest API, for that check the below PowerShell script.

# Copyright (C) 2018 Lex Li
# https://github.com/lextm/vstsabort
# Released under Apache license.

$abort = $env:LEXTUDIO_VSTSABORT
Write-Host "Environment variable LEXTUDIO_VSTSABORT is $abort"
If ($abort -eq "TRUE")
{
    Write-Host "Abort."
    $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds/$($env:BUILD_BUILDID)?api-version=2.0"
    Write-Host "URL: $url"
    $pat = ":$env:SYSTEM_PAT"
    $b  = [System.Text.Encoding]::ASCII.GetBytes($pat)
    $token = [System.Convert]::ToBase64String($b)

    $body = @{ 'status'='Cancelling' } | ConvertTo-Json

    $pipeline = Invoke-RestMethod -Uri $url -Method Patch -Body $body -Headers @{
        'Authorization' = "Basic $token";
        'Content-Type' = "application/json"
    }
    Write-Host "Pipeline = $($pipeline)"
}
Else
{
    Write-Host "Continue."
}

For further information check this SO1 and SO2 threads.

SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15