Omar Amalfi
Omar Amalfi

Reputation: 399

Is it possible in Azure DevOps pipelines yaml templates to set a dependsOn and condition from a parameter jobList?

I want to provide a template that can execute some pre-deploy jobs and the deploy job must wait for the pre-deploy jobs to execute.

Having this template "template-deploy.yaml":

parameters:
 - name: preDeployJobs
   type: jobList
   default: []


stage:
  - ${{ each preDeployJob in parameters.preDeployJobs }}:
    - ${{ each pair in preDeployJob }}:
        ${{ pair.key }}: ${{ pair.value }}

  - job: Deploy
    dependsOn: []  # preDeployJobs must be executed after all preDeployJobs complete
    condition: succeeded()  # preDeployJobs all jobs have to complete successfully
    steps:
      - bash: echo deploy

and for example, a pipeline that uses it "dev.yaml":

stages:
  template: template-deploy.yaml
  parameters: 
     preDeployJobs:
        - job: provisioning
          steps: ...

        - job: databaseMigration
          steps: ...  

Upvotes: 1

Views: 480

Answers (1)

Vic
Vic

Reputation: 66

In your template-deploy.yaml, you can specify that the Deploy job depends on the other jobs by adding their job names to the dependsOn property: parameters:

  - name: preDeployJobs
    type: jobList
    default: []

jobs:
  - ${{ each job in parameters.preDeployJobs }}:
    - job: ${{ job.job }}
      steps: ${{ job.steps }}

  - job: Deploy
    dependsOn:
      - ${{ each job in parameters.preDeployJobs }}:
        - ${{ job.job }}
    condition: succeeded()  
    steps:
      - bash: echo deploy

In your pipeline that uses it "dev.yaml", you just pass the job names and their steps:

stages:
  - stage: deployStage
    jobs:
    - template: template-deploy.yaml
      parameters: 
        preDeployJobs:
          - job: provisioning
            steps:
              - bash: echo provisioning

          - job: databaseMigration
            steps:
              - bash: echo databaseMigration

In this way, the Deploy job will only be triggered when both provisioning and databaseMigration jobs have successfully completed.

Upvotes: 5

Related Questions