Azure管道数据驱动矩阵



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

这将运行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 Actions的数据驱动矩阵策略?

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

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

添加跨产品矩阵策略

此外,还有一种变通方法在官方文件中提到了这个问题:

注意

矩阵语法不支持自动作业缩放,但您可以使用each关键字实现类似的功能。对于例如,请参阅nedrebo/参数化的azure作业。

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-pipelins-window.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 }}

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

# 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-包装作业

最新更新