Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Devops YAML pipeline - how to repeat a task

In my YAML pipeline I have a deployment job:

- stage: deployment_to_development
  jobs:
  - deployment: deployment_to_development
    displayName: Deploy to Development
    environment: Development
    variables:
    - template: migrate-vars.yml
    strategy:
      runOnce:
        deploy:
          steps:
          - template: migrate-data.yml

The deployment template is a simple DbUp task:

steps:
  - task: UpdateDatabaseWithDbUp@2
    displayName: Migrate data
    inputs:
      ConnectionString: 'Data Source=$(DatabaseServer);Initial Catalog=$(DatabaseName);Integrated Security=SSPI'
      ScriptPath: '$(Pipeline.Workspace)\data-migrations'
      JournalSchemaName: dbo
      JournalTableName: _SchemaVersions
      LogScriptOutput: true
      IncludeSubfolders: true
      Order: FolderStructure
      TransactionStrategy: SingleTransaction

The variables template defines the server and db name:

variables:
   DatabaseServer: 'server'
   DatabaseName: 'dbName'
   Instances: '_1,_2'

This all works fine for a single database. However, I wish to repeat the task for each instance defined in the Instances variable, i.e for databases named dbName_1, dbName_2. This seemingly simple feat is proving difficult.

I have tried passing the instances as a parameter array and iterating them using

parameters:
  param: []

steps:
  - ${{each p in parameters.param}}:

but the 'p' variable isn't evaluated in the task. There have been many more futile attempts. I must be missing something very obvious here. What is it?

like image 414
semprini Avatar asked Mar 02 '23 12:03

semprini


1 Answers

I can't test UpdateDatabaseWithDbUp@2 but I have sth what explain how you can achieve your goal. First define template.yaml

parameters:
- name: 'instances'
  type: object
  default: {}
- name: 'server'
  type: string
  default: ''

steps:
- ${{ each instance in parameters.instances }}:
  - script: echo ${{ parameters.server }}:${{ instance }}

then reuse this template in your build:

steps:
- template: template.yaml
  parameters:
    instances: 
    - test1
    - test2
    server: someServer

And here is the result:

Azure DevOps Logs

like image 138
Krzysztof Madej Avatar answered Mar 18 '23 12:03

Krzysztof Madej