11

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?

snark
  • 2,462
  • 3
  • 32
  • 63
  • 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 Jul 22 '21 at 14:01
  • Thanks, but I don't have any releases, only a pipeline. – snark Jul 22 '21 at 14:12

1 Answers1

16

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.

snark
  • 2,462
  • 3
  • 32
  • 63
  • can we also fail our pipeline based on output value from previous task? – shivam May 05 '22 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`](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-a-job-scoped-variable-from-a-script) 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 May 05 '22 at 15:41