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

2

5

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.

Therron answered 23/11, 2021 at 20:1 Comment(0)
K
9

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.

Kimbell answered 24/11, 2021 at 10:37 Comment(3)
This workaround is not super pretty. but works indeed! Nice being able to dot into the stageDependencies result - thanks :)Therron
Hi Krzysztof; can I ask, is there any particular reason to use the in function in the conditions? It looks like the eq function would be appropriate here?Hale
eq is enough ;) I just modified what I found in docs. It was just faster :) Good spot!Kimbell
A
1

Looks like this is possible now. Example from Microsoft...

stages:
- stage: A
  condition: false
  jobs:
  - job: A1
    steps:
    - script: echo Job A1
- stage: B
  condition: in(dependencies.A.result, 'Succeeded', 'SucceededWithIssues', 'Canceled')
  jobs:
  - job: B1
    steps:
    - script: echo Job B1

Documentation: Stage to stage dependencies

Aleurone answered 28/2, 2024 at 18:49 Comment(1)
I am not 100% sure, but it might look like the documentation at the time of my question. In this example the condition in stage B is related to stage A (stage to stage), while what I was originally looking for was a stage B to job A1 condition, but that was apparently not supported.Therron

© 2022 - 2025 — McMap. All rights reserved.