shrish
shrish

Reputation: 428

Dynamic jobs based on the number of directories

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

Upvotes: 4

Views: 1864

Answers (1)

Krzysztof Madej
Krzysztof Madej

Reputation: 40939

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.

Upvotes: 9

Related Questions