jobs.job.strategy 定义

此作业的执行策略。

引用此定义的定义:管道jobs.job

实现

执行 说明
策略:matrix、maxParallel 矩阵作业策略。
策略:并行 并行作业策略。

注解

若要从后续作业中的作业访问变量,请参阅 设置多作业输出变量。

策略:matrix、maxParallel

使用矩阵会生成作业的副本,每个副本都有不同的输入。 这些副本可用于针对不同的配置或平台版本进行测试。

strategy:
  matrix: # Matrix defining the job strategy; see the following examples.
    { string1: { string2: string3 }
  maxParallel: string # Maximum number of jobs running in parallel.

性能

matrix { string1: { string2: string3 }。
定义作业策略的 矩阵;请参阅以下示例。

maxParallel 字符串。
并行运行的作业数上限。

注解

strategy:
  matrix: { string1: { string2: string3 } }
  maxParallel: number

对于矩阵中 string1 的每个匹配项,将生成作业的副本。 string1 的名称 字符串是副本的名称,并追加到作业的名称。 对于 string2的每个匹配项,string2 的变量 字符串 3 的值可供作业使用。

注释

矩阵配置名称必须仅包含基本的拉丁文字母(A-Z 和 a-z)、数字(0-9)和下划线(_)。 它们必须以字母开头。 此外,它们的长度必须为 100 个字符或更少。

可选 maxParallel 关键字指定要同时运行的矩阵腿的最大数目。

如果未指定 maxParallel 或设置为 0,则不会应用限制。

注释

matrix 语法不支持自动作业缩放,但可以使用 each 关键字实现类似的功能。 有关示例,请参阅 表达式

例子

在多个平台上生成

此示例使用 matrix 作业策略在多个平台上构建。

# Build NodeJS Express app using Azure Pipelines
# https://learn.microsoft.com/azure/devops/pipelines/ecosystems/javascript
strategy:
  matrix:
    linux:
      imageName: 'ubuntu-latest'
    mac:
      imageName: 'macOS-latest'
    windows:
      imageName: 'windows-latest'

pool:
  vmImage: $(imageName)

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '8.x'

- script: |
    npm install
    npm test

- task: PublishTestResults@2
  inputs:
    testResultsFiles: '**/TEST-RESULTS.xml'
    testRunTitle: 'Test results for JavaScript'

- task: PublishCodeCoverageResults@1
  inputs: 
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/*coverage.xml'
    reportDirectory: '$(System.DefaultWorkingDirectory)/**/coverage'

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
    includeRootFolder: false

- task: PublishBuildArtifacts@1

此管道使用 脚本 在每个平台的整型脚本解释器中运行:macOS 和 Linux 上的 Bash、Windows 上的 CMD。 有关详细信息,请参阅 多平台脚本

使用自承载代理和Microsoft托管代理在多个平台上构建

以下示例通过同时指定 vmImagePool 变量来构建自承载代理和Microsoft托管代理,如以下示例所示。 对于托管代理,请指定 Azure Pipelines 作为池名称,对于自承载代理,则将 vmImage 留空。 自承载代理的空白 vmImage 可能会导致日志中的某些异常条目,但它们不会影响管道。

strategy:
  matrix:
    microsofthosted:
      poolName: Azure Pipelines
      vmImage: ubuntu-latest

    selfhosted:
      poolName: FabrikamPool
      vmImage:

pool:
  name: $(poolName)
  vmImage: $(vmImage)

steps:
- checkout: none
- script: echo test

使用不同的 Python 版本进行生成

jobs:
- job: Build
  strategy:
    matrix:
      Python35:
        PYTHON_VERSION: '3.5'
      Python36:
        PYTHON_VERSION: '3.6'
      Python37:
        PYTHON_VERSION: '3.7'
    maxParallel: 2

此矩阵创建三个作业:“生成 Python35”、“生成 Python36”和“生成 Python37”。在每个作业中,有一个名为PYTHON_VERSION的变量可用。 在“生成 Python35”中,变量设置为“3.5”。 同样,它在“生成 Python36”中设置为“3.6”。只有两个作业同时运行。

策略:并行

并行作业策略指定应运行作业的副本数。

strategy:
  parallel: string # Run the job this many times.

性能

parallel 字符串。
多次运行作业。

注解

并行作业策略可用于切片大型测试矩阵。 Visual Studio 测试任务 了解如何将测试负载划分为计划作业数。

例子

jobs:
- job: SliceItFourWays
  strategy:
    parallel: 4