Azure DevOps-矩阵策略 - 有条件的工作运行

发布于 2025-01-21 20:56:11 字数 984 浏览 0 评论 0原文

*请忽略格式问题

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)' 

I通过stg参数为e1e3e4

在矩阵中,i已定义vosingsx,其值可以为e1,e2或e3

下面的代码在定义的矩阵值的情况下运行3次作业。

我不想运行所有3个作业,但是STG参数值应该决定作业将运行哪个矩阵值。

工作状况不起作用?请解决这个问题

*please ignore formatting issue

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)' 

I pass stg parameter as E1E3E4.

In Matrix I have defined EnvironmentX whose value can be E1, E2, or E3.

Below code runs Job 3 times with matrix values defined.

I do NOT want to run all 3 jobs but stg parameter value should decide JOb would run for which matrix values.

Job Condition is NOT working? How to fix this please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2025-01-28 20:56:11

您可以有条件地构建矩阵。不理想,但这会起作用:

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)'

You could build the matrix conditionally. Not ideal but that will work:

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)'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文