@olivervea/pipelines 2.0.0-beta.2 → 2.0.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -36
- package/manifest.schema.json +88 -10
- package/package.json +1 -1
- package/src/build/build.ts +3 -3
- package/src/build/cli.ts +1 -1
- package/src/build/hashes.ts +1 -1
- package/src/build/project.ts +26 -5
- package/src/builder/pipeline.ts +77 -9
- package/src/core/config-refs.ts +77 -0
- package/src/core/config.ts +147 -0
- package/src/core/handlers.ts +3 -3
- package/src/core/steps.ts +29 -6
- package/src/ctx/index.ts +3 -0
- package/src/ctx/sh.ts +2 -1
- package/src/ctx/types.ts +4 -1
- package/src/describe/describe.ts +187 -13
- package/src/describe/manifest-schema.ts +37 -4
- package/src/dev/cli.ts +11 -4
- package/src/harness/execute.ts +7 -4
- package/src/harness/index.ts +1 -1
- package/src/harness/main.ts +49 -10
- package/src/harness/validate.ts +19 -1
- package/src/index.ts +4 -3
- package/src/library/checks.ts +23 -0
- package/src/library/compose-deploy.ts +93 -0
- package/src/library/github-source.ts +23 -4
- package/src/library/helm-deploy-ssh.ts +240 -87
- package/src/library/helm-deploy.ts +268 -0
- package/src/library/image-build.ts +313 -33
- package/src/library/index.ts +10 -3
- package/src/library/models.ts +16 -1
- package/src/library/npm.ts +33 -0
- package/src/library/ssh.ts +45 -0
- package/src/testing/index.ts +6 -4
- package/src/util/handler-hash.ts +1 -1
package/src/builder/pipeline.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { IsUnion, OrError, Simplify } from "../core/types.ts";
|
|
|
8
8
|
import type { Optional } from "../core/models.ts";
|
|
9
9
|
import type { FailureContext } from "../library/models.ts";
|
|
10
10
|
import type { MapperResult, Ref } from "./refs.ts";
|
|
11
|
+
import { isConfigRef, narrowConfigRef, unionAt, type ConfigNode, type ConfigRef, type DiscriminatorOf, type VariantOf } from "../core/config.ts";
|
|
11
12
|
|
|
12
13
|
// ─── Availability (builder state, type level) ────────────────────────────────────────────────
|
|
13
14
|
|
|
@@ -96,6 +97,29 @@ type OutputsOf<E extends readonly unknown[], Maybe extends boolean> = {
|
|
|
96
97
|
};
|
|
97
98
|
type MakeMaybe<X extends AvailMap> = { [K in keyof X]: Slot<X[K]["model"], X[K]["value"], true> };
|
|
98
99
|
|
|
100
|
+
// ─── select (branching on config) ────────────────────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
type KeysOfUnion<T> = T extends unknown ? keyof T : never;
|
|
103
|
+
type SlotIn<B, K extends PropertyKey> = B extends { [P in K]: infer X extends Slot } ? X : never;
|
|
104
|
+
/** A case function: the pipeline so far, and the select's reference narrowed to the case's variant. */
|
|
105
|
+
export type SelectCase<In extends AnyModel, A extends AvailMap, R> = (p: Pipeline<In, A>, ref: R) => Pipeline<In, AvailMap>;
|
|
106
|
+
export type SelectCases<In extends AnyModel, A extends AvailMap, U, D extends string> = {
|
|
107
|
+
readonly [V in Extract<U[D & keyof U], string>]: SelectCase<In, A, ConfigNode<VariantOf<U, D, V>>>;
|
|
108
|
+
};
|
|
109
|
+
type CaseAvail<C> = { [K in keyof C]: C[K] extends (...args: never[]) => Pipeline<AnyModel, infer B> ? B : never }[keyof C];
|
|
110
|
+
/**
|
|
111
|
+
* After a select: a step every case has is as available as in its cases (one case always runs);
|
|
112
|
+
* a step only some cases have is maybe-available.
|
|
113
|
+
*/
|
|
114
|
+
export type AfterSelect<A extends AvailMap, B> = Simplify<
|
|
115
|
+
A &
|
|
116
|
+
{ [K in Exclude<keyof B, keyof A>]: Slot<SlotIn<B, K>["model"], SlotIn<B, K>["value"], SlotIn<B, K>["maybe"]> } &
|
|
117
|
+
{ [K in Exclude<KeysOfUnion<B>, keyof B | keyof A>]: Slot<SlotIn<B, K>["model"], SlotIn<B, K>["value"], true> }
|
|
118
|
+
>;
|
|
119
|
+
type SelectOnError<U> = [DiscriminatorOf<U>] extends [never]
|
|
120
|
+
? "❌ select needs a reference to a discriminated union in config (z.discriminatedUnion)"
|
|
121
|
+
: IsUnion<DiscriminatorOf<U>> extends true ? "❌ the union has more than one literal field; select can't tell which is the discriminator" : never;
|
|
122
|
+
|
|
99
123
|
// ─── Conditions / triggers ───────────────────────────────────────────────────────────────────
|
|
100
124
|
|
|
101
125
|
type IsLiteralLike<T> = [T] extends [string | number | boolean]
|
|
@@ -134,7 +158,8 @@ export interface StageStepNode {
|
|
|
134
158
|
}
|
|
135
159
|
export type StageNode =
|
|
136
160
|
| { readonly kind: "stage"; readonly steps: readonly StageStepNode[] }
|
|
137
|
-
| { readonly kind: "when"; readonly cond: Readonly<Record<string, unknown>>; readonly nodes: readonly StageNode[] }
|
|
161
|
+
| { readonly kind: "when"; readonly cond: Readonly<Record<string, unknown>>; readonly nodes: readonly StageNode[] }
|
|
162
|
+
| { readonly kind: "select"; readonly on: ConfigRef; readonly cases: Readonly<Record<string, readonly StageNode[]>> };
|
|
138
163
|
|
|
139
164
|
export type TriggerNode =
|
|
140
165
|
| { readonly kind: "push"; readonly name: string; readonly map: (event: unknown) => unknown }
|
|
@@ -178,6 +203,11 @@ export function isPipeline(p: unknown): boolean {
|
|
|
178
203
|
|
|
179
204
|
// ─── Pipeline builder ────────────────────────────────────────────────────────────────────────
|
|
180
205
|
|
|
206
|
+
// Builder calls record into shared data and hand back a fresh handle; a handle that was already
|
|
207
|
+
// extended no longer matches its type (it would claim fewer stages than the data has), so reuse is a problem.
|
|
208
|
+
const extended = new WeakSet<object>();
|
|
209
|
+
const ReusedProblem = "a pipeline value was extended twice: chain the calls (pipeline({...}).stage(a).stage(b)) instead of reusing an earlier value";
|
|
210
|
+
|
|
181
211
|
export interface PipelineDef<In extends AnyModel> {
|
|
182
212
|
/** Display name. */
|
|
183
213
|
name: string;
|
|
@@ -203,10 +233,19 @@ export class Pipeline<In extends AnyModel, A extends AvailMap> {
|
|
|
203
233
|
};
|
|
204
234
|
}
|
|
205
235
|
|
|
236
|
+
private next(): never {
|
|
237
|
+
const data = this[PIPELINE];
|
|
238
|
+
if (extended.has(this) && !data.problems.includes(ReusedProblem)) data.problems.push(ReusedProblem);
|
|
239
|
+
extended.add(this);
|
|
240
|
+
const handle = Object.create(Object.getPrototypeOf(this)) as Pipeline<In, A>;
|
|
241
|
+
(handle as { [PIPELINE]: PipelineData })[PIPELINE] = data;
|
|
242
|
+
return handle as never;
|
|
243
|
+
}
|
|
244
|
+
|
|
206
245
|
/** Map a GitHub push to the pipeline input (`z.input` view). Runs as code in a `map-trigger` Job per event. */
|
|
207
246
|
onPush<R extends InferInput<In>>(map: (event: PushEvent) => Exact<R, InferInput<In>>): this {
|
|
208
247
|
this[PIPELINE].triggers.push({ kind: "push", name: "push", map: map as (event: unknown) => unknown });
|
|
209
|
-
return this;
|
|
248
|
+
return this.next();
|
|
210
249
|
}
|
|
211
250
|
|
|
212
251
|
/**
|
|
@@ -235,7 +274,7 @@ export class Pipeline<In extends AnyModel, A extends AvailMap> {
|
|
|
235
274
|
intervalSeconds: cfg.intervalSeconds,
|
|
236
275
|
map: map as (value: string) => unknown,
|
|
237
276
|
});
|
|
238
|
-
return this;
|
|
277
|
+
return this.next();
|
|
239
278
|
}
|
|
240
279
|
|
|
241
280
|
/**
|
|
@@ -247,7 +286,7 @@ export class Pipeline<In extends AnyModel, A extends AvailMap> {
|
|
|
247
286
|
map: (event: WebhookEvent) => Exact<R, InferInput<In>> | null,
|
|
248
287
|
): this {
|
|
249
288
|
this[PIPELINE].triggers.push({ kind: "webhook", name: cfg.name, verify: cfg.verify, map: map as (event: WebhookEvent) => unknown });
|
|
250
|
-
return this;
|
|
289
|
+
return this.next();
|
|
251
290
|
}
|
|
252
291
|
|
|
253
292
|
/**
|
|
@@ -272,7 +311,7 @@ export class Pipeline<In extends AnyModel, A extends AvailMap> {
|
|
|
272
311
|
});
|
|
273
312
|
if (entries.length === 0) data.problems.push("empty stage");
|
|
274
313
|
data.nodes.push({ kind: "stage", steps });
|
|
275
|
-
return this;
|
|
314
|
+
return this.next();
|
|
276
315
|
}
|
|
277
316
|
|
|
278
317
|
/** Declarative condition on literal-like pipeline-input fields. Outputs produced inside become maybe-available after. */
|
|
@@ -280,9 +319,38 @@ export class Pipeline<In extends AnyModel, A extends AvailMap> {
|
|
|
280
319
|
const data = this[PIPELINE];
|
|
281
320
|
const inner = new Pipeline<In, A>({ name: data.name, input: data.input as In }, data);
|
|
282
321
|
const returned = block(inner);
|
|
283
|
-
if (returned
|
|
322
|
+
if (!isPipeline(returned) || pipelineData(returned) !== inner[PIPELINE])
|
|
323
|
+
data.problems.push("a when block must return the pipeline it was given (p => p.stage(...))");
|
|
284
324
|
data.nodes.push({ kind: "when", cond: { ...(cond as object) }, nodes: inner[PIPELINE].nodes });
|
|
285
|
-
return this
|
|
325
|
+
return this.next();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Branch on config: `on` references a discriminated union in a config entry, and each case gets the
|
|
330
|
+
* pipeline and `on` narrowed to its variant. Exactly one case runs, chosen by the run's config
|
|
331
|
+
* snapshot (not by run input; that is `when`). The same step name may appear in several cases.
|
|
332
|
+
*/
|
|
333
|
+
select<U, const C extends SelectCases<In, A, U, DiscriminatorOf<U>>>(
|
|
334
|
+
on: ConfigRef<U> & ([SelectOnError<U>] extends [never] ? unknown : SelectOnError<U>),
|
|
335
|
+
cases: C & { readonly [K in Exclude<keyof C, keyof SelectCases<In, A, U, DiscriminatorOf<U>>>]: `❌ '${K & string}' is not a case of this select` },
|
|
336
|
+
): Pipeline<In, AfterSelect<A, CaseAvail<C>>> {
|
|
337
|
+
const data = this[PIPELINE];
|
|
338
|
+
const recorded: Record<string, StageNode[]> = {};
|
|
339
|
+
const union = isConfigRef(on) ? unionAt(on) : undefined;
|
|
340
|
+
if (!union) data.problems.push("select: the first argument must reference a z.discriminatedUnion in a config entry");
|
|
341
|
+
for (const [value, fn] of Object.entries(cases as Record<string, unknown>)) {
|
|
342
|
+
if (typeof fn !== "function") {
|
|
343
|
+
data.problems.push(`select: case '${value}' is not a function`);
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
const inner = new Pipeline<In, A>({ name: data.name, input: data.input as In }, data);
|
|
347
|
+
const returned = (fn as (p: unknown, ref: unknown) => unknown)(inner, union ? narrowConfigRef(on, union.discriminator, value) : on);
|
|
348
|
+
if (!isPipeline(returned) || pipelineData(returned) !== inner[PIPELINE])
|
|
349
|
+
data.problems.push(`select: case '${value}' must return the pipeline it was given ((p, ref) => p.stage(...))`);
|
|
350
|
+
recorded[value] = inner[PIPELINE].nodes;
|
|
351
|
+
}
|
|
352
|
+
data.nodes.push({ kind: "select", on, cases: recorded });
|
|
353
|
+
return this.next();
|
|
286
354
|
}
|
|
287
355
|
|
|
288
356
|
/** Run `handler` (a step whose inputs are all FailureContext) when one of `steps` (default: any) fails. Untracked, best-effort. */
|
|
@@ -292,10 +360,10 @@ export class Pipeline<In extends AnyModel, A extends AvailMap> {
|
|
|
292
360
|
? unknown
|
|
293
361
|
: "❌ a failure handler's inputs must all be FailureContext"),
|
|
294
362
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
295
|
-
opts?: { steps?: readonly Step<StepNames<A>, any, any, any>[] },
|
|
363
|
+
opts?: { steps?: readonly Step<StepNames<A>, any, any, any, any>[] },
|
|
296
364
|
): this {
|
|
297
365
|
this[PIPELINE].failureHandlers.push({ step: handler as AnyStep, steps: opts?.steps ? [...(opts.steps as readonly AnyStep[])] : null });
|
|
298
|
-
return this;
|
|
366
|
+
return this.next();
|
|
299
367
|
}
|
|
300
368
|
}
|
|
301
369
|
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Config references inside library-step definitions: collected (the step receives their entries),
|
|
2
|
+
// recorded in the manifest as `{"$config": "<entry>.<path>"}`, and resolved from ctx.config at run time.
|
|
3
|
+
import { configRefPath, configRefTarget, isConfigRef, type AnyConfigEntry, type ConfigRef } from "./config.ts";
|
|
4
|
+
|
|
5
|
+
/** `T` with every config reference replaced by the type of its value. */
|
|
6
|
+
export type Resolved<T> = T extends ConfigRef<infer U>
|
|
7
|
+
? U
|
|
8
|
+
: T extends readonly (infer E)[]
|
|
9
|
+
? Resolved<E>[]
|
|
10
|
+
: T extends (...args: never[]) => unknown
|
|
11
|
+
? T
|
|
12
|
+
: T extends object
|
|
13
|
+
? { [K in keyof T]: Resolved<T[K]> }
|
|
14
|
+
: T;
|
|
15
|
+
|
|
16
|
+
/** Manifest key of a recorded config reference. */
|
|
17
|
+
export const CONFIG_REF_KEY = "$config";
|
|
18
|
+
/** Beside it on a reference narrowed by a `select` case: the case's condition, `<ref>.<discriminator>=<value>`. */
|
|
19
|
+
export const CONFIG_CASE_KEY = "$case";
|
|
20
|
+
|
|
21
|
+
function isPlain(v: unknown): v is Record<string, unknown> {
|
|
22
|
+
if (typeof v !== "object" || v === null || Array.isArray(v)) return false;
|
|
23
|
+
const proto = Object.getPrototypeOf(v);
|
|
24
|
+
return proto === Object.prototype || proto === null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Every config entry referenced anywhere in `v` (plain objects and arrays only), by name. */
|
|
28
|
+
export function configRefsIn(v: unknown, into = new Map<string, AnyConfigEntry>()): AnyConfigEntry[] {
|
|
29
|
+
if (isConfigRef(v)) {
|
|
30
|
+
const { entry } = configRefTarget(v);
|
|
31
|
+
if (!into.has(entry.name)) into.set(entry.name, entry);
|
|
32
|
+
} else if (Array.isArray(v)) for (const x of v) configRefsIn(x, into);
|
|
33
|
+
else if (isPlain(v)) for (const x of Object.values(v)) configRefsIn(x, into);
|
|
34
|
+
return [...into.values()];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** `v` as manifest JSON: references become `{"$config": "target.beta.host"}`; literal keys starting with `$` are reserved. */
|
|
38
|
+
export function recordConfigRefs(v: unknown): unknown {
|
|
39
|
+
if (isConfigRef(v)) {
|
|
40
|
+
const { path, case: kase } = configRefTarget(v);
|
|
41
|
+
for (const seg of path) if (seg === "" || seg.includes(".")) throw new Error(`config reference '${configRefPath(v)}': field names must not be empty or contain '.'`);
|
|
42
|
+
return { [CONFIG_REF_KEY]: configRefPath(v), ...(kase ? { [CONFIG_CASE_KEY]: kase } : {}) };
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(v)) return v.map(recordConfigRefs);
|
|
45
|
+
if (isPlain(v)) {
|
|
46
|
+
const out: Record<string, unknown> = {};
|
|
47
|
+
for (const [k, x] of Object.entries(v)) {
|
|
48
|
+
if (k.startsWith("$")) throw new Error(`key '${k}': keys starting with '$' are reserved`);
|
|
49
|
+
out[k] = recordConfigRefs(x);
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
return v;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** `v` with every reference replaced by its value in `config` (the step's parsed config entries). */
|
|
57
|
+
export function resolveConfigRefs<T>(v: T, config: Readonly<Record<string, unknown>>): Resolved<T> {
|
|
58
|
+
if (isConfigRef(v)) {
|
|
59
|
+
const { entry, path } = configRefTarget(v);
|
|
60
|
+
if (!Object.hasOwn(config, entry.name)) throw new Error(`config entry '${entry.name}' was not delivered to the step`);
|
|
61
|
+
let cur: unknown = config[entry.name];
|
|
62
|
+
for (const seg of path) cur = cur !== null && typeof cur === "object" ? (cur as Record<string, unknown>)[seg] : undefined;
|
|
63
|
+
return cur as Resolved<T>;
|
|
64
|
+
}
|
|
65
|
+
if (Array.isArray(v)) return v.map((x) => resolveConfigRefs(x, config)) as Resolved<T>;
|
|
66
|
+
if (isPlain(v)) {
|
|
67
|
+
const out: Record<string, unknown> = {};
|
|
68
|
+
for (const [k, x] of Object.entries(v)) out[k] = resolveConfigRefs(x, config);
|
|
69
|
+
return out as Resolved<T>;
|
|
70
|
+
}
|
|
71
|
+
return v as Resolved<T>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Literal `v` (no reference inside), so define-time checks can run on it. */
|
|
75
|
+
export function isLiteral(v: unknown): boolean {
|
|
76
|
+
return configRefsIn(v).length === 0;
|
|
77
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { Secret } from "./secrets.ts";
|
|
3
|
+
|
|
4
|
+
const CONFIG_REF = Symbol.for("olve.configRef");
|
|
5
|
+
declare const VALUE: unique symbol;
|
|
6
|
+
|
|
7
|
+
interface RefTarget {
|
|
8
|
+
readonly entry: AnyConfigEntry;
|
|
9
|
+
readonly path: readonly string[];
|
|
10
|
+
/** Set on a reference narrowed by a `select` case: `<ref>.<discriminator>=<value>`. */
|
|
11
|
+
readonly case?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A typed reference to a config value (`TARGET.beta.host`), accepted by library steps in place of a
|
|
16
|
+
* literal. It is recorded in the manifest as `{"$config": "target.beta.host"}` and resolved from the
|
|
17
|
+
* run's config when the step runs; the step receives the entry automatically.
|
|
18
|
+
*/
|
|
19
|
+
export interface ConfigRef<T = unknown> {
|
|
20
|
+
readonly [CONFIG_REF]: RefTarget;
|
|
21
|
+
readonly [VALUE]?: T;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A literal or a reference to a config value of the same type. */
|
|
25
|
+
export type Configurable<T> = T | ConfigRef<T>;
|
|
26
|
+
|
|
27
|
+
type Fields<T> = T extends readonly unknown[] ? never : T extends object ? T : never;
|
|
28
|
+
type Children<T> = [Fields<NonNullable<T>>] extends [never]
|
|
29
|
+
? {}
|
|
30
|
+
: { readonly [K in keyof Fields<NonNullable<T>> & string]-?: ConfigNode<Fields<NonNullable<T>>[K] | (undefined extends T ? undefined : never)> };
|
|
31
|
+
/** A reference to a config value, and to each of its fields. */
|
|
32
|
+
export type ConfigNode<T> = ConfigRef<T> & Children<T>;
|
|
33
|
+
|
|
34
|
+
/** Fields of an entry that are its own properties; reach config fields with these names through `$`. */
|
|
35
|
+
export type ReservedConfigField = "kind" | "name" | "description" | "schema" | "$";
|
|
36
|
+
|
|
37
|
+
/** A named, typed value stored on the instance (one YAML document per pipeline), declared by the steps that use it. */
|
|
38
|
+
export interface ConfigEntryBase<N extends string = string, S extends z.ZodType = z.ZodType> {
|
|
39
|
+
readonly kind: "config";
|
|
40
|
+
readonly name: N;
|
|
41
|
+
readonly description: string;
|
|
42
|
+
readonly schema: S;
|
|
43
|
+
}
|
|
44
|
+
export type ConfigEntry<N extends string = string, S extends z.ZodType = z.ZodType> = ConfigEntryBase<N, S> &
|
|
45
|
+
ConfigRef<z.output<S>> &
|
|
46
|
+
Omit<Children<z.output<S>>, ReservedConfigField> & { readonly $: ConfigNode<z.output<S>> };
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
48
|
+
export type AnyConfigEntry = ConfigEntryBase<string, any>;
|
|
49
|
+
|
|
50
|
+
/** Config names are YAML keys: camelCase identifiers. */
|
|
51
|
+
export const CONFIG_NAME = /^[a-z][a-zA-Z0-9]*$/;
|
|
52
|
+
|
|
53
|
+
const NOT_REFS = new Set<PropertyKey>(["then", "toJSON", "constructor", "valueOf", "toString", "asymmetricMatch", "$$typeof", "nodeType"]);
|
|
54
|
+
|
|
55
|
+
function node(entry: AnyConfigEntry, path: readonly string[], kase?: string): unknown {
|
|
56
|
+
const target = Object.freeze({ [CONFIG_REF]: Object.freeze({ entry, path, ...(kase ? { case: kase } : {}) }) });
|
|
57
|
+
return new Proxy(target, {
|
|
58
|
+
get(t, prop) {
|
|
59
|
+
if (prop === CONFIG_REF) return t[CONFIG_REF];
|
|
60
|
+
if (typeof prop === "symbol" || NOT_REFS.has(prop)) return undefined;
|
|
61
|
+
return node(entry, [...path, prop], kase);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** `ref` narrowed to the union member whose `discriminator` is `value` (a `select` case). */
|
|
67
|
+
export function narrowConfigRef(ref: ConfigRef, discriminator: string, value: string): unknown {
|
|
68
|
+
const { entry, path } = ref[CONFIG_REF];
|
|
69
|
+
return node(entry, path, `${[entry.name, ...path].join(".")}.${discriminator}=${value}`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function config<const N extends string, S extends z.ZodType>(name: N, description: string, schema: S): ConfigEntry<N, S> {
|
|
73
|
+
const ref = { entry: undefined as unknown as AnyConfigEntry, path: [] as readonly string[] };
|
|
74
|
+
const target = Object.freeze({ kind: "config" as const, name, description, schema, [CONFIG_REF]: ref });
|
|
75
|
+
const entry: unknown = new Proxy(target, {
|
|
76
|
+
get(t, prop) {
|
|
77
|
+
if (prop in t) return t[prop as keyof typeof t];
|
|
78
|
+
if (typeof prop === "symbol" || NOT_REFS.has(prop)) return undefined;
|
|
79
|
+
if (prop === "$") return node(entry as AnyConfigEntry, []);
|
|
80
|
+
return node(entry as AnyConfigEntry, [prop]);
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
ref.entry = entry as AnyConfigEntry;
|
|
84
|
+
Object.freeze(ref);
|
|
85
|
+
return entry as ConfigEntry<N, S>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function isConfigEntry(v: unknown): v is AnyConfigEntry {
|
|
89
|
+
return typeof v === "object" && v !== null && (v as { kind?: unknown }).kind === "config" && (v as { schema?: unknown }).schema instanceof z.ZodType;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function isConfigRef(v: unknown): v is ConfigRef {
|
|
93
|
+
return typeof v === "object" && v !== null && (v as Record<symbol, unknown>)[CONFIG_REF] !== undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The entry and field path a reference points at, and the `select` case it was narrowed by. */
|
|
97
|
+
export function configRefTarget(ref: ConfigRef): { entry: AnyConfigEntry; path: readonly string[]; case?: string } {
|
|
98
|
+
return ref[CONFIG_REF];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** `target.beta.host` */
|
|
102
|
+
export function configRefPath(ref: ConfigRef): string {
|
|
103
|
+
const { entry, path } = configRefTarget(ref);
|
|
104
|
+
return [entry.name, ...path].join(".");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export const SECRET_REF_KIND = "secretRef";
|
|
108
|
+
|
|
109
|
+
/** A config field that names a secret: `{ secret: NAME }` in YAML. The value never enters the config. */
|
|
110
|
+
export function secretRef<const N extends string>(s: Secret<N>) {
|
|
111
|
+
return z.strictObject({ secret: z.literal(s.name) }).meta({ "x-olve-kind": SECRET_REF_KIND });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** `ctx.config`: exactly the entries a step declares, after parsing (defaults applied). */
|
|
115
|
+
export type ConfigValues<C extends AnyConfigEntry> = { readonly [E in C as E["name"]]: z.output<E["schema"]> };
|
|
116
|
+
|
|
117
|
+
// ─── select: branching on a config union ────────────────────────────────────────────────────
|
|
118
|
+
|
|
119
|
+
type IsLiteralString<T> = [T] extends [string] ? (string extends T ? false : true) : false;
|
|
120
|
+
/** The fields of union `U` whose type is a string literal in every member: its discriminator(s). */
|
|
121
|
+
export type DiscriminatorOf<U> = { [K in keyof U & string]-?: IsLiteralString<U[K]> extends true ? K : never }[keyof U & string];
|
|
122
|
+
/** The member of `U` whose discriminator `D` is `V`. */
|
|
123
|
+
export type VariantOf<U, D extends string, V> = Extract<U, { readonly [K in D]: V }>;
|
|
124
|
+
|
|
125
|
+
function unwrap(schema: z.ZodType): z.ZodType {
|
|
126
|
+
let s = schema as unknown as { _zod: { def: { innerType?: z.ZodType } } };
|
|
127
|
+
for (let i = 0; i < 16 && s._zod.def.innerType; i++) s = s._zod.def.innerType as never;
|
|
128
|
+
return s as unknown as z.ZodType;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The discriminated union a reference points at: its discriminator and, per value, nothing else.
|
|
133
|
+
* Undefined when the referenced value is not a `z.discriminatedUnion`.
|
|
134
|
+
*/
|
|
135
|
+
export function unionAt(ref: ConfigRef): { discriminator: string; values: string[] } | undefined {
|
|
136
|
+
const { entry, path } = configRefTarget(ref);
|
|
137
|
+
let s = unwrap(entry.schema as z.ZodType) as unknown as { _zod: { def: Record<string, unknown> } };
|
|
138
|
+
for (const seg of path) {
|
|
139
|
+
const shape = s._zod.def.shape as Record<string, z.ZodType> | undefined;
|
|
140
|
+
if (!shape || !(seg in shape)) return undefined;
|
|
141
|
+
s = unwrap(shape[seg]!) as never;
|
|
142
|
+
}
|
|
143
|
+
const def = s._zod.def as { discriminator?: string; options?: { _zod: { propValues?: Record<string, Set<unknown>> } }[] };
|
|
144
|
+
if (typeof def.discriminator !== "string" || !Array.isArray(def.options)) return undefined;
|
|
145
|
+
const values = def.options.flatMap((o) => [...(o._zod.propValues?.[def.discriminator!] ?? [])]).filter((v): v is string => typeof v === "string");
|
|
146
|
+
return { discriminator: def.discriminator, values };
|
|
147
|
+
}
|
package/src/core/handlers.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IMPLEMENTATION, SKIPPED, type SkippedBrand } from "./brands.ts";
|
|
2
2
|
import type { Ctx } from "../ctx/types.ts";
|
|
3
|
-
import type { AnyStep, LibraryStep, OutputValue, SecretsOf, StepInput } from "./steps.ts";
|
|
3
|
+
import type { AnyStep, ConfigOf, LibraryStep, OutputValue, SecretsOf, StepInput } from "./steps.ts";
|
|
4
4
|
import type { Awaitable } from "./types.ts";
|
|
5
5
|
|
|
6
6
|
/** Returned by `ctx.skip(reason)`: the step produced no output on purpose. */
|
|
@@ -16,7 +16,7 @@ export function isSkipped(v: unknown): v is Skipped {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export type HandlerResult<S extends AnyStep> = OutputValue<S> | Skipped;
|
|
19
|
-
export type Handler<S extends AnyStep> = (input: StepInput<S["input"]>, ctx: Ctx<SecretsOf<S>>) => Awaitable<HandlerResult<S>>;
|
|
19
|
+
export type Handler<S extends AnyStep> = (input: StepInput<S["input"]>, ctx: Ctx<SecretsOf<S>, ConfigOf<S>>) => Awaitable<HandlerResult<S>>;
|
|
20
20
|
|
|
21
21
|
export interface Implementation<S extends AnyStep = AnyStep> {
|
|
22
22
|
readonly step: S;
|
|
@@ -34,7 +34,7 @@ type NoExtraFields<R, T> = R extends Skipped | void
|
|
|
34
34
|
*/
|
|
35
35
|
export function implement<S extends AnyStep, R extends HandlerResult<S>>(
|
|
36
36
|
step: S & (S extends LibraryStep ? "❌ library steps already carry their implementation" : unknown),
|
|
37
|
-
handler: (input: StepInput<S["input"]>, ctx: Ctx<SecretsOf<S>>) => Awaitable<R & NoExtraFields<R, OutputValue<S>>>,
|
|
37
|
+
handler: (input: StepInput<S["input"]>, ctx: Ctx<SecretsOf<S>, ConfigOf<S>>) => Awaitable<R & NoExtraFields<R, OutputValue<S>>>,
|
|
38
38
|
): Implementation<S> {
|
|
39
39
|
return Object.freeze({ [IMPLEMENTATION]: true, step, handler: handler as unknown as Handler<S> }) as Implementation<S>;
|
|
40
40
|
}
|
package/src/core/steps.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { z } from "zod";
|
|
2
2
|
import { STEP, type StepBrand } from "./brands.ts";
|
|
3
3
|
import type { AnyModel, Infer, Optional } from "./models.ts";
|
|
4
|
+
import type { AnyConfigEntry } from "./config.ts";
|
|
5
|
+
import { configRefsIn } from "./config-refs.ts";
|
|
4
6
|
import type { Secret } from "./secrets.ts";
|
|
5
7
|
import type { Simplify } from "./types.ts";
|
|
6
8
|
|
|
@@ -17,13 +19,14 @@ export type InferEntry<E> =
|
|
|
17
19
|
export type StepInput<I> = Simplify<{ -readonly [K in keyof I]: InferEntry<I[K]> }>;
|
|
18
20
|
|
|
19
21
|
/** `custom` steps have a handler file; the others are library steps implemented by the SDK. */
|
|
20
|
-
export type StepKind = "custom" | "script" | "gitHubSource" | "imageBuild" | "helmDeployOverSsh";
|
|
22
|
+
export type StepKind = "custom" | "script" | "gitHubSource" | "imageBuild" | "helmDeployOverSsh" | "helmDeployInCluster" | "composeDeploy";
|
|
21
23
|
|
|
22
24
|
export interface Step<
|
|
23
25
|
N extends string = string,
|
|
24
26
|
I extends InputSpec = InputSpec,
|
|
25
27
|
O extends AnyModel | null = AnyModel | null,
|
|
26
28
|
Sec extends string = string,
|
|
29
|
+
Cfg extends AnyConfigEntry = never,
|
|
27
30
|
> {
|
|
28
31
|
readonly [StepBrand]: true;
|
|
29
32
|
readonly kind: StepKind;
|
|
@@ -33,8 +36,12 @@ export interface Step<
|
|
|
33
36
|
readonly input: I;
|
|
34
37
|
readonly output: O;
|
|
35
38
|
readonly secrets: readonly Secret<Sec>[];
|
|
39
|
+
/** Config entries the step receives as `ctx.config`. */
|
|
40
|
+
readonly config: readonly Cfg[];
|
|
36
41
|
readonly timeoutSeconds?: number;
|
|
37
42
|
readonly retries?: number;
|
|
43
|
+
/** The step pod gets the instance's step ServiceAccount token (`Kubernetes:StepServiceAccount`). */
|
|
44
|
+
readonly clusterAccess?: boolean;
|
|
38
45
|
/** Library steps carry their own implementation and config. */
|
|
39
46
|
readonly builtin?: LibraryImplementation;
|
|
40
47
|
}
|
|
@@ -47,10 +54,12 @@ export interface LibraryImplementation {
|
|
|
47
54
|
}
|
|
48
55
|
export type LibraryStep = { readonly builtin: LibraryImplementation };
|
|
49
56
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
50
|
-
export type AnyStep = Step<string, any, AnyModel | null, any>;
|
|
57
|
+
export type AnyStep = Step<string, any, AnyModel | null, any, any>;
|
|
51
58
|
|
|
52
59
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
|
-
export type SecretsOf<S> = S extends Step<any, any, any, infer Sec> ? Sec : never;
|
|
60
|
+
export type SecretsOf<S> = S extends Step<any, any, any, infer Sec, any> ? Sec : never;
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
62
|
+
export type ConfigOf<S> = S extends Step<any, any, any, any, infer C> ? C : never;
|
|
54
63
|
export type OutputValue<S extends AnyStep> = S["output"] extends AnyModel ? Infer<S["output"]> : void;
|
|
55
64
|
|
|
56
65
|
/** Step names must be literals (identity, duplicate detection and mapper keys depend on it). */
|
|
@@ -61,7 +70,7 @@ export const STEP_NAME = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
|
61
70
|
/** Names that cannot be steps: `input` is the pipeline input's root in mappings. */
|
|
62
71
|
export const RESERVED_STEP_NAMES: readonly string[] = ["input"];
|
|
63
72
|
|
|
64
|
-
export interface StepDef<N extends string, I extends InputSpec, O extends AnyModel | null, Sec extends readonly Secret[]> {
|
|
73
|
+
export interface StepDef<N extends string, I extends InputSpec, O extends AnyModel | null, Sec extends readonly Secret[], Cfg extends readonly AnyConfigEntry[] = []> {
|
|
65
74
|
name: LiteralName<N>;
|
|
66
75
|
/** What the step does, shown in the UI. Required; may be empty. */
|
|
67
76
|
description: string;
|
|
@@ -70,12 +79,23 @@ export interface StepDef<N extends string, I extends InputSpec, O extends AnyMod
|
|
|
70
79
|
input?: I;
|
|
71
80
|
output?: O;
|
|
72
81
|
secrets?: Sec;
|
|
82
|
+
/** Config entries delivered as `ctx.config` (typed to exactly these). */
|
|
83
|
+
config?: Cfg;
|
|
73
84
|
timeoutSeconds?: number;
|
|
74
85
|
/** Automatic retries (an idempotency claim). Default 0. */
|
|
75
86
|
retries?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Ask for a Kubernetes API token in the step pod: the instance's step ServiceAccount
|
|
89
|
+
* (`Kubernetes:StepServiceAccount`), if it grants one. Default false (no token).
|
|
90
|
+
*/
|
|
91
|
+
clusterAccess?: boolean;
|
|
76
92
|
}
|
|
77
93
|
|
|
78
|
-
|
|
94
|
+
/** `configFrom`: a library step's definition; the config entries it references are added to the step's `config`. */
|
|
95
|
+
export function makeStep(kind: StepKind, def: Record<string, unknown>, builtin?: LibraryImplementation, configFrom?: unknown): AnyStep {
|
|
96
|
+
const config = new Map<string, AnyConfigEntry>();
|
|
97
|
+
for (const e of (def.config as readonly AnyConfigEntry[] | undefined) ?? []) if (!config.has(e.name)) config.set(e.name, e);
|
|
98
|
+
configRefsIn(configFrom, config);
|
|
79
99
|
const s = {
|
|
80
100
|
[STEP]: true,
|
|
81
101
|
kind,
|
|
@@ -85,8 +105,10 @@ export function makeStep(kind: StepKind, def: Record<string, unknown>, builtin?:
|
|
|
85
105
|
input: Object.freeze({ ...((def.input as object | undefined) ?? {}) }),
|
|
86
106
|
output: def.output ?? null,
|
|
87
107
|
secrets: Object.freeze([...((def.secrets as readonly Secret[] | undefined) ?? [])]),
|
|
108
|
+
config: Object.freeze([...config.values()]),
|
|
88
109
|
...(def.timeoutSeconds !== undefined ? { timeoutSeconds: def.timeoutSeconds } : {}),
|
|
89
110
|
...(def.retries !== undefined ? { retries: def.retries } : {}),
|
|
111
|
+
...(def.clusterAccess === true ? { clusterAccess: true } : {}),
|
|
90
112
|
...(builtin ? { builtin } : {}),
|
|
91
113
|
};
|
|
92
114
|
return Object.freeze(s) as unknown as AnyStep;
|
|
@@ -102,6 +124,7 @@ export function step<
|
|
|
102
124
|
const I extends InputSpec = {},
|
|
103
125
|
O extends AnyModel | null = null,
|
|
104
126
|
const Sec extends readonly Secret[] = [],
|
|
105
|
-
|
|
127
|
+
const Cfg extends readonly AnyConfigEntry[] = [],
|
|
128
|
+
>(def: StepDef<N, I, O, Sec, Cfg>): Step<N, I, O, Sec[number]["name"], Cfg[number]> {
|
|
106
129
|
return makeStep("custom", def as never) as never;
|
|
107
130
|
}
|
package/src/ctx/index.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface CtxOptions {
|
|
|
12
12
|
secrets: readonly Secret[];
|
|
13
13
|
/** Secret values that take precedence over the environment (tests). */
|
|
14
14
|
secretValues?: Readonly<Record<string, string>>;
|
|
15
|
+
/** Parsed config entries of the step (`ctx.config`). */
|
|
16
|
+
config?: Readonly<Record<string, unknown>>;
|
|
15
17
|
paths: { root: string; workFiles: string; tmp: string };
|
|
16
18
|
signal: AbortSignal;
|
|
17
19
|
/** Where log lines go (default stderr). */
|
|
@@ -102,6 +104,7 @@ export function createCtx(o: CtxOptions): Ctx {
|
|
|
102
104
|
const filesDeps = { run: o.run, workFiles: o.paths.workFiles, tmpDir: o.paths.tmp, root: o.paths.root, signal: o.signal, log };
|
|
103
105
|
return {
|
|
104
106
|
secrets: createSecrets(o.secrets, o.secretValues) as Ctx["secrets"],
|
|
107
|
+
config: Object.freeze({ ...o.config }) as Ctx["config"],
|
|
105
108
|
sh,
|
|
106
109
|
exec,
|
|
107
110
|
readText: async (p) => readFileSync(resolve(p), "utf8"),
|
package/src/ctx/sh.ts
CHANGED
|
@@ -85,7 +85,8 @@ export function createSh(deps: ShDeps): { sh: Sh; exec: (argv: readonly string[]
|
|
|
85
85
|
const scriptPath = typeof cmd === "object" ? cmd.script : isEmbeddedScript(cmd) ? cmd : undefined;
|
|
86
86
|
if (scriptPath === undefined) {
|
|
87
87
|
const shell = detectShell(deps.shellCandidates);
|
|
88
|
-
|
|
88
|
+
// `sh -c cmd sh a b`: the args are $1, $2… of the command.
|
|
89
|
+
return { argv: [shell, "-c", cmd as string, "sh", ...args], what: `command \`${truncate(cmd as string)}\`` };
|
|
89
90
|
}
|
|
90
91
|
const file = materialize(scriptPath);
|
|
91
92
|
const first = readFileSync(file, "utf8").split("\n", 1)[0] ?? "";
|
package/src/ctx/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Skipped } from "../core/handlers.ts";
|
|
2
2
|
import type { FileRef } from "../core/models.ts";
|
|
3
|
+
import type { AnyConfigEntry, ConfigValues } from "../core/config.ts";
|
|
3
4
|
import type { DeclaredSecrets } from "../core/secrets.ts";
|
|
4
5
|
|
|
5
6
|
/** Facts about the current execution, from the service (see README "Environment"). */
|
|
@@ -79,9 +80,11 @@ export interface OutputFileOptions {
|
|
|
79
80
|
name?: string;
|
|
80
81
|
}
|
|
81
82
|
|
|
82
|
-
export interface Ctx<Sec extends string = string> {
|
|
83
|
+
export interface Ctx<Sec extends string = string, Cfg extends AnyConfigEntry = never> {
|
|
83
84
|
/** Exactly the secrets the step declares, read from the environment. */
|
|
84
85
|
readonly secrets: DeclaredSecrets<Sec>;
|
|
86
|
+
/** Exactly the config entries the step declares, from the run's config snapshot (defaults applied). */
|
|
87
|
+
readonly config: ConfigValues<Cfg>;
|
|
85
88
|
readonly sh: Sh;
|
|
86
89
|
/** Run a binary directly, without a shell (works in shell-less images). Rejects on non-zero exit. */
|
|
87
90
|
exec(argv: readonly string[], opts?: ExecOptions): Promise<void>;
|