How to force an Azure DevOps pipeline to fail if any previous tasks have failed?
Asked Answered
W

1

14

My YAML-based Azure DevOps pipeline contains several tasks in which continueOnError: true. I did this deliberately so that I can run all test suites even when some of them fail (each task runs a different suite of tests). At the moment the build is marked as 'partially succeeded' if any of these tasks fail. Now how do I force the build to be marked as 'failed' instead?

I could probably do this manually by setting a build variable in each task if it fails, and then checking the value of this variable in a final step. But what's the easiest way to force a build to fail if any of the previous steps in the pipeline have failed?

Wardroom answered 22/7, 2021 at 13:50 Comment(2)
On release -> Run on agent -> Run this job "Only when all previous jobs have succeeded". This option may prevent you to deploy the built application if it's partially succeededSeaplane
Thanks, but I don't have any releases, only a pipeline.Wardroom
W
22

Adding one of these tasks to the end of each job seems to do the trick:

- bash: |
    echo AGENT_JOBSTATUS = $AGENT_JOBSTATUS
    if [[ "$AGENT_JOBSTATUS" == "SucceededWithIssues" ]]; then exit 1; fi
  displayName: Fail build if partially successful

The echo line is optional. See the Azure docs here re the agent variable called Agent.JobStatus and the values it can take.

Or you can use Azure's built-in conditions to check the variable directly:

- bash: exit 1
  displayName: Fail build if partially successful
  condition: eq(variables['Agent.JobStatus'], 'SucceededWithIssues')

Making the bash task return any non-zero value will cause it to fail when the condition is met.

Wardroom answered 22/7, 2021 at 15:36 Comment(2)
can we also fail our pipeline based on output value from previous task?Agnusago
@Agnusago Probably! I recommend you post that as a separate question with more details;. E.g, are you using a bash task? You could probably use task.setvariable to set a variable to the output of some command and then set condition of a later task to fail the pipeline based on the value of that variable.Wardroom

© 2022 - 2024 — McMap. All rights reserved.