是否可以将带有作业列表的模板传递给 jobList 类型参数?

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

目前在 Azure 管道中,我们可以将要执行的作业列表传递给带有 jobList 类型参数的子模板,如 doco 中所示。

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

有没有一种方法可以将这些来自 pipeline.yml 的作业封装到另一个作业模板中的 jobList 参数,并将该模板传递给 jobList 参数。我尝试按如下方式构建我的管道:

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

我想根据环境动态地将不同的测试作业插入到 deployment template 的末尾。我尝试了 deployment-template.yml 中的 jobList 类型参数,如下所示,但它抛出一个错误,提示 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 }}

有没有办法动态传递 jobList

it throws an error saying mapping not expected.

用YAML样本测试,出现这个问题的原因:你在pipeline.yml文件(- template: post-deploy-tests-dev.yml- template: post-deploy-smoke-tests-prod.yml)。这个位置template相当于一个job,需要加上-.

这是我的样本:

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

结果: