|
2 | 2 | // version to 100% of traffic. The version must come from a provenance |
3 | 3 | // manifest emitted by upload-version.mjs in a successful publish run |
4 | 4 | // that verify-upload-run.mjs has already vouched for, and the live |
5 | | -// deployment must still sit at the manifest's baseline — any drift |
6 | | -// aborts the promotion instead of guessing. |
| 5 | +// deployment must either still sit at the manifest's baseline or |
| 6 | +// already serve exactly this version at 100% (an idempotent re-run); |
| 7 | +// any other drift aborts the promotion instead of guessing. |
7 | 8 | // Usage: promote.mjs --manifest <provenance.json> --promote |
8 | 9 | import { execFileSync } from "node:child_process"; |
9 | 10 | import { existsSync, readFileSync } from "node:fs"; |
@@ -13,77 +14,103 @@ import { fileURLToPath } from "node:url"; |
13 | 14 | const here = dirname(fileURLToPath(import.meta.url)); |
14 | 15 | const cfBin = join(here, "node_modules", ".bin", "cf"); |
15 | 16 |
|
16 | | -const args = process.argv.slice(2); |
17 | | -const manifestFlag = args.indexOf("--manifest"); |
18 | | -const manifestPath = manifestFlag >= 0 ? args[manifestFlag + 1] : undefined; |
19 | | -const rest = args.filter((_, i) => i !== manifestFlag && i !== manifestFlag + 1); |
20 | | -if (!manifestPath || rest.length !== 1 || rest[0] !== "--promote") { |
21 | | - console.error("usage: promote.mjs --manifest <provenance.json> --promote"); |
22 | | - process.exit(2); |
| 17 | +// Classify the live deployment against the manifest without side |
| 18 | +// effects: "already-promoted" when the manifest version is already the |
| 19 | +// sole version at 100% of traffic, "at-baseline" when promotion may |
| 20 | +// proceed, "drifted" for anything else. Pure, for unit tests. |
| 21 | +export function deploymentStatus(current, manifest) { |
| 22 | + const versions = current?.versions ?? []; |
| 23 | + if ( |
| 24 | + versions.length === 1 && |
| 25 | + versions[0]?.version_id === manifest.versionId && |
| 26 | + versions[0]?.percentage === 100 |
| 27 | + ) { |
| 28 | + return "already-promoted"; |
| 29 | + } |
| 30 | + if (versions[0]?.version_id === manifest.baselineVersionId) { |
| 31 | + return "at-baseline"; |
| 32 | + } |
| 33 | + return "drifted"; |
23 | 34 | } |
24 | 35 |
|
25 | | -const config = JSON.parse(readFileSync(join(here, "config.json"), "utf8")); |
26 | | -const manifest = JSON.parse(readFileSync(manifestPath, "utf8")); |
27 | | -const verified = JSON.parse(readFileSync(requireEnv("DC_WORKER_VERIFIED_RUN_FILE"), "utf8")); |
28 | | -const token = requireEnv("DC_WORKER_CF_TOKEN"); |
29 | | -requireEnv("CLOUDFLARE_ACCOUNT_ID"); |
| 36 | +function main() { |
| 37 | + const args = process.argv.slice(2); |
| 38 | + const manifestFlag = args.indexOf("--manifest"); |
| 39 | + const manifestPath = manifestFlag >= 0 ? args[manifestFlag + 1] : undefined; |
| 40 | + const rest = args.filter((_, i) => i !== manifestFlag && i !== manifestFlag + 1); |
| 41 | + if (!manifestPath || rest.length !== 1 || rest[0] !== "--promote") { |
| 42 | + console.error("usage: promote.mjs --manifest <provenance.json> --promote"); |
| 43 | + process.exit(2); |
| 44 | + } |
30 | 45 |
|
31 | | -if (!existsSync(cfBin)) { |
32 | | - throw new Error("pinned cf CLI missing; install dependencies before supplying credentials"); |
33 | | -} |
34 | | -if (manifest.worker !== config.worker) { |
35 | | - throw new Error(`manifest worker ${manifest.worker} does not match config worker ${config.worker}`); |
36 | | -} |
37 | | -if (manifest.sha !== verified.sha || String(manifest.runId) !== String(verified.id) || String(manifest.runAttempt) !== String(verified.attempt)) { |
38 | | - throw new Error("manifest does not match the verified upload run; refusing to promote"); |
39 | | -} |
40 | | -if (manifest.repository?.toLowerCase() !== process.env.GITHUB_REPOSITORY?.toLowerCase()) { |
41 | | - throw new Error("manifest repository does not match this repository; refusing to promote"); |
42 | | -} |
43 | | -if (typeof manifest.versionId !== "string" || !manifest.versionId) { |
44 | | - throw new Error("manifest has no versionId; refusing to promote"); |
45 | | -} |
| 46 | + const config = JSON.parse(readFileSync(join(here, "config.json"), "utf8")); |
| 47 | + const manifest = JSON.parse(readFileSync(manifestPath, "utf8")); |
| 48 | + const verified = JSON.parse(readFileSync(requireEnv("DC_WORKER_VERIFIED_RUN_FILE"), "utf8")); |
| 49 | + const token = requireEnv("DC_WORKER_CF_TOKEN"); |
| 50 | + requireEnv("CLOUDFLARE_ACCOUNT_ID"); |
46 | 51 |
|
47 | | -function cfJson(argv) { |
48 | | - const output = execFileSync(cfBin, argv, { |
49 | | - cwd: here, |
50 | | - encoding: "utf8", |
51 | | - env: { ...process.env, CLOUDFLARE_API_TOKEN: token }, |
52 | | - }); |
53 | | - let parsed; |
54 | | - try { |
55 | | - parsed = JSON.parse(output); |
56 | | - } catch { |
57 | | - throw new Error("cf returned non-JSON output; refusing to continue"); |
| 52 | + if (!existsSync(cfBin)) { |
| 53 | + throw new Error("pinned cf CLI missing; install dependencies before supplying credentials"); |
| 54 | + } |
| 55 | + if (manifest.worker !== config.worker) { |
| 56 | + throw new Error(`manifest worker ${manifest.worker} does not match config worker ${config.worker}`); |
| 57 | + } |
| 58 | + if (manifest.sha !== verified.sha || String(manifest.runId) !== String(verified.id) || String(manifest.runAttempt) !== String(verified.attempt)) { |
| 59 | + throw new Error("manifest does not match the verified upload run; refusing to promote"); |
| 60 | + } |
| 61 | + if (manifest.repository?.toLowerCase() !== process.env.GITHUB_REPOSITORY?.toLowerCase()) { |
| 62 | + throw new Error("manifest repository does not match this repository; refusing to promote"); |
| 63 | + } |
| 64 | + if (typeof manifest.versionId !== "string" || !manifest.versionId) { |
| 65 | + throw new Error("manifest has no versionId; refusing to promote"); |
58 | 66 | } |
59 | | - return parsed?.result ?? parsed; |
60 | | -} |
61 | 67 |
|
62 | | -const version = cfJson(["workers", "versions", "get", manifest.versionId, "--worker-id", config.worker]); |
63 | | -if ((version.id ?? version.version_id) !== manifest.versionId) { |
64 | | - throw new Error("cf did not return the manifest version; refusing to promote"); |
65 | | -} |
66 | | -if (version.annotations?.["workers/tag"] && version.annotations["workers/tag"] !== manifest.tag) { |
67 | | - throw new Error("the version's tag does not match the manifest tag; refusing to promote"); |
68 | | -} |
| 68 | + function cfJson(argv) { |
| 69 | + const output = execFileSync(cfBin, argv, { |
| 70 | + cwd: here, |
| 71 | + encoding: "utf8", |
| 72 | + env: { ...process.env, CLOUDFLARE_API_TOKEN: token }, |
| 73 | + }); |
| 74 | + let parsed; |
| 75 | + try { |
| 76 | + parsed = JSON.parse(output); |
| 77 | + } catch { |
| 78 | + throw new Error("cf returned non-JSON output; refusing to continue"); |
| 79 | + } |
| 80 | + return parsed?.result ?? parsed; |
| 81 | + } |
69 | 82 |
|
70 | | -const deployments = cfJson(["workers", "deployments", "list", "--worker", config.worker, "--per-page", "1"]); |
71 | | -const current = Array.isArray(deployments) ? deployments[0] : deployments?.deployments?.[0]; |
72 | | -const currentVersionId = current?.versions?.[0]?.version_id ?? null; |
73 | | -if (currentVersionId !== manifest.baselineVersionId) { |
74 | | - throw new Error( |
75 | | - `active version ${currentVersionId} is not the manifest baseline ${manifest.baselineVersionId}; refusing to promote over drift`, |
76 | | - ); |
77 | | -} |
| 83 | + const version = cfJson(["workers", "versions", "get", manifest.versionId, "--worker-id", config.worker]); |
| 84 | + if ((version.id ?? version.version_id) !== manifest.versionId) { |
| 85 | + throw new Error("cf did not return the manifest version; refusing to promote"); |
| 86 | + } |
| 87 | + if (version.annotations?.["workers/tag"] && version.annotations["workers/tag"] !== manifest.tag) { |
| 88 | + throw new Error("the version's tag does not match the manifest tag; refusing to promote"); |
| 89 | + } |
78 | 90 |
|
79 | | -const created = cfJson([ |
80 | | - "workers", "deployments", "create", |
81 | | - "--worker", config.worker, |
82 | | - "--strategy", "percentage", |
83 | | - "--versions", JSON.stringify([{ version_id: manifest.versionId, percentage: 100 }]), |
84 | | -]); |
85 | | -const deploymentId = created?.id ?? created?.deployment_id; |
86 | | -console.log(`promoted ${config.worker} version ${manifest.versionId} (tag ${manifest.tag}) to 100%${deploymentId ? `; deployment ${deploymentId}` : ""}`); |
| 91 | + const deployments = cfJson(["workers", "deployments", "list", "--worker", config.worker, "--per-page", "1"]); |
| 92 | + const current = Array.isArray(deployments) ? deployments[0] : deployments?.deployments?.[0]; |
| 93 | + const status = deploymentStatus(current, manifest); |
| 94 | + if (status === "already-promoted") { |
| 95 | + console.log(`verified ${config.worker} version ${manifest.versionId} (tag ${manifest.tag}) is already the sole 100% deployment; nothing to promote`); |
| 96 | + return; |
| 97 | + } |
| 98 | + if (status === "drifted") { |
| 99 | + const active = current?.versions?.[0]?.version_id ?? null; |
| 100 | + throw new Error( |
| 101 | + `active version ${active} is neither the manifest baseline ${manifest.baselineVersionId} nor the verified upload ${manifest.versionId}; refusing to promote over drift`, |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + const created = cfJson([ |
| 106 | + "workers", "deployments", "create", |
| 107 | + "--worker", config.worker, |
| 108 | + "--strategy", "percentage", |
| 109 | + "--versions", JSON.stringify([{ version_id: manifest.versionId, percentage: 100 }]), |
| 110 | + ]); |
| 111 | + const deploymentId = created?.id ?? created?.deployment_id; |
| 112 | + console.log(`promoted ${config.worker} version ${manifest.versionId} (tag ${manifest.tag}) to 100%${deploymentId ? `; deployment ${deploymentId}` : ""}`); |
| 113 | +} |
87 | 114 |
|
88 | 115 | function requireEnv(name) { |
89 | 116 | const value = process.env[name]; |
@@ -92,3 +119,7 @@ function requireEnv(name) { |
92 | 119 | } |
93 | 120 | return value; |
94 | 121 | } |
| 122 | + |
| 123 | +if (process.argv[1] === fileURLToPath(import.meta.url)) { |
| 124 | + main(); |
| 125 | +} |