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?

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 succeeded
    – Cid
    Commented Jul 22, 2021 at 14:01
  • Thanks, but I don't have any releases, only a pipeline.
    – snark
    Commented Jul 22, 2021 at 14:12

1 Answer 1

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.

2
  • can we also fail our pipeline based on output value from previous task?
    – shivam
    Commented May 5, 2022 at 14:58
  • 3
    @shivam 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.
    – snark
    Commented May 5, 2022 at 15:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.