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.