如何获取指定RUN_ID的GitHub Actions工作流摘要?
获取指定RUN_ID的GitHub工作流摘要
用GH CLI快速获取
直接执行命令替换<RUN_ID>即可查看摘要:
gh run view <RUN_ID> --step-summary
要保存到文件:
gh run view <RUN_ID> --step-summary > summary.md
通过GitHub REST API获取
- 先获取目标运行的所有Job ID:
curl -H "Authorization: token YOUR_TOKEN" \ "https://api.github.com/repos/OWNER/REPO/actions/runs/<RUN_ID>/jobs"
从返回的JSON里提取目标Job的id。
- 获取该Job的步骤详情,找到带
summary_url的步骤:
curl -H "Authorization: token YOUR_TOKEN" \ "https://api.github.com/repos/OWNER/REPO/actions/jobs/<JOB_ID>"
在steps数组中找到包含summary_url的条目,复制其路径。
- 请求该URL获取摘要内容:
curl -H "Authorization: token YOUR_TOKEN" \ "https://api.github.com<摘要URL路径>"
在工作流中用github-script获取
如果是在另一个GitHub Actions工作流里获取,可使用actions/github-script:
- name: Fetch workflow summary uses: actions/github-script@v6 with: script: | const targetRunId = 123456789; // 替换为目标RUN_ID const jobs = await github.rest.actions.listJobsForWorkflowRun({ owner: context.repo.owner, repo: context.repo.repo, run_id: targetRunId }); for (const job of jobs.data.jobs) { const jobDetails = await github.rest.actions.getJobForWorkflowRun({ owner: context.repo.owner, repo: context.repo.repo, job_id: job.id }); for (const step of jobDetails.data.steps) { if (step.summary_url) { const summaryRes = await github.request(step.summary_url); console.log("工作流摘要内容:\n", summaryRes.data); return summaryRes.data; } } }
内容的提问来源于stack exchange,提问作者Vitaly Karasik DevOps
相关产品推荐
相关产品推荐

