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

GitHub Actions复用工作流:确认评论触发成功后原PR检查仍失败

解决GitHub Actions PR检查状态不更新的问题

问题原因

PR触发的Check and Run工作流和评论触发的Rerun On Confirm Comment是两个完全独立的工作流实例。PR的检查状态只会关联最初触发的那个失败实例,所以即使新的工作流运行成功,原PR的检查状态依然会显示失败。

无需Checks API的简便解决方案

方案1:合并工作流逻辑,在同一个实例中完成确认与执行

调整原PR工作流,加入等待确认的环节,让整个流程在同一个工作流实例中完成,这样实例成功后PR检查状态会自动更新。

修改后的Check and Run工作流示例:

name: Check and Run
on:
  pull_request:
    types: [opened, synchronize]
  issue_comment:
    types: [created]

jobs:
  pre_check:
    runs-on: ubuntu-latest
    outputs:
      need_confirm: ${{ steps.check_setup.outputs.need_confirm }}
    steps:
      - name: Verify manual setup completion
        id: check_setup
        run: |
          # 替换为你的前置检查逻辑,判断是否需要手动确认
          # 示例:如果某个文件不存在则需要确认
          if [ ! -f "setup_done.txt" ]; then
            echo "need_confirm=true" >> $GITHUB_OUTPUT
          else
            echo "need_confirm=false" >> $GITHUB_OUTPUT
          fi

  wait_for_approval:
    needs: pre_check
    if: |
      (github.event_name == 'pull_request' && needs.pre_check.outputs.need_confirm == 'true')
    environment:
      name: pr-manual-confirm
      url: ${{ github.event.pull_request.html_url }}
    runs-on: ubuntu-latest
    steps:
      - name: Await manual approval
        run: echo "Ready to proceed after manual approval via environment settings."

  execute_plan:
    needs: [pre_check, wait_for_approval]
    if: |
      (needs.pre_check.outputs.need_confirm == 'false') ||
      (github.event_name == 'issue_comment' && contains(github.event.comment.body, '!confirm'))
    uses: ./.github/workflows/plan.yaml
    secrets: inherit
    with:
      # 传入你的参数
  • 当PR触发时,先执行前置检查,若需要确认则进入环境审批环节(你可以在仓库的「Settings > Environments」中配置手动审批人)。
  • 审批通过后,同一个工作流实例会继续执行plan.yaml,完成后PR检查状态会自动更新为成功。
  • 也可以把环境审批替换为监听!confirm评论的逻辑(用actions/github-script实现),更贴合你原有的评论触发习惯。

方案2:用GitHub CLI重新运行原PR的失败工作流实例

在评论触发的工作流中,添加步骤调用GitHub CLI,重新运行原PR对应的失败工作流实例。原实例成功后,PR的检查状态会自动更新。

修改后的Rerun On Confirm Comment工作流示例:

name: Rerun On Confirm Comment
on:
  issue_comment:
    types: [created]
jobs:
  do_checks_and_work_if_true:
    if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '!confirm')
    runs-on: ubuntu-latest
    steps:
      - name: Run plan workflow
        uses: ./.github/workflows/plan.yaml
        secrets: inherit
        with:
          # 传入你的参数

      - name: Rerun original failed PR workflow
        if: success()
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # 获取PR对应的最新失败的Check and Run工作流实例ID
          RUN_ID=$(gh run list --repo ${{ github.repository }} \
            --event pull_request \
            --branch ${{ github.event.pull_request.head.ref }} \
            --status failure \
            --limit 1 \
            --json id \
            --jq '.[0].id')
          
          # 重新运行该实例
          if [ -n "$RUN_ID" ]; then
            gh run rerun $RUN_ID
          else
            echo "No failed workflow run found for this PR."
          fi
  • 确保仓库的GITHUB_TOKEN有workflows权限(默认权限已足够)。
  • 当评论触发的工作流成功执行plan.yaml后,会自动重新运行原PR的失败工作流实例,原实例成功后PR检查状态会更新为成功。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 00:56:23