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

如何在GitHub Action中自动创建含失败作业日志的Issue?

解决GitHub Action失败时自动创建带日志Issue的可行方案

方案一:通过GitHub API匹配矩阵作业ID并获取日志

利用GitHub Context和API,无需手动提取Job ID,自动匹配当前失败的矩阵作业并拉取日志:

jobs:
  test-matrix-job:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16, 18]
        os: [ubuntu-latest, windows-latest]
    steps:
      - name: 执行测试任务
        run: exit 1 # 模拟作业失败
      - name: 创建失败Issue
        if: ${{ failure() }}
        uses: actions/github-script@v7
        with:
          script: |
            // 获取当前运行的所有作业列表
            const jobsList = await github.rest.actions.listJobsForWorkflowRun({
              owner: context.repo.owner,
              repo: context.repo.repo,
              run_id: context.runId
            });
            // 匹配当前失败的作业(包含矩阵参数的作业名称)
            const targetJob = jobsList.data.jobs.find(job => job.name === context.job);
            if (!targetJob) {
              console.log("未找到当前作业的日志信息");
              return;
            }
            // 拉取作业日志
            const jobLogs = await github.rest.actions.getJobLogs({
              owner: context.repo.owner,
              repo: context.repo.repo,
              job_id: targetJob.id
            });
            // 格式化日志内容(截断过长部分避免Issue超限)
            const formattedLogs = jobLogs.data.length > 10000 
              ? `${jobLogs.data.slice(0, 10000)}\n\n[日志过长已截断,可点击运行链接查看完整日志]`
              : jobLogs.data;
            // 创建Issue
            await github.rest.issues.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              title: `作业失败: ${context.job} (运行ID: ${context.runId})`,
              body: `### 作业失败详情\n运行链接: https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}\n\n### 失败日志\n\`\`\`\n${formattedLogs}\n\`\`\``
            });

方案二:用GitHub CLI结合JSON过滤获取失败日志

无需单独处理Job ID,直接通过运行ID过滤失败作业并提取日志:

jobs:
  test-job:
    runs-on: ubuntu-latest
    steps:
      - name: 执行测试
        run: exit 1
      - name: 创建失败Issue
        if: ${{ failure() }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          RUN_ID: ${{ github.run_id }}
          JOB_NAME: ${{ github.job }}
        run: |
          # 过滤当前失败作业的日志(依赖jq,Ubuntu runner默认预装)
          FAILURE_LOGS=$(gh run view $RUN_ID --log-failed --json jobs | jq -r --arg job "$JOB_NAME" '.jobs[] | select(.name == $job) | .logs')
          # 截断过长日志
          TRUNCATED_LOGS=$(echo "$FAILURE_LOGS" | head -c 10000)
          # 生成Issue内容
          ISSUE_BODY="### 作业失败: $JOB_NAME\n运行链接: https://github.com/${GITHUB_REPOSITORY}/actions/runs/$RUN_ID\n\n### 失败日志\n\`\`\`\n$TRUNCATED_LOGS\n\`\`\`"
          # 创建Issue
          gh issue create --title "作业失败: $JOB_NAME (运行ID: $RUN_ID)" --body "$ISSUE_BODY"

方案三:直接捕获步骤输出日志

如果只需要特定步骤的日志,可以在步骤中把输出保存到文件,创建Issue时读取:

jobs:
  test-job:
    runs-on: ubuntu-latest
    steps:
      - name: 执行测试并保存日志
        id: test-step
        run: |
          # 将命令输出同时写入控制台和日志文件
          your-test-command 2>&1 | tee test-output.log
      - name: 创建失败Issue
        if: ${{ failure() }}
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const logContent = fs.readFileSync('test-output.log', 'utf8').slice(0, 10000);
            await github.rest.issues.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              title: `作业失败: ${context.job} (运行ID: ${context.runId})`,
              body: `### 测试步骤失败日志\n\`\`\`\n${logContent}\n\`\`\`\n完整运行链接: https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`
            });

本地gh run view --log-failed返回空的解决办法

  1. 确保已通过gh auth login登录并拥有目标仓库的访问权限
  2. 确认指定的运行ID对应作业确实存在失败步骤
  3. 尝试添加--verbose参数查看详细请求日志,排查权限或网络问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 00:52:40