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

Conditional dependent job in Azure Devops YAML pipelines


I'm building a complex pipeline in yaml and I'm trying to create a dependency between two jobs such that the latter job runs after the former, but only if the former is set to run based on a parameter. I can't seem to wrap my head around whether this is doable or not.

I have a pipeline defined like this:

parameters:
- name: doJobA
  type: boolean

stages:
  jobs:
  - job: JobA
    condition: eq('${{ parameters.doJobA }}', true)
    # ... details removed for brevity

  - job: JobB
    dependsOn: JobA
    # ... details removed for brevity

JobB should run after JobA if parameters.doJobA is true, or immediately if parameters.doJobA is false. Simply adding the dependsOn condition causes JobB to be skipped if the JobA condition is not met which makes sense, but I'd like it to run regardless.

Is it possible to define a conditional dependsOn in this manner?

EDIT: I've run into an additional problem with this that renders the solution below unusable. I need the condition to depend upon a variable set by an earlier running PowerShell script and not based on parameters.


Solution

  • Simpler solution from https://elanderson.net/2020/05/azure-devops-pipelines-depends-on-with-conditionals-in-yaml/

    parameters:
    - name: doJobA
      type: boolean
    
    stages:
      jobs:
      - job: JobA
        condition: eq('${{ parameters.doJobA }}', true)
        # ... details removed for brevity
    
      - job: JobB
        dependsOn: JobA
        condition: in(dependencies.JobA.result, 'Succeeded', 'Skipped')
        # ... details removed for brevity