Diffinery

codician/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

903 lines · 37.2 KBRaw History
1import type { ChildProcess } from "node:child_process";
2import { randomUUID } from "node:crypto";
3import { existsSync } from "node:fs";
4import { mkdir, readFile, writeFile } from "node:fs/promises";
5import path from "node:path";
6import type { Readable } from "node:stream";
7import type { Conclusion, ContainerSpec, JobCompletion, JobSpec, StepReport, StepSpec } from "@diffinery/shared";
8import { evaluate, evaluateIf, interpolate, type StatusFns } from "@diffinery/workflow-yaml";
9import { cpuTimes, runnerInfo, type RunnerApi } from "./api.js";
10import { parseKeyValueFile, parsePathFile } from "./commands.js";
11import { Docker, type Wrap } from "./docker.js";
12import { JobLogger, OutputProcessor, type Annotation, type OutputSink } from "./log.js";
13import { Masker } from "./mask.js";
14import { ToolPrefetch } from "./prefetch.js";
15import { RuntimeClient } from "./runtime.js";
16import { runAction } from "./steps/actions.js";
17import { runScript } from "./steps/run.js";
18import { exec, hashFiles, killTree, nowIso, runnerArch, type ExecResult } from "./util.js";
19
20export type Outcome = "success" | "failure" | "cancelled" | "skipped";
21
22/**
23 * After a cancel or a job timeout, how long the steps that still run (`if: always()`, `if: cancelled()`,
24 * post steps) get before they are stopped too: GitHub's runner gives them 5 minutes.
25 */
26export const CANCEL_GRACE_MS = 5 * 60_000;
27
28export interface StepContextEntry {
29 outputs: Record<string, string>;
30 outcome: Outcome;
31 conclusion: Outcome;
32}
33
34/** Execution scope: the job itself, or the inside of a composite action. */
35export interface Scope {
36 steps: Record<string, StepContextEntry>;
37 inputs: Record<string, unknown>;
38 /** A step in this scope failed (without continue-on-error). */
39 failed: boolean;
40 /** Env inherited from the calling step (composite actions). */
41 inheritedEnv: Record<string, string>;
42 composite: boolean;
43 action?: { path: string; repository: string; ref: string };
44 depth: number;
45}
46
47export interface StepIO extends OutputSink {
48 /** Short reason recorded when the step fails (e.g. exit code). */
49 reason?: string;
50}
51
52export interface StepResult {
53 outcome: Outcome;
54 conclusion: Outcome;
55 outputs: Record<string, string>;
56}
57
58export interface PostStep {
59 name: string;
60 /** Expression evaluated with job status (e.g. `always()`, `success()`). */
61 condition: string;
62 run: (logIndex: number) => Promise<void>;
63}
64
65export interface RunnerOptions {
66 workRoot: string;
67 /** Tool cache dir; defaults to `${workRoot}/_tool` (or RUNNER_TOOL_CACHE). */
68 toolCache?: string;
69 echoLogs?: boolean;
70 /** Base env for child processes (defaults to process.env). */
71 baseEnv?: NodeJS.ProcessEnv;
72 /** Start the Docker daemon as the job begins when the job will use Docker (hosted runners). */
73 prewarmDocker?: boolean;
74 /** Hosted runners: start installing the job's Go or Java toolchain as the job begins (prefetch.ts). */
75 prefetchTools?: boolean;
76}
77
78export class StepFailure extends Error {}
79
80/**
81 * Whether a job will use Docker: job or service containers, `docker://` actions, or a step that names
82 * docker (a `run:` script, or an action such as docker/build-push-action). A guess, so only for a head start.
83 */
84export function jobUsesDocker(spec: JobSpec): boolean {
85 if (spec.container?.image || Object.keys(spec.services ?? {}).length) return true;
86 return spec.steps.some((s) => /\bdocker\b|testcontainers/i.test(`${s.uses ?? ""}\n${s.run ?? ""}`));
87}
88
89/** The runner's own credentials: never in a step's environment. */
90const SENSITIVE_ENV = ["DIFFINERY_JOB_TOKEN", "DIFFINERY_SANDBOX_SECRET", "DIFFINERY_GITHUB_TOKEN"];
91
92export class JobRunner {
93 readonly masker = new Masker();
94 readonly logger: JobLogger;
95 readonly workspace: string;
96 readonly tempDir: string;
97 readonly toolCache: string;
98 readonly actionsDir: string;
99 readonly fileCmdDir: string;
100 readonly eventPath: string;
101 readonly debug: boolean;
102
103 /** Accumulated GITHUB_ENV values. */
104 envAccum: Record<string, string> = {};
105 /** Accumulated GITHUB_PATH entries, most recent first. */
106 pathAccum: string[] = [];
107 jobEnv: Record<string, string> = {};
108 readonly summaries: string[] = [];
109 readonly posts: PostStep[] = [];
110 readonly children = new Set<ChildProcess>();
111 /**
112 * Aborted when the job is cancelled or times out, for work the runner does itself (downloads). A new
113 * one takes its place for the steps that still run after a cancel.
114 */
115 private aborter = new AbortController();
116 readonly stepErrors = new Map<number, Annotation[]>();
117 readonly stepReasons = new Map<number, string>();
118 /** Job and service containers, and Docker actions (self-hosted runners with Docker). */
119 readonly docker: Docker;
120 readonly prefetch = new ToolPrefetch(this);
121 /** The Actions runtime services client (null when the server sent none). */
122 readonly runtime: RuntimeClient | null;
123
124 /** Why the job is ending early: the job's status for status functions and its conclusion. */
125 private stopping: null | "cancelled" | "timeout" = null;
126 /**
127 * Whether the work under way must stop now: set by a cancel, cleared once the steps that still
128 * run after it begin (resumeAfterCancel), and set for good when their grace runs out (`forced`).
129 */
130 private halting = false;
131 private forced = false;
132 private graceTimer: NodeJS.Timeout | undefined;
133 /** Set when the platform signals that it is replacing this runner (a container rollout). */
134 replacing = false;
135 private firstFailure: { step?: number; message: string } | null = null;
136 /** The vCPUs' time when a hosted job started, to say at the end how much of it the host took (steal). */
137 private cpuAtStart: { total: number; steal: number } | null = null;
138 private githubCtx: Record<string, unknown> = {};
139 private defaultEnv: Record<string, string> = {};
140 /**
141 * The Actions runtime services' variables. Like GitHub, only JavaScript and
142 * Docker actions get them (and the built-ins, which call the services directly);
143 * `run:` steps do not.
144 */
145 runtimeEnv: Record<string, string> = {};
146 private baseEnv: NodeJS.ProcessEnv;
147 private secrets: Record<string, string>;
148
149 constructor(
150 readonly spec: JobSpec,
151 readonly api: RunnerApi,
152 logger: JobLogger | null,
153 readonly opts: RunnerOptions,
154 batcher: import("./api.js").LogBatcher | null = null,
155 ) {
156 this.logger = logger ?? new JobLogger(this.masker, batcher, !!opts.echoLogs);
157 this.workspace = path.join(opts.workRoot, spec.repo.name);
158 this.tempDir = path.join(opts.workRoot, "_temp");
159 this.toolCache = opts.toolCache ?? process.env.RUNNER_TOOL_CACHE ?? path.join(opts.workRoot, "_tool");
160 this.actionsDir = path.join(opts.workRoot, "_actions");
161 this.fileCmdDir = path.join(this.tempDir, "_runner_file_commands");
162 this.eventPath = path.join(this.tempDir, "_github_workflow", "event.json");
163 this.secrets = { ...spec.secrets, GITHUB_TOKEN: spec.token };
164 this.debug = this.secrets.ACTIONS_STEP_DEBUG === "true" || spec.vars?.ACTIONS_STEP_DEBUG === "true";
165 const base = { ...(opts.baseEnv ?? process.env) };
166 for (const k of SENSITIVE_ENV) delete base[k];
167 this.baseEnv = base;
168 for (const v of Object.values(this.secrets)) this.masker.add(v);
169 if (spec.runtime) this.masker.add(spec.runtime.token);
170 this.runtime = RuntimeClient.fromSpec(spec);
171 this.docker = new Docker(this);
172 }
173
174 /** The runner's own environment (for internal tools such as the docker CLI). */
175 hostEnv(): NodeJS.ProcessEnv {
176 return { ...this.baseEnv };
177 }
178
179 // ------------------------------------------------------------ public
180
181 get signal(): AbortSignal {
182 return this.aborter.signal;
183 }
184
185 /** Whether the step under way must stop (the job was cancelled or timed out while it ran). */
186 get isStopping(): boolean {
187 return this.halting;
188 }
189
190 /**
191 * SIGINT / SIGTERM / external cancellation, or the job's timeout. Like GitHub, the step under way
192 * stops, and the steps that still run after a cancel (`if: always()`, `if: cancelled()`, post steps)
193 * get `graceMs` before they are stopped too. A second cancel stops them at once.
194 */
195 cancel(reason: "cancelled" | "timeout" = "cancelled", graceMs = CANCEL_GRACE_MS): void {
196 if (this.stopping) return this.force();
197 this.stopping = reason;
198 this.halt();
199 if (graceMs <= 0) return this.force();
200 this.graceTimer = setTimeout(() => {
201 this.logger.line(-1, `##[error]The steps that run after a cancel took longer than ${graceMs / 60_000} minutes; stopping them.`);
202 this.force();
203 }, graceMs);
204 this.graceTimer.unref();
205 }
206
207 private force(): void {
208 if (this.forced) return;
209 this.forced = true;
210 if (this.graceTimer) clearTimeout(this.graceTimer);
211 this.halt();
212 }
213
214 private halt(): void {
215 this.halting = true;
216 this.aborter.abort();
217 for (const c of this.children) killTree(c);
218 }
219
220 /** Once the cancelled step has stopped: let the steps that still run after a cancel do their work. */
221 private resumeAfterCancel(): void {
222 if (!this.halting || this.forced) return;
223 this.halting = false;
224 this.aborter = new AbortController();
225 }
226
227 async run(): Promise<JobCompletion> {
228 const spec = this.spec;
229 let jobTimer: NodeJS.Timeout | undefined;
230 if (spec.timeoutMinutes > 0) {
231 jobTimer = setTimeout(() => {
232 this.logger.line(-1, `##[error]The job running on runner has exceeded the maximum execution time of ${spec.timeoutMinutes} minutes.`);
233 this.cancel("timeout");
234 }, spec.timeoutMinutes * 60_000);
235 jobTimer.unref();
236 }
237 const top: Scope = { steps: {}, inputs: spec.inputs ?? {}, failed: false, inheritedEnv: {}, composite: false, depth: 0 };
238 if (this.opts.prewarmDocker && jobUsesDocker(spec)) this.docker.prewarm();
239 if (this.opts.prefetchTools) this.prefetch.start(spec);
240 try {
241 await this.setup();
242 await this.initContainers(top);
243 for (let i = 0; i < spec.steps.length; i++) {
244 const step = spec.steps[i]!;
245 const fallbackName = defaultStepName(step);
246 if (this.forced) {
247 await this.report(i, { status: "completed", conclusion: "cancelled", name: safeName(step, fallbackName), completedAt: nowIso() });
248 continue;
249 }
250 this.resumeAfterCancel();
251 const startedAt = nowIso();
252 let started = false;
253 const res = await this.executeStep(step, top, i, async (name) => {
254 started = true;
255 await this.report(i, { status: "in_progress", name, startedAt });
256 });
257 await this.report(i, {
258 status: "completed",
259 // After a cancel, a step whose condition kept it from running shows as cancelled, as before.
260 conclusion: this.stopping && res.conclusion === "skipped" ? "cancelled" : res.conclusion,
261 name: res.name ?? fallbackName,
262 ...(started ? { startedAt } : {}),
263 completedAt: nowIso(),
264 });
265 }
266 if (!this.forced) await this.runPosts(top);
267 } catch (e) {
268 const msg = e instanceof Error ? e.message : String(e);
269 this.logger.line(-1, `##[error]${msg}`);
270 top.failed = true;
271 this.firstFailure ??= { message: msg };
272 } finally {
273 if (jobTimer) clearTimeout(jobTimer);
274 if (this.graceTimer) clearTimeout(this.graceTimer);
275 await this.prefetch.stop();
276 try {
277 await this.docker.cleanup({ logIndex: -1, outputs: {}, state: {} });
278 } catch (e) {
279 this.logger.line(-1, `##[warning]Could not remove the job's containers: ${errMsg(e)}`);
280 }
281 }
282
283 let conclusion: Conclusion = top.failed ? "failure" : "success";
284 if (this.stopping === "cancelled") conclusion = "cancelled";
285 if (this.stopping === "timeout") {
286 conclusion = "failure";
287 this.firstFailure = { ...(this.firstFailure ?? {}), message: `The job exceeded the maximum execution time of ${spec.timeoutMinutes} minutes.` };
288 }
289 const outputs = this.jobOutputs(top);
290 const completion: JobCompletion = { conclusion, outputs };
291 const summary = this.summaries.filter((s) => s.trim() !== "").join("\n");
292 if (summary) completion.summary = this.masker.mask(summary);
293 if (conclusion === "failure" && this.firstFailure) completion.failure = { ...this.firstFailure, message: this.masker.mask(this.firstFailure.message) };
294 if (conclusion === "cancelled") completion.failure = { message: "The job was cancelled." };
295 if (this.replacing && conclusion !== "success") completion.replaced = true;
296 const stolen = this.stolenShare();
297 if (stolen !== null) this.logger.line(-1, `Host: ${stolen}% of the vCPUs' time went to other work on the host (steal)`);
298 this.logger.line(-1, `Job completed with conclusion: ${conclusion}`);
299 return completion;
300 }
301
302 /** The share of the vCPUs' time since the job started that the hypervisor gave to other work, in percent. */
303 private stolenShare(): number | null {
304 const end = this.cpuAtStart && cpuTimes();
305 if (!this.cpuAtStart || !end || end.total <= this.cpuAtStart.total) return null;
306 return Math.round((100 * (end.steal - this.cpuAtStart.steal)) / (end.total - this.cpuAtStart.total));
307 }
308
309 // ------------------------------------------------------------ setup
310
311 private async setup(): Promise<void> {
312 const spec = this.spec;
313 await mkdir(this.workspace, { recursive: true });
314 await mkdir(this.fileCmdDir, { recursive: true });
315 await mkdir(this.toolCache, { recursive: true });
316 await mkdir(this.actionsDir, { recursive: true });
317 await mkdir(path.dirname(this.eventPath), { recursive: true });
318 const g = spec.github ?? {};
319 const refName = String(g.ref_name ?? spec.ref.replace(/^refs\/(heads|tags)\//, ""));
320 const refType = String(g.ref_type ?? (spec.ref.startsWith("refs/tags/") ? "tag" : "branch"));
321 const server = spec.apiUrl.replace(/\/$/, "");
322 const repoFull = `${spec.repo.owner}/${spec.repo.name}`;
323 this.githubCtx = {
324 repository: repoFull,
325 repository_owner: spec.repo.owner,
326 sha: spec.sha,
327 ref: spec.ref,
328 ref_name: refName,
329 ref_type: refType,
330 run_id: spec.runId,
331 run_number: g.run_number ?? "1",
332 run_attempt: g.run_attempt ?? "1",
333 job: spec.jobKey,
334 workflow: spec.workflowPath,
335 event_name: "push",
336 event: {},
337 actor: "diffinery",
338 server_url: server,
339 api_url: `${server}/api/v3`,
340 graphql_url: `${server}/api/graphql`,
341 ...g,
342 workspace: this.workspace,
343 token: spec.token,
344 action: "",
345 action_path: "",
346 };
347 await writeFile(this.eventPath, JSON.stringify(this.githubCtx.event ?? {}, null, 2));
348 const str = (v: unknown) => (v === undefined || v === null ? "" : String(v));
349 const d: Record<string, string> = {
350 CI: "true",
351 GITHUB_ACTIONS: "true",
352 DIFFINERY: "true",
353 GITHUB_REPOSITORY: repoFull,
354 GITHUB_REPOSITORY_OWNER: spec.repo.owner,
355 GITHUB_SHA: spec.sha,
356 GITHUB_REF: spec.ref,
357 GITHUB_REF_NAME: refName,
358 GITHUB_REF_TYPE: refType,
359 GITHUB_HEAD_REF: str(g.head_ref),
360 GITHUB_BASE_REF: str(g.base_ref),
361 GITHUB_RUN_ID: str(this.githubCtx.run_id),
362 GITHUB_RUN_NUMBER: str(this.githubCtx.run_number),
363 GITHUB_RUN_ATTEMPT: str(this.githubCtx.run_attempt),
364 GITHUB_JOB: spec.jobKey,
365 GITHUB_ACTOR: str(this.githubCtx.actor),
366 GITHUB_TRIGGERING_ACTOR: str(g.triggering_actor ?? this.githubCtx.actor),
367 GITHUB_EVENT_NAME: str(this.githubCtx.event_name),
368 GITHUB_EVENT_PATH: this.eventPath,
369 GITHUB_SERVER_URL: server,
370 GITHUB_API_URL: `${server}/api/v3`,
371 GITHUB_GRAPHQL_URL: `${server}/api/graphql`,
372 // GitHub Enterprise's names for the same two URLs. Tools such as super-linter build api.github.com from a
373 // default domain unless these are set, and would send the job's token there.
374 GITHUB_CUSTOM_API_URL: `${server}/api/v3`,
375 GITHUB_CUSTOM_SERVER_URL: server,
376 GITHUB_WORKFLOW: str(g.workflow ?? spec.workflowPath),
377 GITHUB_WORKFLOW_REF: str(g.workflow_ref ?? `${repoFull}/${spec.workflowPath}@${spec.ref}`),
378 GITHUB_WORKSPACE: this.workspace,
379 GITHUB_RETENTION_DAYS: "90",
380 RUNNER_OS: "Linux",
381 RUNNER_ARCH: runnerArch(),
382 RUNNER_NAME: "diffinery",
383 RUNNER_ENVIRONMENT: "diffinery-hosted",
384 RUNNER_TEMP: this.tempDir,
385 RUNNER_TOOL_CACHE: this.toolCache,
386 RUNNER_WORKSPACE: path.dirname(this.workspace),
387 AGENT_TOOLSDIRECTORY: this.toolCache,
388 // What hosted ubuntu-24.04 runners report; setup-* actions key tool and dependency caches on it.
389 ImageOS: process.env.ImageOS ?? "ubuntu24",
390 ImageVersion: process.env.ImageVersion ?? process.env.DIFFINERY_IMAGE_VERSION ?? "20250101.1",
391 };
392 if (this.debug || this.secrets.ACTIONS_RUNNER_DEBUG === "true" || spec.vars?.ACTIONS_RUNNER_DEBUG === "true") d.RUNNER_DEBUG = "1";
393 if (spec.runtime) {
394 // The Actions runtime services (cache, artifacts), scoped to this job and expiring with it.
395 this.runtimeEnv = {
396 ACTIONS_RUNTIME_TOKEN: spec.runtime.token,
397 ACTIONS_RESULTS_URL: spec.runtime.resultsUrl,
398 ACTIONS_CACHE_URL: spec.runtime.cacheUrl,
399 ACTIONS_RUNTIME_URL: spec.runtime.runtimeUrl,
400 ACTIONS_CACHE_SERVICE_V2: "true",
401 };
402 }
403 if (spec.idToken && this.api.online) {
404 // @actions/core appends `&audience=...`, so the URL must already carry a query string.
405 d.ACTIONS_ID_TOKEN_REQUEST_URL = `${this.api.url("/oidc")}?api-version=2.0`;
406 d.ACTIONS_ID_TOKEN_REQUEST_TOKEN = spec.token;
407 }
408 this.defaultEnv = d;
409 this.logger.line(-1, `Diffinery runner: job ${spec.jobKey} (${spec.name}) for ${repoFull}@${spec.sha.slice(0, 12)}`);
410 const where = runnerInfo();
411 if (Object.keys(where).length) this.logger.line(-1, `Runner: ${[where.image && `image ${where.image}`, where.location && `in ${where.location}`, where.instance && `container ${where.instance}`].filter(Boolean).join(", ")}`);
412 if (spec.runnerSize) this.logger.line(-1, spec.runnerSize);
413 if (where.cpu) {
414 this.logger.line(-1, `Host: ${where.cpu}`);
415 this.cpuAtStart = cpuTimes();
416 }
417 this.logger.line(-1, `Workspace: ${this.workspace}`);
418 // job-level env
419 const ctx = this.contexts({ steps: {}, inputs: spec.inputs ?? {}, failed: false, inheritedEnv: {}, composite: false, depth: 0 }, {});
420 for (const [k, v] of Object.entries(spec.env ?? {})) {
421 this.jobEnv[k] = this.interp(String(v ?? ""), ctx);
422 }
423 }
424
425 // ------------------------------------------------------------ containers
426
427 private evalContainer(spec: ContainerSpec, ctx: Record<string, unknown>): ContainerSpec {
428 const i = (v: unknown) => this.interp(v === undefined || v === null ? "" : String(v), ctx);
429 const out: ContainerSpec = { image: i(spec.image).trim() };
430 if (spec.credentials) out.credentials = { username: i(spec.credentials.username), password: i(spec.credentials.password) };
431 if (spec.env) out.env = Object.fromEntries(Object.entries(spec.env).map(([k, v]) => [k, i(v)]));
432 if (spec.ports) out.ports = spec.ports.map((p) => i(p)).filter(Boolean);
433 if (spec.volumes) out.volumes = spec.volumes.map((v) => i(v)).filter(Boolean);
434 if (spec.options) out.options = i(spec.options);
435 return out;
436 }
437
438 /** Starts service containers and the job container, like GitHub's "Initialize containers". */
439 private async initContainers(top: Scope): Promise<void> {
440 const spec = this.spec;
441 if (!spec.container && !Object.keys(spec.services ?? {}).length) return;
442 const ctx = this.contexts(top, { ...this.jobEnv });
443 const container = spec.container ? this.evalContainer(spec.container, ctx) : null;
444 // A service whose image evaluates to an empty string is skipped (GitHub does the same).
445 const services = Object.entries(spec.services ?? {})
446 .map(([k, s]) => [k, this.evalContainer(s, ctx)] as const)
447 .filter(([, s]) => s.image);
448 if (!container?.image && !services.length) return;
449 const io: StepIO = { logIndex: -1, outputs: {}, state: {} };
450 this.logger.line(-1, "##[group]Initialize containers");
451 try {
452 await this.docker.ensure(io, container?.image ? "Job containers (jobs.<id>.container)" : "Service containers (jobs.<id>.services)");
453 for (const [key, s] of services) await this.docker.startService(io, key, s);
454 if (container?.image) await this.docker.startJobContainer(io, container);
455 } finally {
456 this.logger.line(-1, "##[endgroup]");
457 }
458 }
459
460 /** Wraps a step process so it runs in the job container, when there is one. */
461 containerWrap(): Wrap | undefined {
462 return this.docker.execWrap();
463 }
464
465 /** The node binary JavaScript actions run with (the runner's own, mounted into a job container). */
466 nodePath(): string {
467 return this.docker.container ? "/__e/node" : process.execPath;
468 }
469
470 // ------------------------------------------------------------ contexts
471
472 statusFns(scope: Scope): StatusFns {
473 return {
474 success: () => !scope.failed && this.stopping === null,
475 // GitHub: once the job is cancelled its status is cancelled, so failure() is false.
476 failure: () => scope.failed && this.stopping === null,
477 cancelled: () => this.stopping !== null,
478 always: () => true,
479 };
480 }
481
482 extra(): { hashFiles: (...p: string[]) => string } {
483 return { hashFiles: (...patterns: string[]) => hashFiles(this.workspace, patterns) };
484 }
485
486 /** Env context visible to a step before its own `env:` is applied. */
487 baseEnvContext(scope: Scope): Record<string, string> {
488 return { ...this.jobEnv, ...this.envAccum, ...scope.inheritedEnv };
489 }
490
491 contexts(scope: Scope, env: Record<string, string>): Record<string, unknown> {
492 const spec = this.spec;
493 const github = { ...this.githubCtx };
494 if (scope.action) {
495 github.action_path = scope.action.path;
496 github.action_repository = scope.action.repository;
497 github.action_ref = scope.action.ref;
498 }
499 return {
500 github,
501 env,
502 vars: spec.vars ?? {},
503 secrets: this.secrets,
504 matrix: spec.matrix ?? {},
505 needs: spec.needs ?? {},
506 steps: scope.steps,
507 runner: {
508 os: "Linux",
509 arch: runnerArch(),
510 name: "diffinery",
511 temp: this.tempDir,
512 tool_cache: this.toolCache,
513 workspace: path.dirname(this.workspace),
514 debug: this.debug ? "1" : "",
515 environment: "diffinery-hosted",
516 },
517 inputs: scope.inputs,
518 job: {
519 status: this.stopping ? "cancelled" : scope.failed ? "failure" : "success",
520 container: this.docker.container ? { id: this.docker.container.id, network: this.docker.container.network } : {},
521 services: Object.fromEntries(Object.entries(this.docker.services).map(([k, v]) => [k, { id: v.id, network: v.network, ports: v.ports }])),
522 },
523 strategy: spec.strategy ?? {},
524 };
525 }
526
527 interp(template: string, ctx: Record<string, unknown>, scope?: Scope): string {
528 if (!template.includes("${{")) return template;
529 const status = scope ? this.statusFns(scope) : undefined;
530 return interpolate(template, ctx, status, this.extra());
531 }
532
533 evalIf(cond: string | undefined, ctx: Record<string, unknown>, scope: Scope): boolean {
534 return evaluateIf(cond, ctx, this.statusFns(scope), this.extra());
535 }
536
537 evalExpr(expr: string, ctx: Record<string, unknown>, scope: Scope): unknown {
538 return evaluate(expr, ctx, this.statusFns(scope), this.extra());
539 }
540
541 /**
542 * Layers workflow env over the runner's defaults. Like GitHub, workflows can
543 * override defaults such as CI or DIFFINERY, but not GITHUB_* or RUNNER_*.
544 */
545 withDefaults(envCtx: Record<string, string>): Record<string, string> {
546 const env = { ...this.defaultEnv, ...envCtx };
547 for (const [k, v] of Object.entries(this.defaultEnv)) if (/^(GITHUB|RUNNER)_/.test(k)) env[k] = v;
548 return env;
549 }
550
551 /** Builds the environment for a child process. */
552 processEnv(envCtx: Record<string, string>, extra: Record<string, string> = {}): NodeJS.ProcessEnv {
553 const env: NodeJS.ProcessEnv = { ...this.baseEnv, ...this.withDefaults(envCtx), ...extra };
554 const basePath = envCtx.PATH ?? this.baseEnv.PATH ?? "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
555 env.PATH = [...this.pathAccum, basePath].filter(Boolean).join(path.delimiter);
556 return env;
557 }
558
559 // ------------------------------------------------------------ steps
560
561 /**
562 * Evaluates `if`, env, name; runs the step; updates the scope's steps
563 * context and failure state.
564 */
565 async executeStep(step: StepSpec, scope: Scope, logIndex: number, onStart?: (name: string) => Promise<void>): Promise<StepResult & { name: string }> {
566 const baseEnv = this.baseEnvContext(scope);
567 let ctx = this.contexts(scope, baseEnv);
568 let name = defaultStepName(step);
569 try {
570 name = step.name !== undefined ? this.interp(String(step.name), ctx, scope) : name;
571 } catch {
572 /* keep default */
573 }
574 const id = step.id;
575 const record = (outcome: Outcome, conclusion: Outcome, outputs: Record<string, string>) => {
576 if (id) scope.steps[id] = { outputs, outcome, conclusion };
577 };
578
579 let run: boolean;
580 try {
581 run = evaluateIf(step.if, ctx, this.statusFns(scope), this.extra());
582 } catch (e) {
583 this.logger.line(logIndex, `##[error]Error evaluating 'if' condition: ${errMsg(e)}`);
584 this.noteFailure(logIndex, `Error evaluating 'if' condition of step '${name}': ${errMsg(e)}`);
585 scope.failed = true;
586 record("failure", "failure", {});
587 return { outcome: "failure", conclusion: "failure", outputs: {}, name };
588 }
589 if (!run) {
590 if (scope.depth > 0) this.logger.line(logIndex, `Skipping step '${name}' (condition not met)`);
591 record("skipped", "skipped", {});
592 return { outcome: "skipped", conclusion: "skipped", outputs: {}, name };
593 }
594 await onStart?.(name);
595 if (scope.depth === 0) await this.prefetch.wait(logIndex, (m) => this.logger.line(logIndex, m));
596
597 const io: StepIO = { logIndex, outputs: {}, state: {} };
598 let outcome: Outcome = "success";
599 let continueOnError = false;
600 try {
601 continueOnError = this.evalBoolish(step["continue-on-error"], ctx, scope);
602 const stepEnv: Record<string, string> = {};
603 const envCtx = { ...baseEnv };
604 ctx = this.contexts(scope, envCtx);
605 for (const [k, v] of Object.entries(step.env ?? {})) {
606 stepEnv[k] = this.interp(String(v ?? ""), ctx, scope);
607 envCtx[k] = stepEnv[k]!;
608 }
609 const timeoutMin = this.evalNumber(step["timeout-minutes"], ctx, scope);
610 const timeoutMs = timeoutMin > 0 ? timeoutMin * 60_000 : 0;
611 if (step.run !== undefined) {
612 await runScript(this, { step, scope, ctx, envCtx, io, timeoutMs, name });
613 } else if (step.uses) {
614 await runAction(this, { step, scope, ctx, envCtx, io, timeoutMs, name });
615 } else {
616 throw new StepFailure("Step has neither `run` nor `uses`.");
617 }
618 if (this.halting) outcome = "cancelled";
619 } catch (e) {
620 if (this.halting) {
621 outcome = "cancelled";
622 } else {
623 outcome = "failure";
624 const msg = errMsg(e);
625 if (!(e instanceof StepFailure && (e as StepFailure & { logged?: boolean }).logged)) {
626 this.logger.line(logIndex, `##[error]${msg}`);
627 this.addAnnotation(logIndex, { level: "error", message: msg });
628 }
629 io.reason ??= msg;
630 }
631 }
632
633 let conclusion: Outcome = outcome;
634 if (outcome === "failure") {
635 if (continueOnError) {
636 conclusion = "success";
637 this.logger.line(logIndex, "##[warning]Step failed but continue-on-error is set; continuing.");
638 } else {
639 scope.failed = true;
640 this.noteFailure(logIndex, io.reason);
641 }
642 }
643 record(outcome, conclusion, io.outputs);
644 return { outcome, conclusion, outputs: io.outputs, name };
645 }
646
647 private evalBoolish(v: unknown, ctx: Record<string, unknown>, scope: Scope): boolean {
648 if (v === undefined || v === null || v === false) return false;
649 if (v === true) return true;
650 let s = String(v).trim();
651 if (s.includes("${{")) s = this.interp(s, ctx, scope);
652 return s === "true";
653 }
654
655 private evalNumber(v: unknown, ctx: Record<string, unknown>, scope: Scope): number {
656 if (v === undefined || v === null || v === "") return 0;
657 if (typeof v === "number") return v;
658 let s = String(v);
659 if (s.includes("${{")) s = this.interp(s, ctx, scope);
660 const n = Number(s);
661 return Number.isFinite(n) ? n : 0;
662 }
663
664 addAnnotation(logIndex: number, a: Annotation): void {
665 let list = this.stepErrors.get(logIndex);
666 if (!list) this.stepErrors.set(logIndex, (list = []));
667 list.push(a);
668 }
669
670 private noteFailure(logIndex: number, reason?: string): void {
671 if (this.firstFailure) return;
672 const errs = (this.stepErrors.get(logIndex) ?? []).filter((a) => a.level === "error");
673 // prefer annotations emitted by the step itself over our generic "exit code" message
674 const userErr = [...errs].reverse().find((a) => a.message !== reason);
675 let message: string;
676 if (userErr) {
677 const loc = userErr.file ? `${userErr.file}${userErr.line ? `:${userErr.line}` : ""}: ` : "";
678 message = `${loc}${userErr.title ? `${userErr.title}: ` : ""}${userErr.message}`;
679 } else {
680 const tail = this.logger.tail(logIndex, 20).filter((l) => !l.startsWith("##[error]"));
681 message = [reason ?? "Step failed", ...(tail.length ? ["", ...tail] : [])].join("\n");
682 }
683 this.firstFailure = { step: logIndex >= 0 ? logIndex : undefined, message };
684 if (this.firstFailure.step === undefined) delete this.firstFailure.step;
685 }
686
687 /**
688 * Runs a process for a step with fresh file-command files, interpreting
689 * workflow commands, then applies GITHUB_ENV/PATH/OUTPUT/STATE/SUMMARY.
690 */
691 async runStepProcess(
692 argv: string[],
693 o: {
694 cwd: string;
695 envCtx: Record<string, string>;
696 io: StepIO;
697 timeoutMs?: number;
698 extraEnv?: Record<string, string>;
699 timeoutLabel?: string;
700 /** Run the process somewhere else (the job container, or a Docker action's container). */
701 wrap?: Wrap;
702 },
703 ): Promise<ExecResult> {
704 const id = randomUUID();
705 const files = {
706 GITHUB_ENV: path.join(this.fileCmdDir, `set_env_${id}`),
707 GITHUB_PATH: path.join(this.fileCmdDir, `add_path_${id}`),
708 GITHUB_OUTPUT: path.join(this.fileCmdDir, `set_output_${id}`),
709 GITHUB_STATE: path.join(this.fileCmdDir, `save_state_${id}`),
710 GITHUB_STEP_SUMMARY: path.join(this.fileCmdDir, `step_summary_${id}`),
711 };
712 await mkdir(this.fileCmdDir, { recursive: true });
713 await Promise.all(Object.values(files).map((f) => writeFile(f, "")));
714 const proc = this.processor(o.io);
715 if (!existsSync(o.cwd)) {
716 throw new StepFailure(`An error occurred trying to start process '${argv[0]}' with working directory '${o.cwd}'. No such file or directory`);
717 }
718 let cmd = argv;
719 let cwd = o.cwd;
720 let env = this.processEnv(o.envCtx, { ...(o.extraEnv ?? {}), ...files });
721 if (o.wrap) ({ argv: cmd, cwd, env } = o.wrap(argv, { ...this.withDefaults(o.envCtx), ...(o.extraEnv ?? {}), ...files }, o.cwd));
722 const res = await exec(cmd[0]!, cmd.slice(1), {
723 cwd,
724 env,
725 timeoutMs: o.timeoutMs,
726 onLine: (l) => proc.handle(l),
727 onSpawn: (c) => {
728 this.children.add(c);
729 c.once("close", () => this.children.delete(c));
730 },
731 });
732 await this.applyFileCommands(files, o.io);
733 if (res.signal && this.replacing && !this.stopping) {
734 // The platform's SIGTERM (a rollout, or a move) reaches every process in the container, not only the agent. The job
735 // can't succeed now, so stop it and report at once: Diffinery starts it again on a new runner.
736 const msg = "The platform stopped this step's processes while stopping the runner.";
737 this.logger.line(o.io.logIndex, `##[error]${msg}`);
738 this.addAnnotation(o.io.logIndex, { level: "error", message: msg });
739 // No steps run after this cancel: the platform is stopping the container, and the job may start again elsewhere.
740 this.cancel("cancelled", 0);
741 }
742 if (res.timedOut) {
743 const mins = (o.timeoutMs ?? 0) / 60000;
744 const msg = `The ${o.timeoutLabel ?? "step"} has timed out after ${Number(mins.toFixed(2))} minutes.`;
745 this.logger.line(o.io.logIndex, `##[error]${msg}`);
746 this.addAnnotation(o.io.logIndex, { level: "error", message: msg });
747 o.io.reason = msg;
748 }
749 return res;
750 }
751
752 processor(io: StepIO): OutputProcessor {
753 return new OutputProcessor(this.logger, this.masker, io, {
754 debug: this.debug,
755 onAnnotation: (a) => this.addAnnotation(io.logIndex, a),
756 });
757 }
758
759 private async applyFileCommands(files: Record<string, string>, io: StepIO): Promise<void> {
760 const read = async (f: string) => {
761 try {
762 return await readFile(f, "utf8");
763 } catch {
764 return "";
765 }
766 };
767 const [envC, pathC, outC, stateC, sumC] = await Promise.all([
768 read(files.GITHUB_ENV!),
769 read(files.GITHUB_PATH!),
770 read(files.GITHUB_OUTPUT!),
771 read(files.GITHUB_STATE!),
772 read(files.GITHUB_STEP_SUMMARY!),
773 ]);
774 const safe = (label: string, fn: () => void) => {
775 try {
776 fn();
777 } catch (e) {
778 this.logger.line(io.logIndex, `##[error]Unable to process file command '${label}' successfully: ${errMsg(e)}`);
779 this.addAnnotation(io.logIndex, { level: "error", message: `Unable to process file command '${label}': ${errMsg(e)}` });
780 }
781 };
782 safe("env", () => {
783 for (const [k, v] of parseKeyValueFile(envC)) {
784 if (k === "NODE_OPTIONS" && /--require|-r\b/.test(v)) continue;
785 this.envAccum[k] = v;
786 }
787 });
788 safe("path", () => {
789 for (const p of parsePathFile(pathC)) this.addPath(p);
790 });
791 safe("output", () => {
792 for (const [k, v] of parseKeyValueFile(outC)) io.outputs[k] = v;
793 });
794 safe("state", () => {
795 for (const [k, v] of parseKeyValueFile(stateC)) io.state[k] = v;
796 });
797 if (sumC.trim()) this.summaries.push(sumC);
798 }
799
800 addPath(p: string): void {
801 this.pathAccum = [p, ...this.pathAccum.filter((x) => x !== p)];
802 }
803
804 /** Runs an internal tool (git, tar, ...) streaming its output into the step log. */
805 async tool(cmd: string, args: string[], o: { io: StepIO; cwd?: string; env?: NodeJS.ProcessEnv; capture?: boolean; silent?: boolean; input?: string | Readable }): Promise<ExecResult> {
806 return exec(cmd, args, {
807 cwd: o.cwd ?? this.workspace,
808 env: o.env ?? this.processEnv(this.baseEnvContext({ steps: {}, inputs: {}, failed: false, inheritedEnv: {}, composite: false, depth: 0 })),
809 capture: o.capture,
810 input: o.input,
811 onLine: o.silent ? undefined : (l) => this.logger.line(o.io.logIndex, l),
812 onSpawn: (c) => {
813 this.children.add(c);
814 c.once("close", () => this.children.delete(c));
815 },
816 });
817 }
818
819 registerPost(p: PostStep): void {
820 this.posts.push(p);
821 }
822
823 private async runPosts(top: Scope): Promise<void> {
824 const posts = [...this.posts].reverse();
825 let index = this.spec.steps.length;
826 for (const p of posts) {
827 const logIndex = index++;
828 if (this.forced) break;
829 this.resumeAfterCancel();
830 let ok: boolean;
831 try {
832 ok = evaluateIf(p.condition, this.contexts(top, this.baseEnvContext(top)), this.statusFns(top), this.extra());
833 } catch {
834 ok = false;
835 }
836 if (!ok) continue;
837 const startedAt = nowIso();
838 await this.report(logIndex, { status: "in_progress", name: p.name, startedAt });
839 let conclusion: Conclusion = "success";
840 try {
841 await p.run(logIndex);
842 } catch (e) {
843 conclusion = "failure";
844 const msg = errMsg(e);
845 if (!(e instanceof StepFailure && (e as StepFailure & { logged?: boolean }).logged)) {
846 this.logger.line(logIndex, `##[error]${msg}`);
847 this.addAnnotation(logIndex, { level: "error", message: msg });
848 }
849 top.failed = true;
850 this.noteFailure(logIndex, msg);
851 }
852 if (this.halting) conclusion = "cancelled";
853 await this.report(logIndex, { status: "completed", conclusion, name: p.name, startedAt, completedAt: nowIso() });
854 }
855 }
856
857 private jobOutputs(top: Scope): Record<string, string> {
858 const out: Record<string, string> = {};
859 const ctx = this.contexts(top, this.baseEnvContext(top));
860 for (const [k, expr] of Object.entries(this.spec.outputs ?? {})) {
861 try {
862 const v = this.interp(String(expr), ctx, top);
863 if (this.masker.containsSecret(v)) {
864 this.logger.line(-1, `##[warning]Skip output '${k}' since it may contain secret.`);
865 continue;
866 }
867 out[k] = v;
868 } catch (e) {
869 this.logger.line(-1, `##[warning]Failed to evaluate job output '${k}': ${errMsg(e)}`);
870 }
871 }
872 return out;
873 }
874
875 private async report(index: number, r: StepReport): Promise<void> {
876 try {
877 await this.api.postStep(index, { ...r, name: this.masker.mask(r.name) });
878 } catch (e) {
879 this.logger.line(-1, `##[warning]Failed to report step ${index}: ${errMsg(e)}`);
880 }
881 }
882}
883
884export function errMsg(e: unknown): string {
885 return e instanceof Error ? e.message : String(e);
886}
887
888export function failure(message: string, logged = false): StepFailure {
889 const f = new StepFailure(message) as StepFailure & { logged?: boolean };
890 f.logged = logged;
891 return f;
892}
893
894export function defaultStepName(step: StepSpec): string {
895 if (step.name) return String(step.name);
896 if (step.uses) return `Run ${step.uses}`;
897 if (step.run) return `Run ${String(step.run).trim().split("\n")[0]}`;
898 return "Step";
899}
900
901function safeName(step: StepSpec, fallback: string): string {
902 return step.name && !String(step.name).includes("${{") ? String(step.name) : fallback;
903}