3

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.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
tholesen
  • 460
  • 5
  • 10

1 Answers1

6

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.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • This workaround is not super pretty. but works indeed! Nice being able to dot into the stageDependencies result - thanks :) – tholesen Nov 24 '21 at 13:11
  • Hi Krzysztof; can I ask, is there any particular reason to use the [in function](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#in) in the conditions? It looks like the [eq function](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#eq) would be appropriate here? – Vince Bowdren Nov 24 '21 at 14:36
  • eq is enough ;) I just modified what I found in docs. It was just faster :) Good spot! – Krzysztof Madej Nov 24 '21 at 14:50