Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkgs/pkarr-worker/deploy-args.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

export const USAGE = [
"usage: deploy.mjs [--dry-run]",
" deploy.mjs --deploy-version <id>",
" deploy.mjs --deploy-version <id> [--dry-run]",
].join("\n");

/**
Expand Down
9 changes: 9 additions & 0 deletions pkgs/pkarr-worker/deploy-version.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export declare function deployExistingVersion(input: {
dryRun: boolean;
deployVersion: string;
worker: string;
workerUrl: string;
createDeployment: (versionId: string) => void;
reportLive: (versionId: string) => Promise<void>;
log?: (message: string) => void;
}): Promise<{ deployed: boolean; dryRun: boolean; versionId: string }>;
43 changes: 43 additions & 0 deletions pkgs/pkarr-worker/deploy-version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// The --deploy-version branch of deploy.mjs, kept in its own module so
// the flag x path matrix can be tested without importing the deploy
// script (which runs cf/bws at import time). The rule is contract
// rule 4 of #3352: a parsed safety flag is honored on every path. The
// HEAD paths hand --dry-run to cf, but this branch creates the
// deployment itself, so dry-run must stop it BEFORE createDeployment:
// a rollback preview must never create a real 100% deployment (#3252).

/**
* Deploy (or preview deploying) an existing version at 100%.
*
* @param {object} input
* @param {boolean} input.dryRun preview only: report and create nothing
* @param {string} input.deployVersion existing version id to deploy
* @param {string} input.worker worker name, for the report lines
* @param {string} input.workerUrl worker URL, for the live line
* @param {(versionId: string) => void} input.createDeployment creates
* the 100% deployment (the mutation dry-run must never reach)
* @param {(versionId: string) => Promise<void>} input.reportLive
* verifies and reports the live deployment
* @param {(message: string) => void} [input.log] report sink
* @returns {Promise<{ deployed: boolean, dryRun: boolean, versionId: string }>}
*/
export async function deployExistingVersion({
dryRun,
deployVersion,
worker,
workerUrl,
createDeployment,
reportLive,
log = console.log,
}) {
if (dryRun) {
log(
`dry run: would create a deployment of worker ${worker} at version ${deployVersion} (100%); no deployment created`,
);
return { deployed: false, dryRun: true, versionId: deployVersion };
}
createDeployment(deployVersion);
await reportLive(deployVersion);
log(`live: ${workerUrl} at version ${deployVersion}`);
return { deployed: true, dryRun: false, versionId: deployVersion };
}
17 changes: 14 additions & 3 deletions pkgs/pkarr-worker/deploy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
// deploy.mjs [--dry-run] upload + deploy HEAD
// deploy.mjs --deploy-version <id> deploy (or roll back to) an
// existing version at 100%
// deploy.mjs --deploy-version <id> --dry-run
// preview that deployment;
// creates nothing (#3252)
//
// Needs CLOUDFLARE_ACCOUNT_ID and BWS_ACCESS_TOKEN; deploy.sh sets them.
// The API token comes from Bitwarden by name (default
Expand All @@ -32,6 +35,7 @@ import { fileURLToPath } from "node:url";

import { parseArgs } from "./deploy-args.mjs";
import { probeWorkerExists } from "./deploy-probe.mjs";
import { deployExistingVersion } from "./deploy-version.mjs";
import { verifyDeployment } from "./deploy-verification.mjs";

const here = dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -74,9 +78,16 @@ if (!existsSync(join(here, "node_modules"))) {
}

if (deployVersion !== null) {
createDeployment(deployVersion);
await reportLive(deployVersion);
console.log(`live: ${WORKER_URL} at version ${deployVersion}`);
// The branch decision lives in deploy-version.mjs so the flag x path
// matrix is tested: --dry-run previews and creates nothing (#3252).
await deployExistingVersion({
dryRun,
deployVersion,
worker: WORKER,
workerUrl: WORKER_URL,
createDeployment,
reportLive,
});
process.exit(0);
}

Expand Down
97 changes: 97 additions & 0 deletions pkgs/pkarr-worker/test/deploy-version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, expect, it, vi } from "vitest";

import { parseArgs } from "../deploy-args.mjs";
import { deployExistingVersion } from "../deploy-version.mjs";

// The flag x path matrix for the --deploy-version branch (#3252):
// --dry-run is a rollback PREVIEW and must never reach createDeployment;
// without it the branch deploys exactly as it did before the flag was
// honored (create, verify/report, live line, in that order).
const harness = () => {
const calls: string[] = [];
const logs: string[] = [];
return {
calls,
logs,
createDeployment: vi.fn((versionId: string) => {
calls.push(`create:${versionId}`);
}),
reportLive: vi.fn(async (versionId: string) => {
calls.push(`report:${versionId}`);
}),
log: (message: string) => {
logs.push(message);
},
};
};

const base = { worker: "pkarr", workerUrl: "https://pkarr.example" };

describe("deploy-version branch (#3252)", () => {
it("--deploy-version with --dry-run creates no deployment and reports the preview", async () => {
const h = harness();
const result = await deployExistingVersion({
...base,
dryRun: true,
deployVersion: "v-rollback-123",
createDeployment: h.createDeployment,
reportLive: h.reportLive,
log: h.log,
});
expect(h.createDeployment).not.toHaveBeenCalled();
expect(h.reportLive).not.toHaveBeenCalled();
expect(result).toEqual({ deployed: false, dryRun: true, versionId: "v-rollback-123" });
expect(h.logs.join("\n")).toMatch(/dry run/);
expect(h.logs.join("\n")).toContain("pkarr");
expect(h.logs.join("\n")).toContain("v-rollback-123");
});

it("--deploy-version without --dry-run deploys as before: create, report, live line", async () => {
const h = harness();
const result = await deployExistingVersion({
...base,
dryRun: false,
deployVersion: "v-rollback-123",
createDeployment: h.createDeployment,
reportLive: h.reportLive,
log: h.log,
});
expect(h.createDeployment).toHaveBeenCalledTimes(1);
expect(h.createDeployment).toHaveBeenCalledWith("v-rollback-123");
expect(h.reportLive).toHaveBeenCalledTimes(1);
expect(h.reportLive).toHaveBeenCalledWith("v-rollback-123");
expect(h.calls).toEqual(["create:v-rollback-123", "report:v-rollback-123"]);
expect(result).toEqual({ deployed: true, dryRun: false, versionId: "v-rollback-123" });
expect(h.logs.join("\n")).toContain("live: https://pkarr.example at version v-rollback-123");
});

it("the parsed flag matrix drives the branch: preview only when --dry-run is parsed", async () => {
for (const argv of [
["--deploy-version", "v-rollback-123", "--dry-run"],
["--dry-run", "--deploy-version=v-rollback-123"],
]) {
const h = harness();
const { dryRun, deployVersion } = parseArgs(argv);
await deployExistingVersion({
...base,
dryRun,
deployVersion: deployVersion ?? "",
createDeployment: h.createDeployment,
reportLive: h.reportLive,
log: h.log,
});
expect(h.createDeployment).not.toHaveBeenCalled();
}
const h = harness();
const parsed = parseArgs(["--deploy-version", "v-rollback-123"]);
await deployExistingVersion({
...base,
dryRun: parsed.dryRun,
deployVersion: parsed.deployVersion ?? "",
createDeployment: h.createDeployment,
reportLive: h.reportLive,
log: h.log,
});
expect(h.createDeployment).toHaveBeenCalledTimes(1);
});
});
Loading