【问题标题】:Define matrix as variable in multi-staged yaml pipeline在多阶段yaml管道中将矩阵定义为变量
【发布时间】:2020-03-29 00:20:46
【问题描述】:

我正在创建一个多阶段 Azure DevOps (yaml) 管道,该管道使用对其他模板的多个引用。正如您在下面的管道中看到的,我必须为我的build-client-steps.ymldistribute-client-steps.yml 文件提供相同的矩阵。该矩阵用于我的管道策略。

有没有办法将此矩阵声明为我的azure-pipelines.yml 文件的变量?

stages:
- stage: build
  displayName: Build & validate
  jobs:
    - template: pipelines/templates/build-client-steps.yml
      parameters:
        matrix:
          base-ios-prod:
            app: string
            artifact: string
            variant: string
            os: ios
            distribution: string
          base-android-prod:
            app: string
            artifact: string
            variant: string
            os: android
            distribution: string
        keystore_password: '$(keystore_password)'
        keystore_alias: '$(keystore_alias)'
        APPLE_CERTIFICATE_SIGNING_IDENTITY: '$(APPLE_CERTIFICATE_SIGNING_IDENTITY)'
        APPLE_PROV_PROFILE_UUID: '$(APPLE_PROV_PROFILE_UUID)'
        teamId: '$(teamId)'
        P12Password: '$(P12Password)'         
    - template: pipelines/templates/build-server-steps.yml
    - template: pipelines/templates/build-infra-steps.yml
    - template: pipelines/templates/build-sap-steps.yml
- stage: prepareBase
  displayName: Prepare base infra
  dependsOn: build
  jobs:
    - template: pipelines/templates/deploy-infra-jobs.yml
      parameters:
        parameterFile: string
        resourceGroup: string
- stage: deployBase
  displayName: Deploy base
  dependsOn: prepareBase
  jobs:
    - template: pipelines/templates/deploy-server-jobs.yml
      parameters:
        resourceGroup: string
- stage: activateBase
  displayName: Activate base
  dependsOn: deployBase
  jobs:
    - template: pipelines/templates/swap-app-jobs.yml
      parameters:
        parameterFile: string
        resourceGroup: string
    - template: pipelines/templates/distribute-client-jobs.yml
      parameters:
        matrix:
          base-ios-prod:
            app: string
            artifact: string
            variant: string
            os: ios
            distribution: string
          base-android-prod:
            app: string
            artifact: string
            variant: string
            os: android
            distribution: string

【问题讨论】:

    标签: azure-devops yaml azure-pipelines


    【解决方案1】:

    在纯 YAML 中似乎无法做到这一点,但 documentation 表明可以从 JSON 序列化字符串解释矩阵策略配置。

    提供的示例演示了如何使用步骤来输出包含潜在动态 JSON 编码对象的变量,然后在后续步骤中使用该变量。我发现也可以以相同的方式使用包含 JSON 编码矩阵的静态变量。带有> 的 YAML 多行文本格式化功能使得定义这样的 JSON 字符串相当方便。例如:

    variables:
      targets: >
        {
          base-ios-prod:
          {
            app: 'string',
            artifact: 'string',
            variant: 'string',
            os: 'ios',
            distribution: 'string'
          },
          base-android-prod:
          {
            app: 'string',
            artifact: 'string',
            variant: 'string',
            os: 'android',
            distribution: 'string'
          }
        }
    
    stages:
    - stage: build
      displayName: Build
      jobs:
        - job: buildSrc
          displayName: Build source
          strategy:
            matrix: $[ variables.targets ]
          ...
    
    - stage: deploy
      displayName: Deploy
      jobs:
        - job: deployApp
          displayName: Deploy application
          strategy:
            matrix: $[ variables.targets ]
          ...
    

    【讨论】:

    • 谢谢,这对于我的用例来说几乎是完美的,其中矩阵是来自变量组的变量中的字符串化 JSON 对象。我必须做的唯一更改是使用格式 $[ variables['variable-name'] ] 正确引用变量
    • 我不得不在我的嵌套模板中使用 $[ parameters.variable_name ],而在其他地方总是使用 ${{ parameters.variable_name }} 作为参数。
    【解决方案2】:

    有没有办法将此矩阵声明为我的变量 azure-pipelines.yml 文件?

    对于这个问题,据我所知,这在yaml管道中是不可行的。在这个document中有一个声明:由于变量是在作业开始时展开的,所以不能在策略中使用它们.因此,即使您可以将矩阵定义为变量,也不能在策略中引用它。

    另外,您的build-client-steps.ymldistribute-client-steps.yml 文件应该是作业模板,然后您应该可以将矩阵写入作业模板。这样,您只需要引用azure-pipelines.yml文件中的作业模板即可。

    【讨论】:

      猜你喜欢
      • 2020-06-10
      • 1970-01-01
      • 2022-09-23
      • 2017-02-11
      • 2021-08-31
      • 2021-11-16
      • 2019-11-05
      • 1970-01-01
      • 2022-11-14
      相关资源
      最近更新 更多