You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure Pipeline中跳过特定分支合并触发的任务/流水线

Azure Pipeline特定合并触发时跳过任务的正确配置方法

问题描述

当Azure Pipeline由分支fe-release-inte-package合并到development分支触发(合并消息固定格式为Merge pull request XXX from fe-release-inte-package into development,XXX为整数)时,需要跳过目标任务或整个流水线。当前配置的YAML如下:

variables:
  CUSTOM_SOURCE_VERSION_MESSAGE: $(Build.SourceVersionMessage)

jobs:
  - job: deploy
    pool:
      name: Azure Pipelines
      vmImage: ubuntu-latest
    condition: not(contains(variables['CUSTOM_SOURCE_VERSION_MESSAGE'], 'fe-release-inte-package'))
    steps:
      - script: |
          echo "Printing all pipeline variables:"
          printenv
        displayName: Print all variables

执行后,Print all variables步骤中显示变量值符合预期:

CUSTOM_SOURCE_VERSION_MESSAGE=Merge pull request 8415 from fe-release-inte-package into development

但目标任务仍会执行,且无其他包含fe-release-inte-package的变量,需修正条件配置逻辑。

问题原因

问题出在变量插值时机:自定义变量CUSTOM_SOURCE_VERSION_MESSAGE使用$(Build.SourceVersionMessage)赋值时,模板解析阶段会优先替换这个变量占位符,但Build.SourceVersionMessage属于运行时变量(只有流水线启动后才能拿到真实值),导致条件判断时使用的是未被正确替换的占位符内容,而非实际合并消息。

解决方案

以下两种方法均可解决该问题:

方案1:直接使用系统变量Build.SourceVersionMessage

省略自定义变量,直接在条件中引用系统变量Build.SourceVersionMessage,确保条件判断使用运行时的真实值:

jobs:
  - job: deploy
    pool:
      name: Azure Pipelines
      vmImage: ubuntu-latest
    condition: not(contains(variables['Build.SourceVersionMessage'], 'fe-release-inte-package'))
    steps:
      - script: |
          echo "Printing all pipeline variables:"
          printenv
        displayName: Print all variables

方案2:使用运行时表达式语法

若必须保留自定义变量,需用$[]包裹条件表达式,强制Azure Pipeline在运行时计算条件,确保拿到变量的真实值:

variables:
  CUSTOM_SOURCE_VERSION_MESSAGE: $(Build.SourceVersionMessage)

jobs:
  - job: deploy
    pool:
      name: Azure Pipelines
      vmImage: ubuntu-latest
    condition: $[ not(contains(variables['CUSTOM_SOURCE_VERSION_MESSAGE'], 'fe-release-inte-package')) ]
    steps:
      - script: |
          echo "Printing all pipeline variables:"
          printenv
        displayName: Print all variables

进阶方案:精准匹配合并消息格式

如果需要更严格地匹配合并消息的固定格式,可使用matches函数结合正则表达式判断:

condition: $[ not(matches(variables['Build.SourceVersionMessage'], 'Merge pull request \\d+ from fe-release-inte-package into development')) ]

内容的提问来源于stack exchange,提问作者Horizon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 14:32:05