使用模板在 Azure Devops 2020 yaml 管道中传递参数

时间:2021-02-03 20:15:47

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

我想实现一个 yaml 管道,以便我可以设置某些解决方案参数一次,然后在多个管道中使用它。目前我有:

azure-pipelines.yml

trigger:
- master

name: $(BuildVersionPrefix).$(DayOfYear)$(Date:HH)

extends:
  parameters:
    buildTemplate: build.yml
  template: solutions.yml

solutions.yml

parameters:
  - name: buildTemplate
    type: string
    default: ""

extends:
  template: ${{parameters.buildTemplate}}
  parameters:
    solutions: 
      - name: a
        SomeOtherProperty: x
      - name: b
        SomeOtherProperty: y
      - name: c
        SomeOtherProperty: z
      - name: d
        SomeOtherProperty: u
      - name: e
        SomeOtherProperty: v

build.yml

parameters:
  - name: solutions
    type: object
    default: []

steps:
 - ${{ each solution in parameters.solutions}}:
    - script: echo ${{solution.name}}
      displayName: build ${{solution.name}}

通过这种配置,我需要每个管道有两个 yml 文件,一个可以跨多个管道共享。有没有办法让每个管道有一个 yml 文件,同时将解决方案抽象为一个共享文件?

编辑 1

为了强调我们需要解决方案的完整语法,我添加了具有不同值的 SomeOtherProperty

1 个答案:

答案 0 :(得分:0)

<块引用>

有没有办法让每个管道有一个 yml 文件,同时将解决方案抽象为一个共享文件?

您可以尝试以下示例:

BuildandSolution.yml:

parameters:
  - name: solutions
    type: object
    default: [a,b,c,d,e]

steps:
- ${{ each solution in parameters.solutions }}: 
    - script: echo ${{ solution }}
      displayName: build ${{ solution }}

Azure-Pipelines.yml:

trigger:
- none

pool:
  vmImage: 'ubuntu-latest'

steps:
  - template: buildandsolution.yml

您可以直接将值添加到解决方案参数(对象类型)中。

更新:

我能理解您的要求,但恐怕我们无法在构建步骤之外单独指定解决方案参数。

以下是两个区块:

1.定义参数时不能直接使用定义对象类型参数的格式:

solutions: 
  - name: a
    SomeOtherProperty: x

这种定义方法需要依赖另一个模板。

2.我们不能只在模板中定义参数而不添加任何构建步骤。

此方法仅受变量支持。

参数字段不能调用yaml中的参数模板。因此,如果只定义了纯参数,则无法在主yaml中调用。