Azure Pipelines 数据驱动矩阵

Azure Pipelines Data Driven Matrix

在GitHub个动作中,我可以这样写一个矩阵作业:

jobs:
  test:
    name: Test-${{matrix.template}}-${{matrix.os}}
    runs-on: ${{matrix.os}}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]
        template: ['API', 'GraphQL', 'Orleans', 'NuGet']
    steps:
      #...

这将 运行 ostemplate 的每个组合。在 Azure Pipelines 中,您必须像这样手动指定每个组合:

stages:
- stage: Test
  jobs:
  - job: Test
    strategy:
      matrix:
        Linux:
          os: ubuntu-latest
          template: API
        Mac:
          os: macos-latest
          template: API
        Windows:
          os: windows-latest
          template: API
        # ...continued
    pool:
      vmImage: $(os)
    timeoutInMinutes: 20
    steps:
      #...

是否可以创建类似于 GitHub 操作的数据驱动矩阵策略?

这不是一个理想的解决方案,但现在,您可以循环参数。编写如下模板,并将您的数据传递给它。

# jobs loop template
parameters:
  jobs: []

jobs:
- ${{ each job in parameters.jobs }}: # 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
    - task: SetupMyBuildTools@1       # Pre steps
    - ${{ job.steps }}                # Users steps
    - task: PublishMyTelemetry@1      # Post steps
      condition: always()

查看此处了解更多示例:https://github.com/Microsoft/azure-pipelines-yaml/blob/master/design/each-expression.md#scenario-wrap-jobs

Is it possible to create a data driven matrix strategy similar to GitHub Actions?

答案是肯定的。这是一个已知问题,已在 github:

上报告

Add cross-product matrix strategy

此外,official documentation 中提到了这个问题的解决方法:

Note

The matrix syntax doesn't support automatic job scaling but you can implement similar functionality using the each keyword. For an example, see nedrebo/parameterized-azure-jobs.

jobs:
- template: azure-pipelines-linux.yml
  parameters:
    images: [ 'archlinux/base', 'ubuntu:16.04', 'ubuntu:18.04', 'fedora:31' ]
    pythonVersions: [ '3.5', '3.6', '3.7' ]
    swVersions: [ '1.0.0', '1.1.0', '1.2.0', '1.3.0' ]
- template: azure-pipelines-windows.yml
  parameters:
    images: [ 'vs2017-win2016', 'windows-2019' ]
    pythonVersions: [ '3.5', '3.6', '3.7' ]
    swVersions: [ '1.0.0', '1.1.0', '1.2.0', '1.3.0' ]

azure-pipelines-windows.yml:

jobs:
  - ${{ each image in parameters.images }}:
    - ${{ each pythonVersion in parameters.pythonVersions }}:
      - ${{ each swVersion in parameters.swVersions }}:
        - job:
          displayName: ${{ format('OS:{0} PY:{1} SW:{2}', image, pythonVersion, swVersion) }}
          pool:
            vmImage: ${{ image }}
          steps:
            - script: echo OS version &&
                      wmic os get version &&
                      echo Lets test SW ${{ swVersion }} on Python ${{ pythonVersion }}