How should I run a group of tasks multiple times with different inputs?

Viewed 22

I am currently porting a build job from Jenkins to Azure Devops. The Jenkinsfile is roughly structured as:

def buildConfig(config) {
  build(config)
  runUnitTests(config)
  runOtherTests(config)
  ...
}

doExpensiveSharedSetup()
buildConfig("config1")
buildConfig("config2")
buildConfig("config3")
...
buildConfig("configN")
doExpensiveCleanup()

I would like to run all this in a single Azure pipeline, because I don't want to repeat the setup/cleanup code on multiple agents (as I think would happen if I triggered a pipeline once for each config). What is the typical approach for grouping multiple tasks into a unit that can be repeated with different input variables?

1 Answers

This can be done using templates (https://docs.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops).

The basic approach is to define a template containing the common code in a separate file:

parameters:
  - name: config
    type: string
    default: 'config1'

steps:
- task: Build@1
  inputs:
    config: ${{ parameters.config }}
    args: 4
- task: RunTests@7
  inputs:
    config: ${{ parameters.config }}

Then the main pipeline can instantiate this template multiple times with the needed arguments:

steps:
- task: ExpensiveSetup@2
  ...

- template: 'path-to-template-file.yml'
  parameters:
    config: 'Config1'

- template: 'path-to-template-file.yml'
  parameters:
    config: 'Config2'

- template: 'path-to-template-file.yml'
  parameters:
    config: 'Config3'

- task: ExpensiveCleanup@1
  inputs:
    argument: 'Finish'
Related