Azure devops 将矩阵定义为多级yaml管道中的变量

Azure devops 将矩阵定义为多级yaml管道中的变量,azure-devops,yaml,azure-pipelines,Azure Devops,Yaml,Azure Pipelines,我正在创建一个多阶段的Azure DevOps(yaml)管道,它使用对其他模板的多个引用。正如您在下面的管道中看到的,我必须为我的构建客户端步骤.yml和分发客户端步骤.yml文件提供相同的矩阵。此矩阵用于我的管道策略 是否有方法将此矩阵声明为我的azure pipelines.yml文件的变量 stages: - stage: build displayName: Build & validate jobs: - template: pipelines/templat

我正在创建一个多阶段的Azure DevOps(yaml)管道,它使用对其他模板的多个引用。正如您在下面的管道中看到的,我必须为我的
构建客户端步骤.yml
分发客户端步骤.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-pipelines.yml文件

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


此外,您的
构建客户端步骤.yml
分发客户端步骤.yml
文件应该是作业模板,然后您应该能够将矩阵写入作业模板。这样,您只需要在
azure pipelines.yml
文件中引用作业模板。

在纯YAML中似乎不可能做到这一点,但表示可以从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 ]
      ...