|
| 1 | +import type * as Core from '@actions/core'; |
| 2 | +import { type Octokit } from '@octokit/rest'; |
| 3 | +import { type context as Context } from '@actions/github'; |
| 4 | + |
| 5 | +import * as PackageJSON from '../package.json' with { type: 'json' }; |
| 6 | + |
| 7 | +type Workflow = { |
| 8 | + id: number; |
| 9 | + name: string; |
| 10 | + path: string; |
| 11 | +}; |
| 12 | + |
| 13 | +type WorkflowRun = { |
| 14 | + id: number; |
| 15 | + status: string; |
| 16 | + conclusion: string | null; |
| 17 | + html_url: string; |
| 18 | +}; |
| 19 | + |
| 20 | +export async function main({ github, context, core }: { github: Octokit, context: typeof Context, core: typeof Core }) { |
| 21 | + core.info(`🏃 Execute Workflow Action v${PackageJSON.version}`); |
| 22 | + try { |
| 23 | + const workflowFileName = core.getInput('workflow'); |
| 24 | + |
| 25 | + const inputsJson = core.getInput('inputs'); |
| 26 | + const inputs = inputsJson ? JSON.parse(inputsJson) : {}; |
| 27 | + |
| 28 | + const { owner, repo } = context.repo; |
| 29 | + const { ref } = context; |
| 30 | + |
| 31 | + const workflows: Workflow[] = await github.paginate( |
| 32 | + github.rest.actions.listRepoWorkflows.endpoint.merge({ |
| 33 | + owner, |
| 34 | + repo, |
| 35 | + }), |
| 36 | + ); |
| 37 | + |
| 38 | + const workflowPath = `.github/workflows/${workflowFileName}`; |
| 39 | + const foundWorkflow = workflows.find((workflow) => workflow.path === workflowPath); |
| 40 | + |
| 41 | + if (!foundWorkflow) throw new Error(`Unable to find workflow '${workflowPath}' in ${owner}/${repo} 😥`); |
| 42 | + |
| 43 | + core.info( |
| 44 | + `🔎 Found workflow, id: ${foundWorkflow.id}, name: ${foundWorkflow.name}, path: ${foundWorkflow.path}`, |
| 45 | + ); |
| 46 | + |
| 47 | + // Get current workflow runs before dispatching |
| 48 | + const runsBefore = await github.rest.actions.listWorkflowRuns({ |
| 49 | + owner, |
| 50 | + repo, |
| 51 | + workflow_id: foundWorkflow.id, |
| 52 | + per_page: 5, |
| 53 | + }); |
| 54 | + |
| 55 | + // Call workflow_dispatch API |
| 56 | + core.info('🚀 Calling GitHub API to dispatch workflow...'); |
| 57 | + await github.request(`POST /repos/${owner}/${repo}/actions/workflows/${foundWorkflow.id}/dispatches`, { |
| 58 | + ref, |
| 59 | + inputs, |
| 60 | + }); |
| 61 | + |
| 62 | + // Wait for the new run to appear |
| 63 | + core.info('⏳ Waiting for workflow run to start...'); |
| 64 | + let workflowRun: WorkflowRun | undefined; |
| 65 | + const maxWaitTime = 60000; // 60 seconds |
| 66 | + const pollInterval = 1000; // 1 second |
| 67 | + const startTime = Date.now(); |
| 68 | + |
| 69 | + while (!workflowRun && Date.now() - startTime < maxWaitTime) { |
| 70 | + await new Promise((resolve) => setTimeout(resolve, pollInterval)); |
| 71 | + |
| 72 | + const runsAfter = await github.rest.actions.listWorkflowRuns({ |
| 73 | + owner, |
| 74 | + repo, |
| 75 | + workflow_id: foundWorkflow.id, |
| 76 | + per_page: 5, |
| 77 | + }); |
| 78 | + |
| 79 | + // Find the new run (one that wasn't in the before list) |
| 80 | + const newRun = runsAfter.data.workflow_runs.find( |
| 81 | + (run) => !runsBefore.data.workflow_runs.some((oldRun) => oldRun.id === run.id), |
| 82 | + ); |
| 83 | + |
| 84 | + if (newRun) { |
| 85 | + workflowRun = newRun as WorkflowRun; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (!workflowRun) { |
| 90 | + throw new Error('Timeout waiting for workflow run to start'); |
| 91 | + } |
| 92 | + |
| 93 | + core.info(`✅ Workflow run started: ${workflowRun.html_url}`); |
| 94 | + core.setOutput('workflowRunId', workflowRun.id); |
| 95 | + |
| 96 | + // Poll until the workflow completes |
| 97 | + core.info('⏳ Waiting for workflow run to complete...'); |
| 98 | + while (workflowRun.status !== 'completed') { |
| 99 | + await new Promise((resolve) => setTimeout(resolve, pollInterval)); |
| 100 | + |
| 101 | + const runStatus = await github.rest.actions.getWorkflowRun({ |
| 102 | + owner, |
| 103 | + repo, |
| 104 | + run_id: workflowRun.id, |
| 105 | + }); |
| 106 | + |
| 107 | + workflowRun = runStatus.data as WorkflowRun; |
| 108 | + core.info(`📊 Status: ${workflowRun.status}`); |
| 109 | + } |
| 110 | + |
| 111 | + core.info(`🏁 Workflow run completed with conclusion: ${workflowRun.conclusion}`); |
| 112 | + core.setOutput('conclusion', workflowRun.conclusion); |
| 113 | + |
| 114 | + if (workflowRun.conclusion !== 'success') { |
| 115 | + throw new Error(`Workflow run failed with conclusion: ${workflowRun.conclusion}`); |
| 116 | + } |
| 117 | + } catch (error) { |
| 118 | + const e = error as Error; |
| 119 | + |
| 120 | + if (e.message.endsWith('a disabled workflow')) { |
| 121 | + core.warning('Workflow is disabled, no action was taken'); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + core.setFailed(e.message); |
| 126 | + } |
| 127 | +} |
0 commit comments