Search code examples
azureazure-devopsazure-pipelinesnested-loopsazure-pipelines-yaml

Nested loops in azure pipeline yml


I have two parameter list and i want to pass these parameter values in sql task and webapp deploy task on azure pipeline yml.

parameters:
- name: db
  type: object
  default: [db1, db2, db3.......]

- name: apps
  type: object
  default: [app1, app2, app3......]

steps:
- ${{ each dblist in parameters.db && each applist in parameters.apps}}:
  - task: SqlAzureDacpacDeployment@1
    displayName: 'Azure SQL SqlTask ${{ db }}'
    inputs:
      azureSubscription: '$(Parameters.connectedServiceName)'
      ServerName: xxxx.xxxx.windows.net
      DatabaseName: ${{ dblist  }}
      SqlUsername: xxxxx
      SqlPassword: xxxxx
      deployType: SqlTask
      SqlFile: 'Table.sql'

  - task: AzureWebApp@1
    displayName: 'Azure Web App Deploy ${{ applist }}'
    continueOnError: true
    inputs:
      azureSubscription: '$(Parameters.connectedServiceName)'
      appType: webApp
      ResourceGroupName: $(group)
      appName: ${{ applist }}
      package: '$(build.artifactstagingdirectory)/**/*.zip'

When i run this pipeline, i got error and sequence that i want to run might not be right.

How can i run tasks like this loop: 
db1
app1
db2
app2
db3
app3...... and so on

How the issue can be solved?


Solution

  • So you have an array of app + db that you would like to deploy in sequence if i understand. Array are just objects so you should be able to do something like that

    parameters:
    - name: configurations
      type: object
      default:
      - app: app1
        db: db1
      - app: app2
        db: db2
    
    steps:
    - ${{ each configuration in parameters.configurations }}:
      - pwsh: Write-Host Hello ${{ configuration.db }}
        displayName: Deploy database
    
      - pwsh: Write-Host Hello ${{ configuration.app }}
        displayName: Deploy webapp