@olivervea/pipelines 0.0.0-stage → 2.0.0-beta.10
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/LICENSE +21 -0
- package/README.md +467 -2
- package/manifest.schema.json +674 -0
- package/package.json +60 -4
- package/src/assets.d.ts +10 -0
- package/src/build/build.ts +127 -0
- package/src/build/cli.ts +52 -0
- package/src/build/elf.ts +35 -0
- package/src/build/hashes.ts +96 -0
- package/src/build/project.ts +107 -0
- package/src/builder/pipeline.ts +373 -0
- package/src/builder/refs.ts +26 -0
- package/src/core/brands.ts +15 -0
- package/src/core/config-refs.ts +77 -0
- package/src/core/config.ts +147 -0
- package/src/core/handlers.ts +50 -0
- package/src/core/models.ts +69 -0
- package/src/core/origin.ts +20 -0
- package/src/core/secrets.ts +13 -0
- package/src/core/steps.ts +138 -0
- package/src/core/types.ts +6 -0
- package/src/core/webhooks.ts +30 -0
- package/src/ctx/files.ts +161 -0
- package/src/ctx/index.ts +122 -0
- package/src/ctx/sh.ts +197 -0
- package/src/ctx/types.ts +116 -0
- package/src/describe/describe.ts +565 -0
- package/src/describe/manifest-schema.ts +179 -0
- package/src/describe/mapper-source.ts +238 -0
- package/src/describe/record.ts +107 -0
- package/src/dev/cli.ts +87 -0
- package/src/harness/env.ts +58 -0
- package/src/harness/execute.ts +97 -0
- package/src/harness/exit.ts +19 -0
- package/src/harness/index.ts +9 -0
- package/src/harness/main.ts +270 -0
- package/src/harness/result.ts +75 -0
- package/src/harness/validate.ts +92 -0
- package/src/index.ts +15 -0
- package/src/library/checks.ts +23 -0
- package/src/library/compose-deploy.ts +96 -0
- package/src/library/github-source.ts +100 -0
- package/src/library/helm-deploy-ssh.ts +358 -0
- package/src/library/helm-deploy.ts +271 -0
- package/src/library/http-health.ts +34 -0
- package/src/library/image-build.ts +418 -0
- package/src/library/index.ts +18 -0
- package/src/library/models.ts +79 -0
- package/src/library/ssh.ts +50 -0
- package/src/testing/index.ts +82 -0
- package/src/util/canonical.ts +19 -0
- package/src/util/handler-hash.ts +22 -0
- package/src/util/hash.ts +22 -0
- package/src/util/tar.ts +167 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// steps.composeDeploy — ask a Docker Compose deployer to recreate a service with an image. The step is
|
|
2
|
+
// thin on purpose: the deployer (pull, recreate, health, rollback) lives beside the compose project
|
|
3
|
+
// and is fixed there, behind a versioned HTTP API, without editing pipelines.
|
|
4
|
+
import type { Ctx } from "../ctx/types.ts";
|
|
5
|
+
import type { Configurable } from "../core/config.ts";
|
|
6
|
+
import { resolveConfigRefs } from "../core/config-refs.ts";
|
|
7
|
+
import { implement, type Implementation } from "../core/handlers.ts";
|
|
8
|
+
import type { Secret } from "../core/secrets.ts";
|
|
9
|
+
import { step, type LiteralName, type Step } from "../core/steps.ts";
|
|
10
|
+
import { httpHealthCheck } from "./http-health.ts";
|
|
11
|
+
import { HelmRelease, PushedImage } from "./models.ts";
|
|
12
|
+
|
|
13
|
+
/** The step's own image: the work is one HTTP call from the harness. */
|
|
14
|
+
export const COMPOSE_DEPLOY_IMAGE = "alpine:3.22";
|
|
15
|
+
|
|
16
|
+
/** A resolved `kind: compose` target. */
|
|
17
|
+
export interface ComposeTargetValue {
|
|
18
|
+
kind: "compose";
|
|
19
|
+
deployerUrl: string;
|
|
20
|
+
service: string;
|
|
21
|
+
token: { secret: string };
|
|
22
|
+
healthUrl?: string | undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What the deployer answers to `POST /v1/deploy`. */
|
|
26
|
+
export interface ComposeDeployResult {
|
|
27
|
+
status: "deployed" | "rolled-back" | "failed";
|
|
28
|
+
service: string;
|
|
29
|
+
image: string;
|
|
30
|
+
previousImage?: string;
|
|
31
|
+
message?: string;
|
|
32
|
+
log?: string[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ComposeDeployDef<N extends string, K extends string> {
|
|
36
|
+
name: LiteralName<N>;
|
|
37
|
+
/** What the step does, shown in the UI. Required; may be empty. */
|
|
38
|
+
description: string;
|
|
39
|
+
/** A compose target, usually a `select` case's reference. */
|
|
40
|
+
target: Configurable<ComposeTargetValue>;
|
|
41
|
+
/** The secret the target's `token` names (declared by the step). */
|
|
42
|
+
token: Secret<K>;
|
|
43
|
+
timeoutSeconds?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const input = { image: PushedImage };
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Deploys a registry image (by digest) to a compose service: `POST <deployerUrl>/v1/deploy
|
|
50
|
+
* { service, image: <repository>@<digest> }` with the target's bearer token. The deployer pulls the
|
|
51
|
+
* image, recreates the service, waits for its health and rolls back on failure; the step reports its
|
|
52
|
+
* answer, then (with the target's `healthUrl`) checks health itself. Output: a `HelmRelease` with the
|
|
53
|
+
* service as `release` and `compose` as `namespace`, so it can share a step name with the helm deploys.
|
|
54
|
+
*/
|
|
55
|
+
export function composeDeploy<const N extends string, const K extends string>(
|
|
56
|
+
def: ComposeDeployDef<N, K>,
|
|
57
|
+
): Implementation<Step<N, typeof input, typeof HelmRelease, K>> {
|
|
58
|
+
const contract = step({
|
|
59
|
+
name: def.name,
|
|
60
|
+
description: def.description,
|
|
61
|
+
kind: "composeDeploy",
|
|
62
|
+
image: COMPOSE_DEPLOY_IMAGE,
|
|
63
|
+
input,
|
|
64
|
+
output: HelmRelease,
|
|
65
|
+
secrets: [def.token],
|
|
66
|
+
timeoutSeconds: def.timeoutSeconds,
|
|
67
|
+
settings: { target: def.target, token: def.token.name },
|
|
68
|
+
});
|
|
69
|
+
return implement(contract, async ({ image }, ctx: Ctx) => {
|
|
70
|
+
const target = resolveConfigRefs(def.target, ctx.config ?? {}) as ComposeTargetValue;
|
|
71
|
+
if (target?.kind !== "compose") throw new Error(`composeDeploy '${def.name}': the target is kind '${String(target?.kind)}', not 'compose'`);
|
|
72
|
+
if (target.token.secret !== def.token.name) throw new Error(`composeDeploy '${def.name}': the target's token names secret ${target.token.secret}, but the step declares ${def.token.name}`);
|
|
73
|
+
const token = (ctx.secrets as Record<string, string>)[def.token.name];
|
|
74
|
+
const ref = `${image.repository}@${image.digest}`;
|
|
75
|
+
const url = `${target.deployerUrl.replace(/\/+$/, "")}/v1/deploy`;
|
|
76
|
+
ctx.log("deploying", { service: target.service, image: ref, deployer: target.deployerUrl });
|
|
77
|
+
const res = await ctx.http.fetch(url, {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
|
|
80
|
+
body: JSON.stringify({ service: target.service, image: ref }),
|
|
81
|
+
});
|
|
82
|
+
const text = await res.text();
|
|
83
|
+
let result: ComposeDeployResult | undefined;
|
|
84
|
+
try {
|
|
85
|
+
result = JSON.parse(text) as ComposeDeployResult;
|
|
86
|
+
} catch {
|
|
87
|
+
// reported below
|
|
88
|
+
}
|
|
89
|
+
for (const line of result?.log ?? []) ctx.log(`deployer: ${line}`);
|
|
90
|
+
if (!res.ok || result?.status !== "deployed")
|
|
91
|
+
throw new Error(`the deployer answered HTTP ${res.status}: ${result ? `${result.status}${result.message ? `: ${result.message}` : ""}` : text.slice(0, 300)}`);
|
|
92
|
+
if (target.healthUrl) await httpHealthCheck(ctx, target.healthUrl, { attempts: 12, delaySeconds: 5 });
|
|
93
|
+
ctx.log("deployed", { service: target.service, image: ref, previous: result.previousImage ?? "-" });
|
|
94
|
+
return { release: target.service, namespace: "compose", image: ref };
|
|
95
|
+
}) as never;
|
|
96
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// steps.gitHubSource — a GitHub repository at a commit, as a `Source`.
|
|
2
|
+
//
|
|
3
|
+
// Runs entirely inside the harness (HTTP + tar extraction in Bun): no git, wget or tar is needed
|
|
4
|
+
// in the image, so v1's busybox-wget TLS footgun (`--no-check-certificate`) is gone.
|
|
5
|
+
import { mkdirSync, mkdtempSync, readdirSync, statSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import type { Ctx } from "../ctx/types.ts";
|
|
9
|
+
import type { Secret } from "../core/secrets.ts";
|
|
10
|
+
import type { Configurable } from "../core/config.ts";
|
|
11
|
+
import { resolveConfigRefs } from "../core/config-refs.ts";
|
|
12
|
+
import { implement, type Implementation } from "../core/handlers.ts";
|
|
13
|
+
import { step, type LiteralName, type Step } from "../core/steps.ts";
|
|
14
|
+
import { extractTar } from "../util/tar.ts";
|
|
15
|
+
import { Source } from "./models.ts";
|
|
16
|
+
|
|
17
|
+
/** Image the step runs in. The work happens in the harness, so any image would do. */
|
|
18
|
+
export const GITHUB_SOURCE_IMAGE = "alpine:3.22";
|
|
19
|
+
|
|
20
|
+
const input = { repo: z.string().regex(/^[\w.-]+\/[\w.-]+$/, "expected owner/name"), sha: z.string().min(1) };
|
|
21
|
+
export type GitHubSourceInput = typeof input;
|
|
22
|
+
|
|
23
|
+
export interface GitHubSourceDef<N extends string, T extends string> {
|
|
24
|
+
name: LiteralName<N>;
|
|
25
|
+
/** What the step does, shown in the UI. Required; may be empty. */
|
|
26
|
+
description: string;
|
|
27
|
+
/** Token sent to the GitHub API (needed for private repositories; raises the rate limit). */
|
|
28
|
+
token?: Secret<T>;
|
|
29
|
+
/** GitHub API base (default `https://api.github.com`; GitHub Enterprise: `https://<host>/api/v3`). */
|
|
30
|
+
apiUrl?: Configurable<string | undefined>;
|
|
31
|
+
timeoutSeconds?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const GITHUB_API_URL = "https://api.github.com";
|
|
35
|
+
|
|
36
|
+
/** A declared secret's value, or "" when it is not set. */
|
|
37
|
+
function optionalSecret(ctx: Pick<Ctx, "secrets">, name: string): string {
|
|
38
|
+
try {
|
|
39
|
+
return ((ctx.secrets as Record<string, string>)[name] ?? "").trim();
|
|
40
|
+
} catch {
|
|
41
|
+
return "";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Tarball URL of `repo` at `sha` (the API redirects to codeload.github.com). */
|
|
46
|
+
export const gitHubTarballUrl = (repo: string, sha: string, apiUrl = GITHUB_API_URL): string =>
|
|
47
|
+
`${apiUrl.replace(/\/+$/, "")}/repos/${repo}/tarball/${encodeURIComponent(sha)}`;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Downloads `repo` at `sha` from GitHub as a tarball and outputs it as a `Source` whose `tree` is the
|
|
51
|
+
* repository root (the tarball's single `<owner>-<repo>-<sha>/` directory, stripped).
|
|
52
|
+
* Input `{ repo: "owner/name", sha }` is a plain schema, so the stage needs a mapper.
|
|
53
|
+
*/
|
|
54
|
+
export function gitHubSource<const N extends string, const T extends string = never>(
|
|
55
|
+
def: GitHubSourceDef<N, T>,
|
|
56
|
+
): Implementation<Step<N, GitHubSourceInput, typeof Source, T>> {
|
|
57
|
+
const contract = step({
|
|
58
|
+
name: def.name,
|
|
59
|
+
description: def.description,
|
|
60
|
+
kind: "gitHubSource",
|
|
61
|
+
image: GITHUB_SOURCE_IMAGE,
|
|
62
|
+
input,
|
|
63
|
+
output: Source,
|
|
64
|
+
secrets: def.token ? [def.token] : [],
|
|
65
|
+
timeoutSeconds: def.timeoutSeconds,
|
|
66
|
+
settings: { ...(def.token ? { token: def.token.name } : {}), ...(def.apiUrl !== undefined ? { apiUrl: def.apiUrl } : {}) },
|
|
67
|
+
});
|
|
68
|
+
return implement(contract, async ({ repo, sha }, ctx: Ctx) => {
|
|
69
|
+
const headers: Record<string, string> = { accept: "application/vnd.github+json", "user-agent": "olve-pipelines" };
|
|
70
|
+
const token = def.token ? optionalSecret(ctx, def.token.name) : "";
|
|
71
|
+
if (token) headers.authorization = `Bearer ${token}`;
|
|
72
|
+
else if (def.token) ctx.log(`${def.token.name} is not set: fetching anonymously (public repositories only)`);
|
|
73
|
+
const url = gitHubTarballUrl(repo, sha, resolveConfigRefs(def.apiUrl, ctx.config ?? {}) ?? GITHUB_API_URL);
|
|
74
|
+
mkdirSync(ctx.tmpDir, { recursive: true });
|
|
75
|
+
const dir = mkdtempSync(join(ctx.tmpDir, "github-source-"));
|
|
76
|
+
const archive = join(dir, "repo.tar.gz");
|
|
77
|
+
|
|
78
|
+
ctx.log("fetching source", { repo, sha });
|
|
79
|
+
// Retries network errors and 5xx; a 4xx (bad sha, no access) fails at once.
|
|
80
|
+
const status = await ctx.retry({ attempts: 4, delaySeconds: 2, backoff: 2 }, async () => {
|
|
81
|
+
const res = await ctx.http.fetch(url, { headers });
|
|
82
|
+
if (res.status >= 500) throw new Error(`HTTP ${res.status} from ${url}`);
|
|
83
|
+
if (res.ok) await Bun.write(archive, res);
|
|
84
|
+
return res.status;
|
|
85
|
+
});
|
|
86
|
+
if (status < 200 || status >= 300) {
|
|
87
|
+
throw new Error(`cannot fetch ${repo}@${sha}: HTTP ${status} from ${url}${def.token ? "" : " (no token configured: a private repository needs one)"}`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const extracted = join(dir, "x");
|
|
91
|
+
mkdirSync(extracted);
|
|
92
|
+
await extractTar(archive, extracted);
|
|
93
|
+
// GitHub tarballs hold one top-level directory, plus a pax_global_header entry (the commit id)
|
|
94
|
+
// that some extractors materialise as a file.
|
|
95
|
+
const roots = readdirSync(extracted).filter((e) => e !== "pax_global_header" && statSync(join(extracted, e)).isDirectory());
|
|
96
|
+
if (roots.length !== 1) throw new Error(`unexpected tarball layout for ${repo}@${sha}: top-level entries ${JSON.stringify(readdirSync(extracted))}`);
|
|
97
|
+
const tree = await ctx.output.dir(join(extracted, roots[0]!), { name: "tree" });
|
|
98
|
+
return { repo, sha, tree };
|
|
99
|
+
}) as never;
|
|
100
|
+
}
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
// steps.helmDeployOverSsh — get built images onto a k3s host and helm-upgrade a release there, over
|
|
2
|
+
// SSH; optionally verify an HTTP health endpoint afterwards.
|
|
3
|
+
//
|
|
4
|
+
// Ported from v1's olve-lib.sh (olve_ssh_host, olve_image_import, olve_helm_deploy) and
|
|
5
|
+
// deploy-beta.sh (rollout wait + health loop); the footguns are kept as comments below.
|
|
6
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import type { Ctx } from "../ctx/types.ts";
|
|
9
|
+
import type { FileRef } from "../core/models.ts";
|
|
10
|
+
import type { Secret } from "../core/secrets.ts";
|
|
11
|
+
import type { Configurable } from "../core/config.ts";
|
|
12
|
+
import { resolveConfigRefs } from "../core/config-refs.ts";
|
|
13
|
+
import { implement, type Implementation } from "../core/handlers.ts";
|
|
14
|
+
import { step, type LiteralName, type Step } from "../core/steps.ts";
|
|
15
|
+
import type { Simplify } from "../core/types.ts";
|
|
16
|
+
import { httpHealthCheck, type HttpHealthCheckOptions } from "./http-health.ts";
|
|
17
|
+
import { BuiltImage, HelmRelease, PushedImage, Source } from "./models.ts";
|
|
18
|
+
import { shq, sshSession } from "./ssh.ts";
|
|
19
|
+
import { helmDeployOverSshTarget, type HelmDeploySshTargetDef, type HelmTargetDeployInput } from "./helm-deploy.ts";
|
|
20
|
+
|
|
21
|
+
/** Alpine: `apk add openssh-client` at run time gives ssh/scp (the health check runs in the harness). */
|
|
22
|
+
export const HELM_DEPLOY_IMAGE = "alpine:3.22";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Footgun: import via the k3s containerd socket, NOT the default one — kubelet only sees images in
|
|
26
|
+
* the k3s instance's `k8s.io` namespace (otherwise pods fail with ErrImageNeverPull).
|
|
27
|
+
*/
|
|
28
|
+
export const K3S_IMAGE_IMPORT = "sudo nerdctl --address /run/k3s/containerd/containerd.sock --namespace k8s.io load";
|
|
29
|
+
/** Registry images are pulled the same way before the rollout, so the rollout doesn't wait on the pull. */
|
|
30
|
+
export const K3S_IMAGE_PULL = "sudo nerdctl --address /run/k3s/containerd/containerd.sock --namespace k8s.io pull -q";
|
|
31
|
+
|
|
32
|
+
export const DEFAULT_ROLLOUT_TIMEOUT_SECONDS = 120;
|
|
33
|
+
|
|
34
|
+
type DefaultImages = { readonly image: "image" };
|
|
35
|
+
type ImageModel<R extends boolean> = R extends true ? typeof PushedImage : typeof BuiltImage;
|
|
36
|
+
export type HelmDeployOverSshInput<
|
|
37
|
+
Im extends Record<string, string> = DefaultImages,
|
|
38
|
+
R extends boolean = false,
|
|
39
|
+
Rf extends Record<string, string> = {},
|
|
40
|
+
> = Simplify<
|
|
41
|
+
{ readonly source: typeof Source } & { readonly [K in keyof Im]: ImageModel<R> } & { readonly [K in keyof Rf]: typeof PushedImage }
|
|
42
|
+
>;
|
|
43
|
+
|
|
44
|
+
export interface HelmDeployOverSshDef<
|
|
45
|
+
N extends string,
|
|
46
|
+
K extends string,
|
|
47
|
+
Im extends Record<string, string> = DefaultImages,
|
|
48
|
+
R extends boolean = false,
|
|
49
|
+
Rf extends Record<string, string> = {},
|
|
50
|
+
> {
|
|
51
|
+
name: LiteralName<N>;
|
|
52
|
+
/** What the step does, shown in the UI. Required; may be empty. */
|
|
53
|
+
description: string;
|
|
54
|
+
/** SSH target, `user@host`. The user needs passwordless sudo for nerdctl, and helm/kubectl with a kubeconfig. */
|
|
55
|
+
host: Configurable<string>;
|
|
56
|
+
/** Private key for `host`. */
|
|
57
|
+
sshKey: Secret<K>;
|
|
58
|
+
/** Helm release name. */
|
|
59
|
+
release: Configurable<string>;
|
|
60
|
+
namespace: Configurable<string>;
|
|
61
|
+
/** Chart directory inside the source tree, e.g. `helm`. */
|
|
62
|
+
chart: string;
|
|
63
|
+
/** Values files, relative to the chart directory (`-f`, in order). */
|
|
64
|
+
values?: Configurable<readonly string[]>;
|
|
65
|
+
/**
|
|
66
|
+
* Chart-specific `--set` flags (e.g. `{ "ingress.enabled": "false" }`). The image flags are added by
|
|
67
|
+
* the step and always win: `<prefix>.repository`, `<prefix>.tag`, `<prefix>.pullPolicy`.
|
|
68
|
+
*/
|
|
69
|
+
set?: Configurable<Readonly<Record<string, string>>>;
|
|
70
|
+
/**
|
|
71
|
+
* The images to deploy: step input key → the chart's values prefix for that image. Each input is a
|
|
72
|
+
* `BuiltImage` (or `PushedImage` with `fromRegistry`). Default `{ image: "image" }`; several images
|
|
73
|
+
* need a mapper, e.g. `{ server: "image", web: "web.image" }`. `{}` deploys the chart alone.
|
|
74
|
+
*/
|
|
75
|
+
images?: Im & { readonly source?: never };
|
|
76
|
+
/**
|
|
77
|
+
* The images are `PushedImage`s from a registry (`imageBuild` with `push`): pulled on the host
|
|
78
|
+
* first, then `pullPolicy=IfNotPresent`. Default: `BuiltImage` tarballs, imported on the host, then
|
|
79
|
+
* `pullPolicy=Never`.
|
|
80
|
+
*/
|
|
81
|
+
fromRegistry?: R & (boolean extends R ? "❌ fromRegistry must be a literal true or false (use `as const` on a shared config object)" : unknown);
|
|
82
|
+
/**
|
|
83
|
+
* Registry images the chart only references (not run by the release's own pods, e.g. an image the
|
|
84
|
+
* app starts Jobs with): step input key → values prefix. Each input is a `PushedImage`; the step sets
|
|
85
|
+
* `<prefix>.repository`, `<prefix>.tag` and `<prefix>.digest` and does not pull it on the host. Works
|
|
86
|
+
* next to tarball `images`.
|
|
87
|
+
*/
|
|
88
|
+
imageRefs?: Rf & { readonly source?: never };
|
|
89
|
+
/** Rollouts to wait for, in order: `deploy/<name>` for a bare name. Default: the release name, or none without images or with helm's wait. */
|
|
90
|
+
deployments?: readonly string[];
|
|
91
|
+
/** Per rollout (`kubectl rollout status --timeout`), or for helm's own wait. Default 120. */
|
|
92
|
+
rolloutTimeoutSeconds?: number;
|
|
93
|
+
/**
|
|
94
|
+
* Pull the dependencies `Chart.yaml` lists (`charts/` is usually not committed) into the chart on the
|
|
95
|
+
* host before installing: `helm pull <name> --repo <repository> --version <version>`, or the `oci://`
|
|
96
|
+
* reference. Default false.
|
|
97
|
+
*/
|
|
98
|
+
dependencies?: boolean;
|
|
99
|
+
/** `helm upgrade --wait`: helm waits for all of the release's resources to be ready. Default false. */
|
|
100
|
+
helmWait?: boolean;
|
|
101
|
+
/** `helm upgrade --atomic`: wait like `helmWait`, and roll back when the upgrade fails. Default false. */
|
|
102
|
+
atomic?: boolean;
|
|
103
|
+
/** After the rollouts, GET this URL until it returns 2xx (`httpHealthCheck`: 5 × 5 s unless given), or fail the step. */
|
|
104
|
+
healthUrl?: Configurable<string> | ({ url: Configurable<string> } & Pick<HttpHealthCheckOptions, "attempts" | "delaySeconds">);
|
|
105
|
+
timeoutSeconds?: number;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface HelmImageFlags {
|
|
109
|
+
prefix: string;
|
|
110
|
+
repository: string;
|
|
111
|
+
tag: string;
|
|
112
|
+
pullPolicy: "Never" | "IfNotPresent";
|
|
113
|
+
/** Pins the image: `<prefix>.digest` (the chart renders `repository:tag@digest`). */
|
|
114
|
+
digest?: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A registry image the chart only references (`imageRefs`). */
|
|
118
|
+
export interface HelmImageRefFlags {
|
|
119
|
+
prefix: string;
|
|
120
|
+
repository: string;
|
|
121
|
+
tag: string;
|
|
122
|
+
digest: string;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface HelmFlagOptions {
|
|
126
|
+
values: readonly string[];
|
|
127
|
+
set: Readonly<Record<string, string>>;
|
|
128
|
+
images: readonly HelmImageFlags[];
|
|
129
|
+
imageRefs?: readonly HelmImageRefFlags[];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** `helm upgrade` flags: values files, `--set`, then the image flags (which win). */
|
|
133
|
+
export function helmFlags(o: HelmFlagOptions): string[] {
|
|
134
|
+
// Chart-relative values paths (`-f values-beta.yaml`) resolve because helm runs from inside the
|
|
135
|
+
// copied chart dir. Extra flags come before the image flags, so the image overrides win.
|
|
136
|
+
// --set-string: a tag like `2026.10` or `1e5` would otherwise be parsed as a number.
|
|
137
|
+
const flags = [
|
|
138
|
+
...o.values.flatMap((v) => ["-f", v]),
|
|
139
|
+
...Object.entries(o.set).flatMap(([k, v]) => ["--set", `${k}=${v}`]),
|
|
140
|
+
...o.images.flatMap((i) => [
|
|
141
|
+
"--set", `${i.prefix}.repository=${i.repository}`,
|
|
142
|
+
"--set", `${i.prefix}.tag=${i.tag}`,
|
|
143
|
+
"--set", `${i.prefix}.pullPolicy=${i.pullPolicy}`,
|
|
144
|
+
...(i.digest ? ["--set-string", `${i.prefix}.digest=${i.digest}`] : []),
|
|
145
|
+
]),
|
|
146
|
+
...(o.imageRefs ?? []).flatMap((i) => [
|
|
147
|
+
"--set-string", `${i.prefix}.repository=${i.repository}`,
|
|
148
|
+
"--set-string", `${i.prefix}.tag=${i.tag}`,
|
|
149
|
+
"--set-string", `${i.prefix}.digest=${i.digest}`,
|
|
150
|
+
]),
|
|
151
|
+
];
|
|
152
|
+
return flags;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Remote `helm upgrade --install`, run from inside the copied chart directory. */
|
|
156
|
+
export function helmUpgradeCommand(o: HelmFlagOptions & { remoteChart: string; release: string; namespace: string; wait?: HelmWait }): string {
|
|
157
|
+
const flags = helmFlags(o);
|
|
158
|
+
const wait = o.wait ? [o.wait.atomic ? "--atomic" : "--wait", `--timeout=${o.wait.timeoutSeconds}s`] : [];
|
|
159
|
+
return [
|
|
160
|
+
`cd ${shq(o.remoteChart)}`,
|
|
161
|
+
["helm", "upgrade", "--install", o.release, ".", "-n", o.namespace, ...flags, ...wait].map(shq).join(" "),
|
|
162
|
+
`cd / && rm -rf ${shq(o.remoteChart)}`,
|
|
163
|
+
].join(" && ");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** helm's own wait (`--wait`, or `--atomic` to roll back on failure). */
|
|
167
|
+
export interface HelmWait {
|
|
168
|
+
atomic: boolean;
|
|
169
|
+
timeoutSeconds: number;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** A dependency from `Chart.yaml`. */
|
|
173
|
+
export interface ChartDependency {
|
|
174
|
+
name: string;
|
|
175
|
+
version: string;
|
|
176
|
+
repository: string;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** The dependencies `<chartDir>/Chart.yaml` lists, checked to be pullable (an http(s) or oci repository). */
|
|
180
|
+
export function chartDependencies(chartDir: string): ChartDependency[] {
|
|
181
|
+
const chart = Bun.YAML.parse(readFileSync(join(chartDir, "Chart.yaml"), "utf8")) as { dependencies?: Partial<ChartDependency>[] } | null;
|
|
182
|
+
return (chart?.dependencies ?? []).map((d) => {
|
|
183
|
+
if (!d.name || !d.version || !d.repository || !/^(https?|oci):\/\//.test(d.repository))
|
|
184
|
+
throw new Error(`chart dependency ${JSON.stringify(d)}: needs a name, a version and an http(s) or oci repository`);
|
|
185
|
+
return { name: d.name, version: d.version, repository: d.repository };
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** The remote `helm pull` of a dependency into `<remoteChart>/charts`. */
|
|
190
|
+
export function helmPullCommand(dep: ChartDependency, remoteChart: string): string {
|
|
191
|
+
const ref = dep.repository.startsWith("oci://") ? [`${dep.repository.replace(/\/+$/, "")}/${dep.name}`] : [dep.name, "--repo", dep.repository];
|
|
192
|
+
return ["helm", "pull", ...ref, "--version", dep.version, "--destination", `${remoteChart}/charts`].map(shq).join(" ");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** One image to get onto the host before the upgrade. */
|
|
196
|
+
export interface HostImage {
|
|
197
|
+
repository: string;
|
|
198
|
+
tag: string;
|
|
199
|
+
digest?: string;
|
|
200
|
+
/** An image tarball to stream in; without one the image is pulled from its registry. */
|
|
201
|
+
tar?: string;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Over SSH: get each image into the host's k3s containerd, copy the chart (with any generated values
|
|
206
|
+
* files in it), `helm upgrade --install`, then wait for each rollout.
|
|
207
|
+
*/
|
|
208
|
+
export async function deployOverSsh(
|
|
209
|
+
ctx: Ctx,
|
|
210
|
+
o: {
|
|
211
|
+
host: string;
|
|
212
|
+
key: string;
|
|
213
|
+
chartDir: string;
|
|
214
|
+
release: string;
|
|
215
|
+
namespace: string;
|
|
216
|
+
hostImages: readonly HostImage[];
|
|
217
|
+
flags: HelmFlagOptions;
|
|
218
|
+
deployments: readonly string[];
|
|
219
|
+
rolloutTimeoutSeconds: number;
|
|
220
|
+
dependencies?: readonly ChartDependency[];
|
|
221
|
+
wait?: HelmWait;
|
|
222
|
+
},
|
|
223
|
+
): Promise<void> {
|
|
224
|
+
const ssh = await sshSession(ctx, { host: o.host, key: o.key });
|
|
225
|
+
for (const image of o.hostImages) {
|
|
226
|
+
if (image.tar) {
|
|
227
|
+
ctx.log("importing image", { image: `${image.repository}:${image.tag}`, host: o.host });
|
|
228
|
+
// Streamed over ssh's stdin (ctx.exec has none, hence the shell redirect).
|
|
229
|
+
await ctx.sh(`${ssh.shell(K3S_IMAGE_IMPORT)} < ${shq(image.tar)}`);
|
|
230
|
+
} else {
|
|
231
|
+
const ref = image.digest ? `${image.repository}@${image.digest}` : `${image.repository}:${image.tag}`;
|
|
232
|
+
ctx.log("pulling image", { image: ref, host: o.host });
|
|
233
|
+
await ssh.run(`${K3S_IMAGE_PULL} ${shq(ref)}`);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const remoteChart = `/tmp/${o.release}-helm-${o.namespace}`;
|
|
237
|
+
ctx.log("helm upgrade", { release: o.release, namespace: o.namespace, host: o.host });
|
|
238
|
+
await ssh.run(`rm -rf ${shq(remoteChart)}`);
|
|
239
|
+
await ssh.copy(o.chartDir, remoteChart);
|
|
240
|
+
if (o.dependencies?.length) await ssh.run(`mkdir -p ${shq(`${remoteChart}/charts`)}`);
|
|
241
|
+
for (const dep of o.dependencies ?? []) {
|
|
242
|
+
ctx.log("pulling chart dependency", { chart: `${dep.name}@${dep.version}`, repository: dep.repository });
|
|
243
|
+
await ssh.run(helmPullCommand(dep, remoteChart));
|
|
244
|
+
}
|
|
245
|
+
await ssh.run(helmUpgradeCommand({ ...o.flags, remoteChart, release: o.release, namespace: o.namespace, ...(o.wait ? { wait: o.wait } : {}) }));
|
|
246
|
+
for (const d of o.deployments) {
|
|
247
|
+
const target = d.includes("/") ? d : `deploy/${d}`;
|
|
248
|
+
ctx.log("waiting for rollout", { deployment: target });
|
|
249
|
+
await ssh.run(`kubectl -n ${shq(o.namespace)} rollout status ${shq(target)} --timeout=${o.rolloutTimeoutSeconds}s`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The chart directory inside a fetched source tree, or an error naming it. */
|
|
254
|
+
export function chartDirectory(tree: string, chart: string): string {
|
|
255
|
+
const dir = join(tree, chart);
|
|
256
|
+
if (!existsSync(dir) || !statSync(dir).isDirectory()) throw new Error(`chart directory '${chart}' not found in the source tree`);
|
|
257
|
+
return dir;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Deploys images with the Helm chart at `<source>/<chart>` on a k3s host over SSH: get each image
|
|
262
|
+
* into the host's k3s containerd (stream a tarball in, or pull from the registry), copy the chart,
|
|
263
|
+
* `helm upgrade --install`, wait for each rollout, then (with `healthUrl`) poll the health endpoint.
|
|
264
|
+
*/
|
|
265
|
+
export function helmDeployOverSsh<
|
|
266
|
+
const N extends string,
|
|
267
|
+
const K extends string,
|
|
268
|
+
const Im extends Record<string, string> = DefaultImages,
|
|
269
|
+
const Rf extends Record<string, string> = {},
|
|
270
|
+
>(def: HelmDeploySshTargetDef<N, K, Im, Rf>): Implementation<Step<N, HelmTargetDeployInput<Im, Rf>, typeof HelmRelease, K>>;
|
|
271
|
+
export function helmDeployOverSsh<
|
|
272
|
+
const N extends string,
|
|
273
|
+
const K extends string,
|
|
274
|
+
const Im extends Record<string, string> = DefaultImages,
|
|
275
|
+
const R extends boolean = false,
|
|
276
|
+
const Rf extends Record<string, string> = {},
|
|
277
|
+
>(def: HelmDeployOverSshDef<N, K, Im, R, Rf>): Implementation<Step<N, HelmDeployOverSshInput<Im, R, Rf>, typeof HelmRelease, K>>;
|
|
278
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
279
|
+
export function helmDeployOverSsh(def: any): any {
|
|
280
|
+
return "target" in def ? helmDeployOverSshTarget(def) : helmDeployOverSshLiteral(def);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
284
|
+
function helmDeployOverSshLiteral(def: HelmDeployOverSshDef<string, string, any, boolean, any>): Implementation {
|
|
285
|
+
const images: Record<string, string> = def.images ?? { image: "image" };
|
|
286
|
+
const imageEntries = Object.entries(images);
|
|
287
|
+
const fromRegistry = def.fromRegistry === true;
|
|
288
|
+
const rolloutTimeout = def.rolloutTimeoutSeconds ?? DEFAULT_ROLLOUT_TIMEOUT_SECONDS;
|
|
289
|
+
const wait: HelmWait | undefined = def.atomic || def.helmWait ? { atomic: def.atomic === true, timeoutSeconds: rolloutTimeout } : undefined;
|
|
290
|
+
if ("source" in images) throw new Error(`helmDeployOverSsh '${def.name}': 'source' can't be an image input`);
|
|
291
|
+
if (def.deployments?.length === 0) throw new Error(`helmDeployOverSsh '${def.name}': deployments must not be empty`);
|
|
292
|
+
const imageRefs: Record<string, string> = def.imageRefs ?? {};
|
|
293
|
+
const refEntries = Object.entries(imageRefs);
|
|
294
|
+
if ("source" in imageRefs) throw new Error(`helmDeployOverSsh '${def.name}': 'source' can't be an image input`);
|
|
295
|
+
for (const [key] of refEntries) if (key in images) throw new Error(`helmDeployOverSsh '${def.name}': '${key}' is both in images and imageRefs`);
|
|
296
|
+
const imageModel = fromRegistry ? PushedImage : BuiltImage;
|
|
297
|
+
const input = {
|
|
298
|
+
source: Source,
|
|
299
|
+
...Object.fromEntries(imageEntries.map(([key]) => [key, imageModel])),
|
|
300
|
+
...Object.fromEntries(refEntries.map(([key]) => [key, PushedImage])),
|
|
301
|
+
};
|
|
302
|
+
const isDefaultImages = imageEntries.length === 1 && images.image === "image";
|
|
303
|
+
const configurable = { host: def.host, release: def.release, namespace: def.namespace, values: def.values ?? [], set: def.set ?? {}, healthUrl: def.healthUrl };
|
|
304
|
+
|
|
305
|
+
const contract = step({
|
|
306
|
+
name: def.name,
|
|
307
|
+
description: def.description,
|
|
308
|
+
kind: "helmDeployOverSsh",
|
|
309
|
+
image: HELM_DEPLOY_IMAGE,
|
|
310
|
+
input,
|
|
311
|
+
output: HelmRelease,
|
|
312
|
+
secrets: [def.sshKey],
|
|
313
|
+
timeoutSeconds: def.timeoutSeconds,
|
|
314
|
+
settings: {
|
|
315
|
+
host: def.host,
|
|
316
|
+
sshKey: def.sshKey.name,
|
|
317
|
+
release: def.release,
|
|
318
|
+
namespace: def.namespace,
|
|
319
|
+
chart: def.chart,
|
|
320
|
+
values: configurable.values,
|
|
321
|
+
set: configurable.set,
|
|
322
|
+
...(isDefaultImages ? {} : { images: { ...images } }),
|
|
323
|
+
...(fromRegistry ? { fromRegistry: true } : {}),
|
|
324
|
+
...(refEntries.length > 0 ? { imageRefs: { ...imageRefs } } : {}),
|
|
325
|
+
...(def.deployments ? { deployments: [...def.deployments] } : {}),
|
|
326
|
+
...(def.rolloutTimeoutSeconds !== undefined ? { rolloutTimeoutSeconds: def.rolloutTimeoutSeconds } : {}),
|
|
327
|
+
...(def.dependencies ? { dependencies: true } : {}),
|
|
328
|
+
...(def.atomic ? { atomic: true } : def.helmWait ? { helmWait: true } : {}),
|
|
329
|
+
...(def.healthUrl !== undefined ? { healthUrl: def.healthUrl } : {}),
|
|
330
|
+
},
|
|
331
|
+
});
|
|
332
|
+
return implement(contract, async (stepInput, ctx: Ctx) => {
|
|
333
|
+
const input = stepInput as unknown as Record<string, { repository: string; tag: string; digest: string; tar?: FileRef }> & { source: { tree: FileRef } };
|
|
334
|
+
const d = resolveConfigRefs(configurable, ctx.config ?? {}) as {
|
|
335
|
+
host: string; release: string; namespace: string; values: string[]; set: Record<string, string>;
|
|
336
|
+
healthUrl?: string | ({ url: string } & Pick<HttpHealthCheckOptions, "attempts" | "delaySeconds">);
|
|
337
|
+
};
|
|
338
|
+
const chartDir = chartDirectory(await ctx.files.fetch(input.source.tree), def.chart);
|
|
339
|
+
const hostImages: HostImage[] = [];
|
|
340
|
+
const flags: HelmImageFlags[] = [];
|
|
341
|
+
for (const [key, prefix] of imageEntries) {
|
|
342
|
+
const image = input[key]!;
|
|
343
|
+
hostImages.push({ repository: image.repository, tag: image.tag, ...(fromRegistry ? {} : { tar: await ctx.files.fetch(image.tar!) }) });
|
|
344
|
+
flags.push({ prefix, repository: image.repository, tag: image.tag, pullPolicy: fromRegistry ? "IfNotPresent" : "Never" });
|
|
345
|
+
}
|
|
346
|
+
const refs: HelmImageRefFlags[] = refEntries.map(([key, prefix]) => ({ prefix, repository: input[key]!.repository, tag: input[key]!.tag, digest: input[key]!.digest }));
|
|
347
|
+
await deployOverSsh(ctx, {
|
|
348
|
+
host: d.host, key: ctx.secrets[def.sshKey.name]!, chartDir, release: d.release, namespace: d.namespace, hostImages,
|
|
349
|
+
flags: { values: d.values, set: d.set, images: flags, imageRefs: refs },
|
|
350
|
+
deployments: def.deployments ?? (imageEntries.length && !wait ? [d.release] : []), rolloutTimeoutSeconds: rolloutTimeout,
|
|
351
|
+
...(def.dependencies ? { dependencies: chartDependencies(chartDir) } : {}),
|
|
352
|
+
...(wait ? { wait } : {}),
|
|
353
|
+
});
|
|
354
|
+
const health = typeof d.healthUrl === "string" ? { url: d.healthUrl } : d.healthUrl;
|
|
355
|
+
if (health) await httpHealthCheck(ctx, health.url, { attempts: health.attempts, delaySeconds: health.delaySeconds });
|
|
356
|
+
return { release: d.release, namespace: d.namespace, ...(flags[0] ? { image: `${flags[0].repository}:${flags[0].tag}` } : {}) };
|
|
357
|
+
}) as never;
|
|
358
|
+
}
|