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

GitHub Actions问题:合并未通过检查的PR后状态查询异常

问题:GitHub Action获取提交状态返回pending,但实际存在失败检查

我正在配置一个GitHub Action,目标是在合并未通过所有状态检查的PR时触发告警。当前的Action配置如下:

name: Alert Merged Failed Checks
on: 
  push:
    branches:
      - main
      - develop
jobs:
  alert-if-merged-failed-checks:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      # Get the commit that checks were run on (non-merge commit)
      - name: Get commit 
        id: get_commit
        run: |
          commit_hash=$(git log -n 1 ${{ github.event.after }}^2 --format='%H')
          echo "commit_hash=$commit_hash" >> $GITHUB_OUTPUT
          echo "commit_hash=$commit_hash"
      # Get the status of the commit
      - name: GitHub API Request
        uses: octokit/request-action@v2.1.7
        id: get_check_status
        with:
          route: GET /repos/{owner}/{repo}/commits/{ref}/status
          owner: ***
          repo: ***
          ref: ${{ steps.get_commit.outputs.commit_hash }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - run: "echo check status: ${{ steps.get_check_status.outputs.data }}"
      - run: "echo help"

但GitHub API请求返回的结果是:

{
    "state": "pending",
    "statuses": [],
    "total_count": 0,
    ...
}

然而在GitHub上查看同一SHA的提交时,明明显示该提交存在检查失败的情况。根据GitHub官方文档,我期望返回的state值应为"failure",想知道问题原因和解决方案。


原因分析

你使用的GET /repos/{owner}/{repo}/commits/{ref}/status是旧版Commit Status API,它仅返回通过传统方式创建的commit statuses(比如第三方CI工具通过Status API上报的状态)。而GitHub Actions运行的检查、PR里的默认检查(比如代码扫描、分支保护规则里的检查)都属于GitHub Checks,这类检查的结果需要通过新版的Checks API来获取,旧版Status API无法读取到这些数据,所以会返回pending且statuses为空。


解决方案

1. 替换为Checks API获取检查结果

将API路由改为GET /repos/{owner}/{repo}/commits/{ref}/check-runs,这个接口会返回该提交所有的GitHub Checks运行结果。

2. 修改Action配置示例

调整后的关键步骤如下:

- name: GitHub API Request (Checks API)
  uses: octokit/request-action@v2.1.7
  id: get_check_runs
  with:
    route: GET /repos/{owner}/{repo}/commits/{ref}/check-runs
    owner: ***
    repo: ***
    ref: ${{ steps.get_commit.outputs.commit_hash }}
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Check for failed checks
  run: |
    # 解析check-runs结果,检查是否有状态为failed的检查
    failed_checks=$(echo '${{ steps.get_check_runs.outputs.data }}' | jq '.check_runs[] | select(.status == "completed" and .conclusion == "failed")')
    if [ -n "$failed_checks" ]; then
      echo "发现未通过的检查,触发告警"
      # 这里可以添加告警逻辑,比如发送Slack通知、创建Issue等
      exit 1
    else
      echo "所有检查均通过"
    fi

3. 验证commit_hash的正确性

确保你获取的commit_hash是PR合并前的实际提交(而非合并提交)。可以通过以下命令验证:

git log --oneline -n 3 ${{ github.event.after }}

确认输出中的第二个提交是PR里的目标提交,若${{ github.event.after }}^2不正确,可以改用${{ github.event.after }}^(合并提交的第一个父提交)尝试。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 20:27:41