Repository navigation
Collect CI Failures #124
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Collect CI Failures | |
| on: | |
| workflow_run: | |
| workflows: ['Continuous integration'] # List your CI workflow names | |
| types: [completed] | |
| permissions: | |
| issues: write | |
| jobs: | |
| collect-failures: | |
| runs-on: ubuntu-slim | |
| if: failure() || github.event.workflow_run.conclusion == 'failure' | |
| steps: | |
| - name: Comment on issue with failures | |
| env: | |
| ISSUE_NUMBER: '2456' # Change to your tracking issue number | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const issueNumber = process.env.ISSUE_NUMBER; | |
| // Get the workflow run that triggered this workflow | |
| const workflowRun = context.payload.workflow_run; | |
| // List all jobs for the workflow run | |
| const jobs = await github.rest.actions.listJobsForWorkflowRun({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: workflowRun.id, | |
| }); | |
| // Filter for failed jobs | |
| const failedJobs = jobs.data.jobs.filter(job => job.conclusion === 'failure'); | |
| if (failedJobs.length === 0) { | |
| console.log('No failed jobs found'); | |
| return; | |
| } | |
| // Build the comment | |
| const failedJobsList = failedJobs | |
| .map(job => `- [${job.name}](${job.html_url})`) | |
| .join('\n'); | |
| const comment = `## CI Failure Report | |
| **Workflow:** ${workflowRun.name} | |
| **Commit:** [${workflowRun.head_sha.substring(0, 7)}](${workflowRun.html_url}/commit/${workflowRun.head_sha}) | |
| **Time:** ${new Date(workflowRun.created_at).toISOString()} | |
| ### Failed Jobs | |
| ${failedJobsList} | |
| [View full workflow run](${workflowRun.html_url}?attempt=${workflowRun.run_attempt})`; | |
| // Post the comment | |
| const commentResponse = await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: parseInt(issueNumber), | |
| body: comment, | |
| }); | |
| const commentId = commentResponse.data.id; | |
| const commentUrl = `${context.payload.repository.html_url}/issues/${issueNumber}#issuecomment-${commentId}`; | |
| const summary = `${comment} | |
| See ${commentUrl}`; | |
| // Append the comment to the summary as well | |
| fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, summary); | |