Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 923
shrish Avatar asked Oct 24 '25 19:10

shrish


1 Answers

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.

like image 177
Krzysztof Madej Avatar answered Oct 27 '25 01:10

Krzysztof Madej