Search code examples
azure-devopsazure-pipelinesazure-pipelines-yaml

In Azure Pipeline YAML, how to make stage run even though a job in previous stage fails, using succeeded('JobName')


I am trying to make my second stage run even though one of the two jobs in the first stage fails, but I cannot get it to work as expected with the job status check function succeeded('JobName').

In the following YAML pipeline, I would expect it to run Stage2 even though Job2 fails, as long as Job1 succeeds, but it does not:

stages:
  - stage: Stage1
    jobs:
      - job: Job1
        steps:
          - pwsh: echo "Job1"
      - job: Job2
        steps:
          - pwsh: write-error "Job2 error"

  - stage: Stage2
    condition: succeeded('Job1')
    jobs:
      - job: Job3
        steps:
          - pwsh: echo "Job3"

How do I get Stage2 to run even though Job2 has failed, as long as Job1 has succeeded?

Using always() will make Stage2 run always, but I would like it to depend the success state of Job1, regardless of Job2 state.

Related documentation:

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/stages?view=azure-devops&tabs=yaml#conditions

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#job-status-functions.


Solution

  • It looks that this is not possible to handle job result on stage level of the next stage. However you may use this workaraound:

    trigger: none
    
    pool:
      vmImage: ubuntu-latest
    
    stages:
      - stage: Stage1
        jobs:
          - job: Job1
            steps:
              - pwsh: echo "Job1"
          - job: Job2
            steps:
              - pwsh: write-error "Job2 error"
    
      - stage: Stage2
        dependsOn: Stage1
        condition: always()
        jobs:
          - job: Job3
            condition: in(stageDependencies.Stage1.Job1.result, 'Succeeded')
            steps:
              - pwsh: echo "Job3"
          - job: Job4
            condition: in(stageDependencies.Stage1.result, 'Succeeded')
            steps:
              - pwsh: echo "Job4"
    
    

    enter image description here

    Documentation for this you have here.