How do I make a yaml pipeline dependson work in the context of a previous loop?

Viewed 356

I want to be able to work through the array.. run the job however many times (as many times as items in the array) - But I need the next job to depend on the LAST job.

How do I get the value of the last element in the array into the dependsOn: parameter of the next job ?? Is it even possible? If not then how do I make the next job dependOn the success of the last run of the previous job?

parameters:
- name: solutionName
  type: object
  default: ['entry1', 'entry2']

variables:
- group: 'Key Vault Test'
- name: Var1
  value: 'lol'


trigger:
- main

jobs:
- ${{each Solution in parameters.SolutionName}}:
  - job: Job_${{Solution}}
    displayName: ${{Solution}}
    pool:
      vmImage: ubuntu-latest
    steps: 
    - script: |
        echo $(Var1)
        echo ${{Solution}}
        echo ${{join(' ',parameters.SolutionName)}}

- job: Job_2
  dependsOn: HOW_DO_I_PUT_JOB_NAME_IF_I_DONT_KNOW_IT
  displayName: Job2
  pool:
    vmImage: ubuntu-latest
  steps:
  - script: |
      echo 'Second Job'
2 Answers

You can use another each to define the list of dependencies in the second job:

jobs:
- ${{each Solution in parameters.SolutionName}}:
  - job: Job_${{Solution}}
    displayName: ${{Solution}}
    pool:
      vmImage: ubuntu-latest
    steps: 
    - script: |
        echo $(Var1)
        echo ${{Solution}}
        echo ${{join(' ',parameters.SolutionName)}}

- job: Job_2
  dependsOn:
    - ${{each Solution in parameters.SolutionName}}:
      - Job_${{Solution}}
  displayName: Job2
  pool:
    vmImage: ubuntu-latest
  steps:
  - script: |
      echo 'Second Job'

Note that the 'last' job from that first each loop is not a definite thing; those jobs will all be run in parallel, so any of them might finish first or last. So to make your second job wait for them all to finish, it needs an explicit dependency on each of them.

${{parameters.SolutionName}} is object type which is not accepted by the keyword dependsOn(it accepts string), to specify the job name, you need to use below format to depends on the previous jobs.

- job: Job_2
  dependsOn: 
    - Job_${{parameters.SolutionName[0]}}   # it points to the 1st job
    - Job_${{parameters.SolutionName[1]}}   # it points to the 2nd job
  displayName: Job2
  pool:
    vmImage: ubuntu-latest
  steps:
  - script: |
      echo 'Second Job'

enter image description here

Or you can put the Job_2 into a new stage.

stages:
  - stage: stage1
    jobs:
    - ${{each Solution in parameters.SolutionName}}:
      - job: Job_${{Solution}}
        displayName: ${{Solution}}
        pool:
          vmImage: ubuntu-latest
        steps: 
        - script: |
            echo $(Var1)
            echo ${{Solution}}
            echo ${{join(' ',parameters.SolutionName)}}
  - stage: stage2
    dependsOn: stage1   # depends on stage instead.
    jobs:
    - job: Job_2
      displayName: Job2
      pool:
        vmImage: ubuntu-latest
      steps:
      - script: |
          echo 'Second Job'
Related