Azure Devops - 矩阵策略 - 条件作业 运行

Azure Devops - Matrix Strategy - Conditional Job Run

*请忽略格式问题

trigger:
 - none
parameters:
- name: stg
displayName: "Environment Names"
type: string
 
pool:
  vmImage: ubuntu-latest
stages:
  - stage: stage1
    displayName: 'Stage C1'
    jobs:
    - job: jobabc
      strategy:
        maxParallel: 2
        matrix: 
            env_1:
                EnvironmentX: E1
                Browser: IE
            env_2:
                EnvironmentX: E2
                Browser: Firefox
            env_3:
                EnvironmentX: E3
                Browser: Chrome
    displayName: 'Job 1'
    condition: contains('${{parameters.stg}}', $(EnvironmentX))}}  
    steps: 
    - task: CmdLine@2
      displayName: 'My Task '
      inputs:
        script: 'echo Stage C1 $(Browser)' 

我将 stg 参数作为 E1E3E4 传递。

在Matrix中我定义了EnvironmentX,其值可以是E1, E2, or E3.

下面的代码 运行s Job 3 times with matrix values defined.

我不想 运行 所有 3 个作业,但 stg 参数值应该决定 JOb 运行 的矩阵值。

工作条件不工作?请问如何解决这个问题

您可以有条件地构建矩阵。不理想,但会奏效:

trigger:
 - none
parameters:
- name: stg
  displayName: Environment Names
  type: string

pool:
  vmImage: ubuntu-latest
stages:
- stage: stage1
  displayName: 'Stage C1'
  jobs:
  - job: jobabc
    strategy:
      maxParallel: 2
      matrix:
        ${{ if contains(parameters.stg, 'E1') }}:
          env_1:
            EnvironmentX: E1
            Browser: IE
        ${{ if contains(parameters.stg, 'E2') }}:
          env_2:
            EnvironmentX: E2
            Browser: Firefox
        ${{ if contains(parameters.stg, 'E3') }}:
          env_3:
            EnvironmentX: E3
            Browser: Chrome
    displayName: 'Job 1'
    steps: 
    - task: CmdLine@2
      displayName: 'My Task '
      inputs:
        script: 'echo Stage C1 $(Browser)'