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

使用octokit向指定workflow-run/check-suite添加自定义check的问题求解

GitHub Actions多工作流并行时自定义Check绑定异常解决方案

问题根因

GitHub Checks API在未显式指定check_suite_id参数时,会根据传入的head_sha自动匹配该SHA下最早创建的Check Suite,因此单次Push触发多个工作流时,所有后续创建的自定义Check都会被绑定到第一个触发的工作流对应的Check Suite下。如果多个Check使用完全相同的name,后创建的Check还会直接覆盖同SHA下的同名Check,不会生成新条目。

解决方法

你可以通过两种方式解决绑定问题,按需选择即可:

方案一:使用工作流默认GITHUB_TOKEN

如果场景允许,直接将你当前传入的自定义token替换为工作流默认的${{ secrets.GITHUB_TOKEN }},GitHub会自动将当前步骤创建的Check绑定到所属的工作流Run下,无需额外调整其他参数。

方案二:显式指定绑定的Check Suite ID

如果必须使用自定义token,你可以先查询当前工作流对应的Check Suite ID,创建Check时显式传入该ID即可,同时要保证每个Check的名称唯一,避免覆盖。修改后的代码示例如下:

- uses: actions/github-script@v4
  with:
    github-token: ${{ inputs.github-token }}
    script: |
      const date = new Date();
      // 查询当前SHA对应的所有GitHub Actions生成的Check Suite
      const suites = await github.checks.listSuitesForRef({
        owner: "${{ steps.vars.outputs.owner }}",
        repo: "${{ steps.vars.outputs.repo }}",
        ref: "${{ inputs.sha }}",
        app_id: 15368,
      });
      // 匹配当前工作流Run对应的Check Suite
      const currentSuite = suites.data.check_suites.find(suite => 
        suite.latest_check_runs?.some(run => run.external_id === "${{ github.run_id }}")
      );
      const check = await github.checks.create({
        owner: "${{ steps.vars.outputs.owner }}",
        repo: "${{ steps.vars.outputs.repo }}",
        name: "Custom Script ${{ github.run_id }}",
        started_at: date.toISOString(),
        completed_at: date.toISOString(),
        head_sha: "${{ inputs.sha }}",
        check_suite_id: currentSuite.id,
        external_id: "${{ github.run_id }}",
        details_url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
        status: "completed",
        conclusion: "success",
        output: {
          title: "Some funny title",
          summary: "Build successful",
          text: "Image pushed to https://${{ inputs.region }}.console.aws.amazon.com/ecr/repositories/private/${{ inputs.customer-id }}/modix/base/${{ inputs.image }}"
        }
      });

注:代码中的15368是GitHub Actions官方应用的固定ID,无需修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 16:30:05