| 1 | import { spawn } from "node:child_process"; |
| 2 | |
| 3 | /** Where the image puts GitHub's runner (Dockerfile). */ |
| 4 | export const ACTIONS_RUNNER_DIR = "/opt/actions-runner"; |
| 5 | |
| 6 | /** |
| 7 | * How long a platform rollout's SIGTERM waits before it reaches the job, as for Diffinery's own jobs |
| 8 | * (main.ts drainOnSigtermMs): Cloudflare gives a container 15 minutes after SIGTERM. |
| 9 | */ |
| 10 | export const DRAIN_ON_SIGTERM_MS = 13 * 60_000; |
| 11 | |
| 12 | export interface GitHubRunnerOptions { |
| 13 | /** The just-in-time configuration GitHub made for this runner: its credentials. Never logged. */ |
| 14 | jitConfig: string; |
| 15 | runnerDir?: string; |
| 16 | env?: NodeJS.ProcessEnv; |
| 17 | drainOnSigtermMs?: number; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * A GitHub job on a Diffinery runner (apps/diffinery/src/github-runners/): runs GitHub's own runner with |
| 22 | * its just-in-time configuration. GitHub hands it one queued job, it runs the job and exits, and the |
| 23 | * container stops. Returns the runner's exit code. |
| 24 | * |
| 25 | * Jobs run as root, as Diffinery's own hosted jobs do (the image's tools and caches are root's). SIGINT |
| 26 | * (Diffinery stopping the runner) reaches the runner at once, which cancels the job; SIGTERM (a platform |
| 27 | * rollout) waits up to drainOnSigtermMs for the job to finish first. |
| 28 | */ |
| 29 | export function runGitHubRunner(o: GitHubRunnerOptions): Promise<number> { |
| 30 | const dir = o.runnerDir ?? ACTIONS_RUNNER_DIR; |
| 31 | const env: NodeJS.ProcessEnv = { ...(o.env ?? process.env), RUNNER_ALLOW_RUNASROOT: "1" }; |
| 32 | // The job's steps inherit the runner's environment: the configuration stays out of it. |
| 33 | delete env.DIFFINERY_GITHUB_JIT_CONFIG; |
| 34 | const child = spawn(`${dir}/run.sh`, ["--jitconfig", o.jitConfig], { cwd: dir, env, stdio: ["ignore", "inherit", "inherit"] }); |
| 35 | let draining: NodeJS.Timeout | null = null; |
| 36 | const forward = (sig: NodeJS.Signals) => { |
| 37 | if (child.exitCode === null) child.kill(sig); |
| 38 | }; |
| 39 | const onInt = () => forward("SIGINT"); |
| 40 | const onTerm = () => { |
| 41 | if (draining) return forward("SIGTERM"); |
| 42 | process.stderr.write(`[diffinery-runner] the platform is stopping this runner; the job keeps running for up to ${Math.round((o.drainOnSigtermMs ?? DRAIN_ON_SIGTERM_MS) / 60_000)} minutes\n`); |
| 43 | draining = setTimeout(() => forward("SIGTERM"), o.drainOnSigtermMs ?? DRAIN_ON_SIGTERM_MS); |
| 44 | draining.unref(); |
| 45 | }; |
| 46 | process.on("SIGINT", onInt); |
| 47 | process.on("SIGTERM", onTerm); |
| 48 | return new Promise((resolve) => { |
| 49 | const done = (code: number) => { |
| 50 | if (draining) clearTimeout(draining); |
| 51 | process.off("SIGINT", onInt); |
| 52 | process.off("SIGTERM", onTerm); |
| 53 | resolve(code); |
| 54 | }; |
| 55 | child.on("error", (e) => { |
| 56 | process.stderr.write(`[diffinery-runner] could not start GitHub's runner: ${e.message}\n`); |
| 57 | done(2); |
| 58 | }); |
| 59 | child.on("exit", (code, signal) => done(code ?? (signal ? 1 : 0))); |
| 60 | }); |
| 61 | } |