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

时间:2020-08-05 00:14:07

标签: azure-devops azure-pipelines azure-pipelines-yaml

当前在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的末尾。我按照以下方式尝试了jobList中的deployment-template.yml类型的参数,但是它抛出一个错误,提示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

1 个答案:

答案 0 :(得分:0)

它抛出一个错误,提示不期望映射。

使用YAML示例进行测试,造成此问题的原因:在 pipeline.yml 文件(-和{{1} })。在这个位置上,模板相当于一个工作,您需要添加- template: post-deploy-tests-dev.yml

这是我的样品:

pipeline.yml

- template: post-deploy-smoke-tests-prod.yml

deployment-template.yml

-

post-deploy-tests-dev.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

post-deploy-smoke-tests-prod.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

结果:

enter image description here