Iterative insertion over depenencies and condition on Multistage Azure DevOps Yamls

257 Views Asked by At

I want to have multiple dependencies over a template. Is there a way to establish condition over the iteration of a parameter array ?

parameters:
  deps: [Integration, Migration]


jobs:
- deployment: Deploy
  displayName: 'Deploy'
  dependsOn: 
  - ${{ each dep in parameters.deps }}:
    - ${{ dep }}
  condition: 
  - ${{ each dep in parameters.deps }}:
    - in(dependencies.${{ dep }}.result, 'Succeeded', 'Skipped')
  environment: QA
  strategy:                 
    runOnce: 
      deploy: 
        steps:
        - bash: |
            echo "Deploy dev"

This one is marked as (Line: 12, Col: 3): A sequence was not expected

1

There are 1 best solutions below

0
Levi Lu-MSFT On

The error is caused by below condition:

condition: 
- ${{ each dep in parameters.deps }}:
  - in(dependencies.${{ dep }}.result, 'Succeeded', 'Skipped')

Above yaml will be evaluated to :

condition: 
  - in(dependencies.Integration.result, 'Succeeded', 'Skipped')
  - in(dependencies.Migration.result, 'Succeeded', 'Skipped')

Condition cannot accept a sequence of expressions. Multiple expressions should be joined with and , or or xor. See here for more information.

You might have to evaluate the condition outside the template.

For example, add an additional job in your azure-pipelines.yml to depend on the above jobs: See below:

  #azure-pipelines.yml

 - job: CheckStatus
   dependsOn:
   - Integration
   - Migration
   condition: |
     and
     (
      in(dependencies.Integration.result, 'Succeeded', 'SucceededWithIssues', 'Skipped'),
      in(dependencies.Migration.result, 'Succeeded', 'SucceededWithIssues', 'Skipped')
     )
     
   steps:
   - powershell: 

 - template: template.yaml

   parameters:
     deps: CheckStatus

Then you can just check the status of job CheckStatus in the template:

#template.yml
parameters:
  deps: CheckStatus
  
jobs:
- job: secure_buildjob
  dependsOn: ${{parameters.deps}}
  condition: eq(dependencies.${{ parameters.deps }}.result, 'Succeeded')