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

如何通过GitHub Actions工作流禁用Merge pull request按钮

解决GitHub PR合并按钮未按预期禁用的问题

你开启的「Require pull request reviews before merging」和「Require status checks to pass before merging」本身没问题,但按钮没禁用的核心原因是:你自定义的GitHub Actions工作流没被添加到「必填状态检查」列表里,或者工作流的状态没正确关联到PR的合并条件。

下面给两种实现方案,优先推荐原生无第三方包的方式:

方案一:原生GitHub Actions实现(无第三方包)

直接创建一个包含所有必要检查的工作流,然后把它设为PR合并的必填条件,这样只要工作流处于「运行中」或「失败」状态,合并按钮就会被锁定。

  1. 创建工作流文件
    在仓库的.github/workflows/目录下新建block-merge.yml,内容如下:
name: 检查PR可合并状态
on:
  # 触发时机:PR打开、代码同步、重新打开时
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  pr-validation:
    runs-on: ubuntu-latest
    steps:
      - name: 拉取代码
        uses: actions/checkout@v4

      # 替换成你的实际检查步骤,比如安装依赖、跑测试、构建等
      - name: 安装依赖
        run: npm install # 可替换为pip install、yarn install等
      
      - name: 运行测试
        run: npm test # 替换为你的测试命令
      
      - name: 构建项目
        run: npm run build # 替换为你的构建命令
  1. 添加到必填状态检查
  • 进入仓库「Settings」→「Branches」,找到你要保护的分支规则(比如main)
  • 在「Require status checks to pass before merging」区域,点击「Add status check」
  • 找到刚才创建的工作流名称「检查PR可合并状态」,选中后保存设置

完成后,只要这个工作流的任务在运行或失败,PR的合并按钮就会被禁用;只有当工作流成功,且PR通过审核时,按钮才会解锁。

方案二:用第三方脚本实现更精细控制

如果需要监控所有工作流的状态(比如多个独立job),可以用actions/github-script来调用GitHub API,自定义合并逻辑:

创建.github/workflows/enforce-merge-block.yml:

name: 强制锁定PR合并
on:
  pull_request:
    types: [opened, synchronize, reopened]
  check_suite:
    types: [completed, in_progress]

jobs:
  enforce-block:
    runs-on: ubuntu-latest
    steps:
      - name: 检查所有任务状态并控制合并权限
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            // 获取PR编号
            const prNumber = context.payload.pull_request?.number || context.payload.check_suite.pull_requests[0].number;
            // 获取当前分支的所有运行中检查
            const { data: inProgressChecks } = await github.rest.checks.listForRef({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref: context.payload.pull_request?.head.sha || context.payload.check_suite.head_sha,
              status: 'in_progress'
            });
            // 获取所有已完成的检查
            const { data: completedChecks } = await github.rest.checks.listForRef({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref: context.payload.pull_request?.head.sha || context.payload.check_suite.head_sha,
              status: 'completed'
            });

            // 判断是否有失败或运行中的检查
            const hasFailed = completedChecks.check_runs.some(check => check.conclusion === 'failure');
            const hasRunning = inProgressChecks.check_runs.length > 0;

            if (hasFailed || hasRunning) {
              // 锁定PR,禁止合并
              await github.rest.pulls.update({
                owner: context.repo.owner,
                repo: context.repo.repo,
                pull_number: prNumber,
                mergeable_state: 'blocked'
              });
            } else {
              // 解锁PR,允许合并
              await github.rest.pulls.update({
                owner: context.repo.owner,
                repo: context.repo.repo,
                pull_number: prNumber,
                mergeable_state: 'clean'
              });
            }

同样需要把这个工作流添加到「必填状态检查」列表中,这样它会实时监控所有任务的状态,只要有任何任务在运行或失败,就会锁定合并按钮。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 00:05:15