3

在 GitHub Actions 中,我可以像这样编写矩阵作业:

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:
      #...

os这将运行和的每个组合template。在 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 Actions 的数据驱动矩阵策略?

4

2 回答 2

2

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

答案是肯定的。这是一个已知问题,已经在 github 上报告过:

添加跨产品矩阵策略

另外,官方文档中有提到这个问题的解决方法:

笔记

矩阵语法不支持自动作业缩放,但您可以使用 each 关键字实现类似的功能。例如,请参阅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' ]

天蓝色管道-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 }}
于 2020-07-14T09:01:42.323 回答
1

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

# 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

于 2020-07-14T08:37:53.060 回答