|
| 1 | +import * as core from "@actions/core"; |
| 2 | +import * as github from "@actions/github"; |
| 3 | + |
| 4 | +interface Inputs { |
| 5 | + github: ReturnType<typeof github.getOctokit>; |
| 6 | + context: typeof github.context; |
| 7 | + core: typeof core; |
| 8 | +} |
| 9 | + |
| 10 | +export default async ({github, context, core}: Inputs) => { |
| 11 | + const token = process.env.GITHUB_TOKEN; |
| 12 | + const commitSha = process.env.BASE_SHA; |
| 13 | + const artifactName = process.env.ARTIFACT_NAME; |
| 14 | + const workflowName = process.env.WORKFLOW_NAME; |
| 15 | + |
| 16 | + // Get all workflow runs for this commit |
| 17 | + const runs = await github.rest.actions.listWorkflowRunsForRepo({ |
| 18 | + owner: context.repo.owner, |
| 19 | + repo: context.repo.repo, |
| 20 | + per_page: 20, |
| 21 | + event: "push", |
| 22 | + head_sha: commitSha |
| 23 | + }); |
| 24 | + |
| 25 | + if (runs.data.workflow_runs.length === 0) { |
| 26 | + core.setFailed(`No workflow runs found for commit ${commitSha}`); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const workflowRuns = runs.data.workflow_runs; |
| 31 | + if (workflowRuns.length === 0) { |
| 32 | + core.setFailed(`No workflow runs found for commit ${commitSha}`); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + let found = false; |
| 37 | + for (const run of workflowRuns) { |
| 38 | + if (run.status !== "completed" || run.conclusion !== "success") { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + if (run.name !== workflowName) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + const artifactsResp = await github.rest.actions.listWorkflowRunArtifacts({ |
| 47 | + owner: context.repo.owner, |
| 48 | + repo: context.repo.repo, |
| 49 | + run_id: run.id, |
| 50 | + }); |
| 51 | + |
| 52 | + const artifact = artifactsResp.data.artifacts.find(a => a.name === artifactName); |
| 53 | + if (artifact) { |
| 54 | + core.setOutput("artifact_id", artifact.id.toString()); |
| 55 | + core.setOutput("run_id", run.id.toString()); |
| 56 | + found = true; |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (!found) { |
| 62 | + core.setFailed(`No artifact named '${artifactName}' found for commit ${commitSha}`); |
| 63 | + } |
| 64 | +} |
0 commit comments