3

Is it possible to create dynamic jobs in the Azure DevOps pipeline?

I have a scenario wherein I have multiple directories for the deployment, the number of directories will be dynamic (for example: it can have 1 app for deployment or can have many). What I want to do is to create a dynamic number of jobs wherein it should run cd app && cf push for each directory.


deployments/

├── app1

│   ├── app.jar

│   └── manifest.yml

├── app2

│   ├── app.jar

│   └── manifest.yml

└── app3

|   ├── app.jar

|   └── manifest.yml

shrish
  • 408
  • 2
  • 8
  • 22

1 Answers1

6

You can try to use matrix configuration to handle this:

What you have to do is to build variables which represents your folder structure and then pass it as configuration:

jobs:
- job: JobA
  steps:
  - pwsh: |
      $json="{'job1': {'Work': 'work1'}, 'job2': {'Work': 'work2'}}"
      Write-Host "##vso[task.setvariable variable=targets;isOutput=true]$json"
    name: setTargets
  - script: echo $(setTargets.targets)
    name: echovar

- job: buildSrc
  dependsOn: JobA
  displayName: Build source
  strategy:
    matrix: $[ dependencies.JobA.outputs['setTargets.targets'] ]
  variables:
    targets: $[ dependencies.JobA.outputs['setTargets.targets'] ]
  steps:
  - pwsh: Write-Host "${{ convertToJson(variables) }}"
    displayName: 'Print all variables via expression'

So you will get this:

enter image description here

Be aware if you want to use stages as then you may need different syntax to reach output variable.

beatcracker
  • 6,714
  • 1
  • 18
  • 41
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107