0
votes

I've got a very simple Azure Pipeline Release, I just want to skip a stage if there are any errors in the previous one. I've already checked https://docs.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=classic

Setting the Test job to run "Only when all previous jobs have succeeded" doesn't help

My main goal is to skip the test stage whenever there's a particular condition in the previous one, and passing variables between stages doesn't seem to be possible, nor using the gates, so I've got to idea to deliberately raise an error in the stage. The stages run some PS scripts, and I can't make the whole stage fail from that either

Screen shot

2
If you're using logging command to raise the error, you need to add exit1 as Krzysztof Madej suggests. Also, this is documented [here](Krzysztof Madej).LoLance

2 Answers

0
votes

The stages run some PS scripts, and I can't make the whole stage fail from that either

Are you using PowerShell task or similar tasks? Try editing the task and enabling the Fail on Standard Error option:

enter image description here

After that task will fail if there's any error is written to the error pipeline. Also in pre-deployment conditions of your Test stage, make sure the trigger when ... checkbox is unchecked:

enter image description here

0
votes

From what I understood you need sth like this feature. So as you see this is not supported out of the box.

You can use REST API to get state of running release and analyze response to verify it there is any issue with issueType = Error. Then in this script you need to call exit 1. This is not ideal, but it works.

$uri = "https://vsrm.dev.azure.com/thecodemanual/Devops manual/_apis/release/releases/$(Release.ReleaseId)?api-version=5.1"

Write-Host $(Release.ReleaseId)
Write-Host $uri

# Invoke the REST call
$result = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}


$errors = @()

$result.environments |
    ForEach-Object { $_.deploySteps | 
        ForEach-Object { $_.releaseDeployPhases |
            ForEach-Object { $_.deploymentJobs |
                ForEach-Object { $_.tasks |
                    ForEach-Object { $errors += $_.issues | Where-Object { $_.issueType -eq "Error" } }}}}}

Write-Host $errors.Count

if($errors.Count -gt 0) {
    Write-Host Error
    exit 1
}

Without step above I got this:

enter image description here

And with this step this:

enter image description here