Is it possible to pass a template with a list of jobs to a jobList type param?

Viewed 1445

Currently in Azure pipelines, we can pass the list of jobs to be executed to a child template with a parameter of jobList type as shown in the doco.

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#iterative-insertion

Is there a way I can encapsulate these jobs from pipeline.yml that are being passed to the jobList param inside another job template and pass that template to a jobList param. I tried to structure my pipeline as follows:

pipeline.yml
deployment-template.yml
post-deploy-tests-dev.yml
post-deploy-smoke-tests-prod.yml

I would like to dynamically insert different tests' jobs to the end of the deployment template depending on the environment. I tried the jobList type parameter in the deployment-template.yml as the following but it throws an error saying mapping not expected.

#post-deploy-tests-dev.yml
jobs:
    - job: Test1
      steps:
      - script: execute test1

#post-deploy-tests-smoke-tests-prod.yml
jobs:
    - job: Test2
      steps:
      - script: execute test2

#pipeline.yml
...
- template: deployment-template.yml
  parameters:
    environment: dev
    testsJobsList: 
      template: post-deploy-tests-dev.yml
- template: deployment-template.yml
  parameters:
    environment: prod
    testsJobsList: 
      template: post-deploy-smoke-tests-prod.yml

#deployment-template.yml
parameters:
  - name: testsJobsList
    type: jobList
    default: []
#All deployment jobs here
jobs:
...
...
#Tests as the end
  - ${{ parameters.testsJobsList }}

Is there a way to dynamically pass the jobList ?

1 Answers

it throws an error saying mapping not expected.

Test with the YAML sample, the cause of this issue: you are missing - before the template field in pipeline.yml file (- template: post-deploy-tests-dev.yml and - template: post-deploy-smoke-tests-prod.yml). In this position, template is equivalent to a job, and you need to add -.

Here is my Sample:

pipeline.yml

trigger:
- none

pool:
  vmImage: 'windows-latest'

jobs:
- template: deployment-template.yml
  parameters:
    testsJobsList: 
      - template: post-deploy-tests-dev.yml

- template: deployment-template.yml
  parameters:
    testsJobsList: 
      - template: post-deploy-smoke-tests-prod.yml

deployment-template.yml

parameters:
- name: 'testsJobsList'
  type: jobList
  default: []

jobs:
- ${{ each job in parameters.testsJobsList }}: # Each job
  - ${{ each pair in job }}:          # Insert all properties other than "steps"
      ${{ if ne(pair.key, 'steps') }}:
        ${{ pair.key }}: ${{ pair.value }}
    steps:                            # Wrap the steps
    - ${{ job.steps }}                # Users steps

post-deploy-tests-dev.yml

jobs:
    - job: Test1
      steps:
      - script: echo test1

post-deploy-smoke-tests-prod.yml

jobs:
    - job: Test2
      steps:
      - script: echo test2

Result:

enter image description here

Related