|
| 1 | +module.exports = async ({ github, context, core }) => { |
| 2 | + const runID = process.env.WORKFLOW_RUN_ID; |
| 3 | + |
| 4 | + core.info(`Checking failed workflow run: ${runID}`); |
| 5 | + |
| 6 | + const { data: workflowRun } = await github.rest.actions.getWorkflowRun({ |
| 7 | + owner: context.repo.owner, |
| 8 | + repo: context.repo.repo, |
| 9 | + run_id: runID |
| 10 | + }); |
| 11 | + |
| 12 | + core.info(`Workflow run status: ${workflowRun.status}`); |
| 13 | + core.info(`Workflow run conclusion: ${workflowRun.conclusion}`); |
| 14 | + |
| 15 | + const { data: jobs } = await github.rest.actions.listJobsForWorkflowRun({ |
| 16 | + owner: context.repo.owner, |
| 17 | + repo: context.repo.repo, |
| 18 | + run_id: runID |
| 19 | + }); |
| 20 | + |
| 21 | + core.info(`Found ${jobs.jobs.length} jobs in the workflow run`); |
| 22 | + |
| 23 | + const failedJobs = jobs.jobs.filter(job => job.conclusion === 'failure'); |
| 24 | + |
| 25 | + if (failedJobs.length === 0) { |
| 26 | + core.info('No failed jobs found to retry'); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + core.info(`Found ${failedJobs.length} failed jobs to analyze:`); |
| 31 | + |
| 32 | + function isRetryableError(errorMessage) { |
| 33 | + if (!errorMessage) return false; |
| 34 | + return errorMessage.includes('The self-hosted runner lost communication with the server.'); |
| 35 | + } |
| 36 | + |
| 37 | + const jobsToRetry = []; |
| 38 | + |
| 39 | + for (const job of failedJobs) { |
| 40 | + core.info(`Analyzing job: ${job.name} (ID: ${job.id})`); |
| 41 | + |
| 42 | + try { |
| 43 | + const { data: jobDetails } = await github.rest.actions.getJobForWorkflowRun({ |
| 44 | + owner: context.repo.owner, |
| 45 | + repo: context.repo.repo, |
| 46 | + job_id: job.id |
| 47 | + }); |
| 48 | + |
| 49 | + core.info(`Job status: ${jobDetails.status}, conclusion: ${jobDetails.conclusion}`); |
| 50 | + |
| 51 | + const { data: annotations } = await github.rest.checks.listAnnotations({ |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + check_run_id: jobDetails.check_run_url.split('/').pop() |
| 55 | + }); |
| 56 | + |
| 57 | + core.info(`Found ${annotations.length} annotations for job: ${job.name}`); |
| 58 | + |
| 59 | + const errorAnnotations = annotations.filter(annotation => |
| 60 | + annotation.annotation_level === 'failure' || |
| 61 | + annotation.annotation_level === 'warning' || |
| 62 | + annotation.message.toLowerCase().includes('error') || |
| 63 | + annotation.message.toLowerCase().includes('failed') || |
| 64 | + annotation.message.toLowerCase().includes('timeout') || |
| 65 | + annotation.message.toLowerCase().includes('connection') || |
| 66 | + annotation.message.toLowerCase().includes('runner') |
| 67 | + ); |
| 68 | + |
| 69 | + core.info(`Found ${errorAnnotations.length} error-related annotations:`); |
| 70 | + errorAnnotations.forEach(annotation => { |
| 71 | + core.info(` [${annotation.annotation_level}] ${annotation.message}`); |
| 72 | + }); |
| 73 | + |
| 74 | + const allAnnotationMessages = annotations.map(annotation => annotation.message).join(''); |
| 75 | + |
| 76 | + const isRetryable = isRetryableError(allAnnotationMessages); |
| 77 | + |
| 78 | + if (isRetryable) { |
| 79 | + core.info(`✅ Job "${job.name}" is retryable - infrastructure issue detected in annotations`); |
| 80 | + jobsToRetry.push(job); |
| 81 | + } else { |
| 82 | + core.info(`❌ Job "${job.name}" is NOT retryable - likely a code/test issue based on annotations`); |
| 83 | + } |
| 84 | + |
| 85 | + } catch (error) { |
| 86 | + core.error(`Failed to analyze job ${job.name}:`, error.message); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (jobsToRetry.length === 0) { |
| 91 | + core.info('No jobs found with retryable errors. Skipping retry.'); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + core.info(`Retrying ${jobsToRetry.length} jobs with retryable errors:`); |
| 96 | + jobsToRetry.forEach(job => { |
| 97 | + core.info(`- ${job.name} (ID: ${job.id})`); |
| 98 | + }); |
| 99 | + |
| 100 | + for (const job of jobsToRetry) { |
| 101 | + try { |
| 102 | + core.info(`Retrying job: ${job.name} (ID: ${job.id})`); |
| 103 | + |
| 104 | + await github.rest.actions.reRunJob({ |
| 105 | + owner: context.repo.owner, |
| 106 | + repo: context.repo.repo, |
| 107 | + job_id: job.id |
| 108 | + }); |
| 109 | + |
| 110 | + core.info(`✅ Successfully initiated retry for job: ${job.name}`); |
| 111 | + } catch (error) { |
| 112 | + core.error(`❌ Failed to retry job ${job.name}:`, error.message); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + core.info('Retry process completed'); |
| 117 | +}; |
0 commit comments