我可以实例化带有值列表的模板管道吗?

时间:2020-06-08 15:25:12

标签: azure-devops

我们有以下Azure DevOps管道模板:

- stage: DeployToPreprod
  displayName: 'Deploy to PREPROD'
  dependsOn: PrepareDeployToPreprod
  condition: not(eq(variables['Build.Reason'], 'Schedule'))
  jobs:
    - template: Scripts/deploy.yaml
      parameters:
        targetHost: SRV-SAF
        targetHostDisplayName: SRV_SAF
        targetEnv: PREPROD
    - template: Scripts/deploy.yaml
      parameters:
        targetHost: DT1CTX003
        targetHostDisplayName: DT1CTX003
        targetEnv: PREPROD
    - template: Scripts/deploy.yaml
      parameters:
        targetHost: DT1CTX004
        targetHostDisplayName: DT1CTX004
        targetEnv: PREPROD
    - template: Scripts/deploy.yaml
      parameters:
        targetHost: VDA-PROD-R01
        targetHostDisplayName: VDA_PROD_R01
        targetEnv: PREPROD
    - template: Scripts/deploy.yaml
      parameters:
        targetHost: VDA-PROD-R02
        targetHostDisplayName: VDA_PROD_R02
        targetEnv: PREPROD
    - template: Scripts/deploy.yaml
      parameters:
        targetHost: VDA-PROD-R03
        targetHostDisplayName: VDA_PROD_R03
        targetEnv: PREPROD
    - template: Scripts/deploy.yaml
      parameters:
        targetHost: VDA-PROD-R04
        targetHostDisplayName: VDA_PROD_R04
        targetEnv: PREPROD

很容易看出,这基本上是用不同的计算机名称列表实例化同一模板。有没有一种方法可以删除一些重复并实例化模板几次,从而为其提供一个列表?

1 个答案:

答案 0 :(得分:1)

有没有一种方法可以删除重复项并实例化模板几次,从而为其提供一个列表?

答案是肯定的。

您可以使用strategy and matrix解决此问题:

- stage: DeployToPreprod
  displayName: 'Deploy to PREPROD'
  dependsOn: PrepareDeployToPreprod
  condition: not(eq(variables['Build.Reason'], 'Schedule'))
  jobs:
  - job: Dev
    displayName: Dev
    pool:
     name: MyPrivateAgent

    strategy: 
      matrix:
        dev_1:
          targetHost: SRV-SAF
          targetHostDisplayName: SRV_SAF
          targetEnv: PREPROD
        dev_2:
          targetHost: DT1CTX003
          targetHostDisplayName: DT1CTX003
          targetEnv: PREPROD

     - template: child.yml #change this to your Scripts/deploy.yaml file
       parameters:
         targetHost: $(targetHost)
         targetHostDisplayName: $(targetHostDisplayName)
         targetEnv: $(targetEnv)

Child.yml

parameters:
- name: targetHost 
  type: string 
  default: false

- name: targetHostDisplayName 
  type: string 
  default: false

- name: targetEnv 
  type: string 
  default: false

steps:
- script: echo ${{ parameters.targetHost }}
  displayName: 'targetHost'

- script: echo ${{ parameters.targetHostDisplayName }}
  displayName: 'targetHostDisplayName'

- script: echo ${{ parameters.targetEnv }}
  displayName: 'targetEnv'

结果:

enter image description here

希望这会有所帮助。