|
| 1 | +/** |
| 2 | + * This script downloads mxd executable from github actions |
| 3 | + * Original repo: github.com/koitococo/mxlite |
| 4 | + * |
| 5 | + * To use this script, set the following environment variables: |
| 6 | + * - COMMIT: The commit hash of the build you want to download. |
| 7 | + * - PLATFORM: The platform of the build you want to download (windows, linux-gnu, linux-musl, darwin). |
| 8 | + * - ARCH: The architecture of the build you want to download (x86_64, aarch64). |
| 9 | + * - GITHUB_TOKEN: A GitHub token with permissions to access actions. |
| 10 | + * |
| 11 | + * If the artifact is found, it will be downloaded as 'artifact.zip'. |
| 12 | + * If no artifact is found, it will log an error message and exit with code 1. |
| 13 | + * |
| 14 | + * To run this script, ensure you have the octokit package installed: |
| 15 | + * |
| 16 | + * ```bash |
| 17 | + * npm install octokit |
| 18 | + * ``` |
| 19 | + */ |
| 20 | + |
| 21 | +import { writeFile } from 'node:fs/promises' |
| 22 | +import { exit } from 'node:process' |
| 23 | + |
| 24 | +import { Octokit } from 'octokit' |
| 25 | + |
| 26 | +const commit = process.env.COMMIT || exit(2) |
| 27 | +const platform = (process.env.PLATFORM || exit(2)) as 'windows' | 'linux-gnu' | 'linux-musl' | 'darwin' |
| 28 | +const arch = (process.env.ARCH || exit(2)) as 'x86_64' | 'aarch64' |
| 29 | + |
| 30 | +const octokit = new Octokit({ |
| 31 | + auth: process.env.GITHUB_TOKEN, |
| 32 | +}) |
| 33 | + |
| 34 | +console.log('Fetching workflow runs') |
| 35 | +const runs = await octokit.rest.actions.listWorkflowRunsForRepo({ |
| 36 | + owner: 'koitococo', |
| 37 | + repo: 'mxlite', |
| 38 | + // eslint-disable-next-line camelcase |
| 39 | + head_sha: commit, |
| 40 | + headers: { |
| 41 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 42 | + }, |
| 43 | +}) |
| 44 | + |
| 45 | +if (runs.data.workflow_runs.length > 0) { |
| 46 | + console.log('Fetching artifacts') |
| 47 | + const artifacts = await octokit.rest.actions.listWorkflowRunArtifacts({ |
| 48 | + owner: 'koitococo', |
| 49 | + repo: 'mxlite', |
| 50 | + // eslint-disable-next-line camelcase |
| 51 | + run_id: runs.data.workflow_runs[0]!.id, |
| 52 | + headers: { |
| 53 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 54 | + }, |
| 55 | + }) |
| 56 | + |
| 57 | + const matchedArtifact = artifacts.data.artifacts.find( |
| 58 | + (artifact) => artifact.name.match(/mxd-/) && artifact.name.includes(platform) && artifact.name.includes(arch), |
| 59 | + ) |
| 60 | + if (matchedArtifact) { |
| 61 | + console.log('Downloading artifact:', matchedArtifact.name) |
| 62 | + const response = await octokit.rest.actions.downloadArtifact({ |
| 63 | + owner: 'koitococo', |
| 64 | + repo: 'mxlite', |
| 65 | + // eslint-disable-next-line camelcase |
| 66 | + artifact_id: matchedArtifact.id, |
| 67 | + // eslint-disable-next-line camelcase |
| 68 | + archive_format: 'zip', |
| 69 | + headers: { |
| 70 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 71 | + }, |
| 72 | + }) |
| 73 | + const binary = response.data as ArrayBuffer |
| 74 | + await writeFile('artifact.zip', new Uint8Array(binary)) |
| 75 | + console.log('Artifact downloaded successfully') |
| 76 | + exit(0) |
| 77 | + } else { |
| 78 | + console.error('No matching artifact found.') |
| 79 | + } |
| 80 | +} else { |
| 81 | + console.error('No workflow runs found.') |
| 82 | + console.error(runs) |
| 83 | +} |
| 84 | +exit(1) |
0 commit comments