0
votes

I have some code that loop through some values utilising templates. My issue is the jobs all run randomly so I want to create some dependencies, but because they are in a for loop I have not been able to work out how I can name the job steps to then utilise the "dependsOn" option. Also if I tried to hard code a job name I get a duplicate job name error.

patch-tasks.yml

 jobs:
   - job: '${{ parameters.sqlserver }}_A'
     displayName: '${{ parameters.sqlserver }}--set-up-stuff'
     steps:
     - task: PowerShell@2
       inputs:
         targetType: 'inline'
         script: |
           Write-Host "Patchgroup '${{ parameters.patchgroup }}'"
           Write-Host "sqlserver '${{ parameters.sqlserver }}'"
   
   - job: '${{ parameters.sqlserver }}_B'
     displayName: '${{ parameters.sqlserver }}--set-up-morestuff'
     steps:
     - task: PowerShell@2
       inputs:
         targetType: 'inline'
         script: |
           Write-Host "Patchgroup '${{ parameters.patchgroup }}'"
           Write-Host "sqlserver '${{ parameters.sqlserver }}'"

patch-template.yml

parameters:
- name: sqlservers 
  type: object
  default: [] 

- name: patchgroup 
  type: string
  default: ''


jobs:
- ${{ each sqlserver in parameters.sqlservers }}:
  - template: patch-tasks.yml
    parameters:
      sqlserver: ${{ sqlserver }}
      patchgroup: ${{ parameters.patchgroup }}

pipeline script

trigger: none

parameters:
- name: InstanceArgs 
  type: object
  default: [] 
    
variables:
    patchgroup: test
    


jobs: 
 - template: patch-template.yml  

   parameters:    
     patchgroup: $(patchgroup)    
     sqlservers:  ${{ parameters.InstanceArgs }} 
  
1
Hi friend, is there any update for this issue? Feel free to let me know if you're still blocked, I'll try my best to help :)Walter

1 Answers

1
votes

The pipeline must contain at least one job with no dependencies. So, you can try to add dependencies in the second job. For example:

jobs:
   - job: ${{ parameters.sqlserver }}_A
     displayName: '${{ parameters.sqlserver }}--set-up-stuff'
     steps:
     - task: PowerShell@2
       inputs:
         targetType: 'inline'
         script: |
           Write-Host "Patchgroup '${{ parameters.patchgroup }}'"
           Write-Host "sqlserver '${{ parameters.sqlserver }}'"
   
   - job: ${{ parameters.sqlserver }}_B
     displayName: '${{ parameters.sqlserver }}--set-up-morestuff'
     dependsOn: ${{ parameters.sqlserver }}_A
     steps:
     - task: PowerShell@2
       inputs:
         targetType: 'inline'
         script: |
           Write-Host "Patchgroup '${{ parameters.patchgroup }}'"
           Write-Host "sqlserver '${{ parameters.sqlserver }}'"

According to your configuration, your job names should be different. When I used the same job name as yours, I did not get a duplicate job name error. Here is my result:

enter image description here