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

如何在推送到prod分支时重新运行现有PR的Github Actions工作流?

解决Prod分支推送后重新运行开放PR工作流的问题

问题根源

当推送代码到prod分支时,GitHub Actions只会触发prod分支上的工作流实例,不会自动重新运行针对main分支的开放PR中的工作流——因为PR的工作流是绑定到PR的源分支(head branch),而非目标分支或prod分支。

完整解决方案

修改你的工作流,新增一个专门的Job,在prod分支推送时,通过GitHub API重新触发所有开放PR的目标工作流:

on:
  push:
    branches:
      - prod
  pull_request:
    branches:
      - main

jobs:
  # 原有检查提交差异的Job(保留你的业务逻辑)
  check-commit-diff:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0 # 必须拉取全量提交才能计算分支差异
      - name: 计算Prod与Main的提交次数差异
        run: |
          git fetch origin prod
          git fetch origin main
          # 计算prod到main的提交数
          PROD_TO_MAIN=$(git rev-list --count origin/prod..origin/main)
          # 计算main到prod的提交数
          MAIN_TO_PROD=$(git rev-list --count origin/main..origin/prod)
          echo "Prod领先Main的提交数: $MAIN_TO_PROD"
          echo "Main领先Prod的提交数: $PROD_TO_MAIN"

  # 新增Job:Prod推送时重新运行所有开放PR的工作流
  rerun-pr-workflows:
    runs-on: ubuntu-latest
    # 仅在prod分支推送时触发
    if: github.event_name == 'push' && github.ref == 'refs/heads/prod'
    # 配置必要权限
    permissions:
      actions: write    # 允许操作工作流运行
      pull-requests: read # 允许读取PR列表
    steps:
      - name: 重新触发开放PR的工作流
        uses: actions/github-script@v7
        with:
          script: |
            // 1. 获取所有目标分支为main的开放PR
            const openPRs = await github.rest.pulls.list({
              owner: context.repo.owner,
              repo: context.repo.repo,
              state: 'open',
              base: 'main'
            });

            // 2. 遍历每个PR,重新触发工作流
            for (const pr of openPRs.data) {
              console.log(`处理PR #${pr.number}: ${pr.title}`);
              
              // 尝试重新运行该PR的最新工作流实例
              const workflowRuns = await github.rest.actions.listWorkflowRunsForRepo({
                owner: context.repo.owner,
                repo: context.repo.repo,
                branch: pr.head.ref,
                workflow_id: '你的工作流文件名.yml', // 替换为实际工作流文件名(如commit-diff-check.yml)
                status: 'completed'
              });

              if (workflowRuns.data.total_count > 0) {
                // 重新运行最新的已完成工作流
                const latestRun = workflowRuns.data.workflow_runs[0];
                await github.rest.actions.reRunWorkflow({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  run_id: latestRun.id
                });
                console.log(`已重新运行PR #${pr.number}的工作流实例 #${latestRun.id}`);
              } else {
                // 如果没有历史运行记录,触发新的工作流调度
                await github.rest.actions.createWorkflowDispatch({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  workflow_id: '你的工作流文件名.yml',
                  ref: pr.head.ref
                });
                console.log(`已为PR #${pr.number}触发新的工作流运行`);
              }
            }

关键配置说明

  1. 权限设置:必须给rerun-pr-workflowsJob配置actions: write和pull-requests: read权限,否则GitHub API会返回权限不足的错误。
  2. 工作流文件名:将脚本中的你的工作流文件名.yml替换为实际的工作流文件名(比如你的工作流保存在.github/workflows/commit-diff-check.yml,就填commit-diff-check.yml)。
  3. 触发条件:通过if限制该Job仅在prod分支推送时运行,避免不必要的执行。

排查要点

  • 若之前使用github-script失败,大概率是权限不足:检查仓库的「Settings > Actions > General > Workflow permissions」是否设置为「Read and write permissions」,或者在Job中显式配置权限。
  • 确保PR的源分支(head branch)存在对应的工作流文件,否则createWorkflowDispatch会失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 11:38:25