-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.mjs
More file actions
328 lines (302 loc) · 10 KB
/
Copy pathaction.mjs
File metadata and controls
328 lines (302 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// @ts-check
import { spawnSync } from "node:child_process";
import { createHash, randomUUID } from "node:crypto";
import { appendFileSync, chmodSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { EOL } from "node:os";
import { join } from "node:path";
import { arch, env, exit, platform, stdout } from "node:process";
const REGISTRY = env.RUNNER_NPM_REGISTRY || "https://registry.npmjs.org";
const FETCH_TIMEOUT_MS = 3000;
const RETRY_BACKOFFS_MS = [250, 750];
/**
* @param {"GITHUB_PATH" | "GITHUB_OUTPUT"} name
* @param {string} block
*/
function fileCommand(name, block) {
const file = env[name];
if (!file) throw new Error(`${name} is not set, not running inside a GitHub Action?`);
appendFileSync(file, `${block}${EOL}`);
}
/** @param {string} dir */
function addPath(dir) {
fileCommand("GITHUB_PATH", dir);
}
/**
* @param {string} name
* @param {string} value
*/
function setOutput(name, value) {
const delim = `ghadelimiter_${randomUUID()}`;
if (name.includes(delim) || value.includes(delim)) {
throw new Error("output delimiter collision (astronomically unlikely, retry)");
}
fileCommand("GITHUB_OUTPUT", `${name}<<${delim}${EOL}${value}${EOL}${delim}`);
}
/**
* @param {string} s
* @returns {string}
*/
function escapeData(s) {
return s.replace(/%/g, "%25").replace(/\r/g, "%0D").replace(/\n/g, "%0A");
}
/** @param {string} title */
function startGroup(title) {
stdout.write(`::group::${escapeData(title)}${EOL}`);
}
function endGroup() {
stdout.write(`::endgroup::${EOL}`);
}
/** @param {string} msg */
function warn(msg) {
stdout.write(`::warning::${escapeData(msg)}${EOL}`);
}
/** @param {string} msg */
function debug(msg) {
stdout.write(`::debug::${escapeData(msg)}${EOL}`);
}
/**
* @param {string} file
* @param {string[]} args
* @param {"inherit" | "pipe"} stdio
* @returns {import("node:child_process").SpawnSyncReturns<string>}
*/
function run(file, args, stdio) {
const res = spawnSync(file, args, { encoding: "utf8", stdio });
if (res.error) throw res.error;
if (res.status !== 0) {
throw new Error(`\`${file} ${args.join(" ")}\` exited with ${res.status ?? "signal"}`);
}
return res;
}
/**
* @template T
* @param {() => Promise<T>} fn
* @param {number[]} backoffsMs
* @returns {Promise<T>}
*/
async function withRetry(fn, backoffsMs) {
for (let attempt = 0;; attempt++) {
try {
return await fn();
} catch (err) {
if (attempt >= backoffsMs.length) throw err;
const wait = backoffsMs[attempt];
const msg = err instanceof Error ? err.message : String(err);
debug(`attempt ${attempt + 1} failed (${msg}), retrying in ${wait}ms`);
await new Promise((resolve) => setTimeout(resolve, wait));
}
}
}
/** @returns {string} */
function resolveSpec() {
const requested = env.INPUT_VERSION || "";
if (requested === "" || requested === "latest") {
console.log("version: latest");
return "latest";
}
const m = /^v?(\d{1,9}(?:\.\d{1,9}){0,2}(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?)$/
.exec(requested);
if (!m) {
warn(`'${requested}' is not a semver pin or 'latest', falling back to 'latest'`);
return "latest";
}
console.log(`version: ${m[1]} (from '${requested}')`);
return m[1];
}
/**
* Resolve the `@runner-run/<pkg>` platform package matching this runner.
* Returns null on any unexpected, unmapped platform, unreadable manifest, or
* undetectable libc.
* @returns {{ scope: string, pkg: string } | null}
*/
function resolvePlatformTarget() {
/** @type {{ scope: string, targets: { pkg: string, os: string[], cpu: string[], libc?: string[] | null }[] }} */
let manifest;
try {
manifest = JSON.parse(readFileSync(join(import.meta.dirname, "npm", "targets.json"), "utf8"));
} catch (err) {
debug(`could not read npm/targets.json (${err instanceof Error ? err.message : String(err)})`);
return null;
}
/** @type {"glibc" | "musl" | undefined} */
let libc;
if (platform === "linux") {
try {
const report = /** @type {{ header?: { glibcVersionRuntime?: string } }} */ (process.report?.getReport?.());
libc = report?.header?.glibcVersionRuntime ? "glibc" : "musl";
} catch {
libc = undefined;
}
}
const match = manifest.targets.find((t) =>
t.os.includes(platform)
&& t.cpu.includes(arch)
&& (t.libc == null || (libc !== undefined && t.libc.includes(libc)))
);
if (!match) {
debug(`no npm/targets.json entry for ${platform}/${arch}${libc ? `/${libc}` : ""}`);
return null;
}
return { scope: manifest.scope, pkg: match.pkg };
}
/**
* @param {string} spec
* @returns {boolean}
*/
function isExactPin(spec) {
return /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(spec);
}
/**
* @param {string} a
* @param {string} b
* @returns {number}
*/
function compareVersions(a, b) {
const pa = a.split(/[.+-]/, 3).map(Number);
const pb = b.split(/[.+-]/, 3).map(Number);
for (let i = 0; i < 3; i++) {
if (pa[i] !== pb[i]) return (pa[i] || 0) - (pb[i] || 0);
}
const preA = a.includes("-");
const preB = b.includes("-");
if (preA !== preB) return preA ? -1 : 1;
return a < b ? -1 : a > b ? 1 : 0;
}
/**
* @param {string} url
* @returns {Promise<unknown>}
*/
async function getJson(url) {
const res = await fetch(url, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) });
if (!res.ok) throw new Error(`GET ${url} responded ${res.status}`);
return res.json();
}
/**
* Resolve a version spec to the concrete npm dist for the platform package.
* @param {string} pkgName
* @param {string} spec
* @returns {Promise<{ version: string, tarball: string, integrity: string | undefined }>}
*/
async function resolveDist(pkgName, spec) {
if (spec === "latest" || isExactPin(spec)) {
const manifest = /** @type {{ version: string, dist: { tarball: string, integrity?: string } }} */ (
await withRetry(() => getJson(`${REGISTRY}/${pkgName}/${encodeURIComponent(spec)}`), RETRY_BACKOFFS_MS)
);
return { version: manifest.version, tarball: manifest.dist.tarball, integrity: manifest.dist.integrity };
}
const doc = /** @type {{ versions: Record<string, { dist: { tarball: string, integrity?: string } }> }} */ (
await withRetry(() => getJson(`${REGISTRY}/${pkgName}`), RETRY_BACKOFFS_MS)
);
const prefix = `${spec}.`;
const matches = Object.keys(doc.versions ?? {}).filter((v) => v === spec || v.startsWith(prefix));
if (matches.length === 0) throw new Error(`no ${pkgName} version matching '${spec}'`);
matches.sort(compareVersions);
const version = matches[matches.length - 1];
const dist = doc.versions[version].dist;
return { version, tarball: dist.tarball, integrity: dist.integrity };
}
/**
* @param {Buffer} buf
* @param {string | undefined} integrity
* @param {string} label
*/
function verifyIntegrity(buf, integrity, label) {
if (!integrity) {
warn(`no integrity metadata for ${label}, skipping checksum`);
return;
}
const dash = integrity.indexOf("-");
const algo = integrity.slice(0, dash);
const expected = integrity.slice(dash + 1);
const actual = createHash(algo).update(buf).digest("base64");
if (actual !== expected) {
throw new Error(`integrity mismatch for ${label}: expected ${algo}-${expected}, got ${algo}-${actual}`);
}
}
/**
* @param {string} tarball
* @param {string | undefined} integrity
* @param {string} label
* @param {string} binDir
*/
async function downloadExtract(tarball, integrity, label, binDir) {
startGroup(`download ${tarball}`);
try {
const buf = await withRetry(async () => {
const res = await fetch(tarball, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) });
if (!res.ok) throw new Error(`GET ${tarball} responded ${res.status}`);
return Buffer.from(await res.arrayBuffer());
}, RETRY_BACKOFFS_MS);
verifyIntegrity(buf, integrity, label);
mkdirSync(binDir, { recursive: true });
const tgz = join(binDir, ".pkg.tgz");
writeFileSync(tgz, buf);
run("tar", ["-xzf", tgz, "-C", binDir, "--strip-components=2", "package/bin"], "inherit");
rmSync(tgz, { force: true });
if (platform !== "win32") {
for (const b of ["runner", "run"]) chmodSync(join(binDir, b), 0o755);
}
} finally {
endGroup();
}
}
/**
* @param {string} binDir
* @returns {string}
*/
function verifyVersion(binDir) {
const runner = join(binDir, platform === "win32" ? "runner.exe" : "runner");
const res = run(runner, ["--version"], "pipe");
const out = `${res.stdout ?? ""}${res.stderr ?? ""}`;
stdout.write(out.endsWith("\n") ? out : `${out}${EOL}`);
const m = /\b(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?)\b/.exec(out);
if (!m) throw new Error(`could not parse version from \`${runner} --version\`: ${out.trim()}`);
return m[1];
}
/** @returns {string} */
function toolCacheRoot() {
const toolCache = env.RUNNER_TOOL_CACHE;
if (!toolCache) throw new Error("RUNNER_TOOL_CACHE is not set, not running inside a GitHub Action?");
return toolCache;
}
/**
* @param {string} binDir
* @param {string} label
* @param {string} spec
*/
function finish(binDir, label, spec) {
const verified = verifyVersion(binDir);
if (isExactPin(spec) && verified !== spec) {
throw new Error(`requested ${label}@${spec} but runner --version reported ${verified}`);
}
const suffix = platform === "win32" ? ".exe" : "";
const runnerBin = join(binDir, `runner${suffix}`);
const runBin = join(binDir, `run${suffix}`);
addPath(binDir);
console.log(`version: ${verified}`);
console.log(`bin-dir: ${binDir}`);
console.log(`runner-bin: ${runnerBin}`);
console.log(`run-bin: ${runBin}`);
setOutput("version", verified);
setOutput("bin-dir", binDir);
setOutput("runner-bin", runnerBin);
setOutput("run-bin", runBin);
}
async function main() {
const spec = resolveSpec();
const target = resolvePlatformTarget();
if (!target) throw new Error(`no prebuilt runner binary for ${platform}/${arch}`);
const label = `${target.scope}/${target.pkg}`;
const root = toolCacheRoot();
const dist = await resolveDist(label, spec);
const binDir = join(root, "runner-cli", dist.version, target.pkg);
await downloadExtract(dist.tarball, dist.integrity, `${label}@${dist.version}`, binDir);
finish(binDir, label, spec);
}
try {
await main();
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
stdout.write(`::error::${escapeData(msg)}${EOL}`);
exit(1);
}