Search code examples
azure-devopsconditional-statementsazure-pipelinesmultistage-pipeline

Azure DevOps Conditional execution of Job that depends on a Job in another Stage


I have a pipeline.yaml that looks like this

pool:
  vmImage: image

stages:
  -stage: A
   jobs: 
     -job: a
      steps: 
     - script: |
          echo "This is stage build"
          echo "##vso[task.setvariable variable=doThing;isOutput=true]Yes"
        name: BuildStageRun
  -stage: B
   jobs:
      -job: b
       steps: #do something in steps

      -job: c
       dependsOn: a
       condition: eq(dependencies.build.outputs['BuildStageRun.doThing'], 'Yes')
       steps:
        - script: echo "I am scripting" 

So, there are 2 stages, A with one job a, and B with 2 jobs b and c. I would like to have job c executed only when job a has executed. I tried to do so by setting the variable doThing in job a to Yes and then check this variable in job c.

But I get an error:

Stage plan job c depends on unknown job a.

The varible definition and the definition of condition was taken from Azure documentation

Do you have any suggestion on how to get this working?


Solution

  • It's because you can't depend on a job from another stage, you can depend stage B on stage A or job c on job b.

    You can't achieve your goal with YAML conditions because you want to use a variable that you declared it in the first stage, the second stage doesn't know this variable, Azure DevOps don't support it yet:

    You cannot currently specify that a stage run based on the value of an output variable set in a previous stage.

    You can depend stage B on A, so if in stage A there is only one job you depend stage B on stage A:

    - stage: B
      dependsOn: A