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

如何配置Azure Pipelines仅在master分支提交及PR合并后运行?

问题

我有一个关联Azure Pipelines的GitHub仓库,想要配置管道仅在master分支有提交推送时运行,其他分支不触发。同时,针对master分支的拉取请求(PR),仅在合并到master分支后触发管道,而非PR创建或更新时触发。

目前我编写的Azure Pipeline YAML已实现:

  • master分支提交时执行
  • 其他分支提交时不执行

但存在问题:当在其他命名分支创建针对master的PR时,管道仍会被触发。现有YAML代码如下:

trigger:
  branches:
    include:
    - master

resources:
  - repo: self

variables:
  - name: imageName
    value: sample
  - group: containerReg

stages:
  - stage: Build
    displayName: Build image
    jobs:
      - job: Build
        displayName: Build
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          # Build the container image
          - task: Docker@2
            inputs:
              containerRegistry: 'GHCR service connection'
              repository: '$(registryUsername)/$(imageName)'
              command: 'build'
              Dockerfile: '**/Dockerfile'
              tags: |
                $(Build.BuildId)
                latest
            displayName: Build and tag container image

          # Login to containter registry
          - task: Docker@2
            inputs:
              containerRegistry: 'GHCR service connection'
              command: 'login'
            displayName: Login to container registry

          # Push all tagged images to the container registry
          - task: Docker@2
            inputs:
              containerRegistry: 'GHCR service connection'
              repository: '$(registryUsername)/$(imageName)'
              command: 'push'
              tags: |
                $(Build.BuildId)
                latest
            displayName: Push all tagged images to the container registry

希望修改YAML实现需求,且要阻止整个管道运行,而非仅跳过阶段/作业/步骤。

解决方案

要实现需求,只需在YAML中添加pr配置块,明确禁用PR触发即可。因为Azure Pipelines默认会对所有PR触发管道,所以需要显式关闭PR触发逻辑,确保只有master分支的推送提交才会触发管道。

修改后的完整YAML如下:

trigger:
  branches:
    include:
    - master

# 禁用PR触发,确保仅在master分支推送时运行管道
pr: none

resources:
  - repo: self

variables:
  - name: imageName
    value: sample
  - group: containerReg

stages:
  - stage: Build
    displayName: Build image
    jobs:
      - job: Build
        displayName: Build
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          # Build the container image
          - task: Docker@2
            inputs:
              containerRegistry: 'GHCR service connection'
              repository: '$(registryUsername)/$(imageName)'
              command: 'build'
              Dockerfile: '**/Dockerfile'
              tags: |
                $(Build.BuildId)
                latest
            displayName: Build and tag container image

          # Login to containter registry
          - task: Docker@2
            inputs:
              containerRegistry: 'GHCR service connection'
              command: 'login'
            displayName: Login to container registry

          # Push all tagged images to the container registry
          - task: Docker@2
            inputs:
              containerRegistry: 'GHCR service connection'
              repository: '$(registryUsername)/$(imageName)'
              command: 'push'
              tags: |
                $(Build.BuildId)
                latest
            displayName: Push all tagged images to the container registry

说明

  • 添加pr: none后,任何PR(包括针对master的PR)都不会触发管道,只有当PR合并到master分支产生的推送提交,才会触发trigger配置中的master分支规则,符合需求。
  • 这种方式直接阻止管道启动,而非跳过后续阶段/作业,完全满足“阻止整个管道运行”的要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 04:35:56