jobs.job.strategy 定義

此作業的執行策略。

參考此定義的定義: pipelinejobs.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 且值為 string3 的變數可供作業使用。

注意

矩陣組態名稱必須只包含 A-Z 和 a-z) 的基本拉丁 (字母字母、 (0-9) 的數位,以及底 _ 線 () 。 這些名稱必須以字母開頭。 此外,其長度必須是 100 個字元或更少。

選擇性 maxParallel 關鍵字會指定要同時執行的矩陣回合數目上限。

如果未 maxParallel 指定或設定為 0,則不會套用任何限制。

如果未 maxParallel 指定,則不會套用任何限制。

注意

語法 matrix 不支援自動調整作業,但您可以使用 關鍵字來實作 each 類似的功能。 如需範例,請參閱 運算式

範例

在多個平臺上建置

此範例會 matrix 使用作業策略在多個平臺上建置。

# Build NodeJS Express app using Azure Pipelines
# https://learn.microsoft.com/azure/devops/pipelines/ecosystems/javascript?view=azure-devops
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 裝載的代理程式,在多個平臺上建置

下列範例會藉由指定 和 Pool 變數,在自我裝載代理程式和 vmImage 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 的變數可供使用。 在 「Build Python35」 中,變數會設定為 「3.5」。 它同樣設定為 「Build Python36」 中的 「3.6」。只有兩個作業同時執行。

策略:平行

平行作業策略會指定應該執行多少個重複作業。

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

屬性

parallel 字串。
執行此作業多次。

備註

平行作業策略適用于切割大型測試矩陣。 Visual Studio 測試工作瞭解如何將測試負載除以排程的工作數目。

範例

jobs:
- job: SliceItFourWays
  strategy:
    parallel: 4