Repository navigation
Expand file tree
/
Copy pathdeploy-vercel.mjs
More file actions
137 lines (125 loc) · 6.76 KB
/
Copy pathdeploy-vercel.mjs
File metadata and controls
137 lines (125 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Deployment credentials stay in environment secrets, never in source or logs.
// PR jobs never execute this script with deployment credentials.
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const root = path.resolve(import.meta.dirname, "..");
const localStaging = process.argv.includes("--local-staging");
const branch = localStaging ? "dev" : process.env.GITHUB_REF_NAME;
const temporary = [];
const env = { ...process.env, DEBUG: "" };
const cli = process.env.VERCEL_CLI;
const command = cli?.endsWith(".js") ? process.execPath : cli || "vercel";
const prefix = cli?.endsWith(".js") ? [cli] : [];
function requireValue(key) {
const value = process.env[key];
if (!value?.trim()) throw new Error(`Missing deployment setting: ${key}`);
return value;
}
function origin(key) {
const value = requireValue(key);
const url = new URL(value);
if (url.protocol !== "https:" || url.username || url.password || url.search || url.hash || url.pathname !== "/") {
throw new Error(`Expected an HTTPS origin for ${key}`);
}
return url.origin;
}
function workspace() {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "procoderji-deploy-"));
temporary.push(directory);
return directory;
}
function vercel(args, cwd, project, token) {
const result = spawnSync(command, [
...prefix, ...args,
...(token ? ["--token", token] : []),
], {
cwd, env: { ...env, VERCEL_ORG_ID: requireValue("VERCEL_ORG_ID"), VERCEL_PROJECT_ID: project },
encoding: "utf8", windowsHide: true, timeout: 12 * 60 * 1000,
maxBuffer: 8 * 1024 * 1024,
});
let data;
try { data = JSON.parse(result.stdout); } catch { /* Never dump raw provider logs. */ }
if (result.status !== 0 || !data) throw new Error("Vercel deployment failed. Inspect its build in the project dashboard.");
return data;
}
function authentication(tokenKey) {
if (localStaging && !process.env[tokenKey]) return undefined;
// Native CLI 65 requires its explicit token option; no shell or command logging is used.
return requireValue(tokenKey);
}
function deploySource() {
const result = spawnSync("git", ["ls-files", "-z"], { cwd: root, encoding: "utf8", windowsHide: true });
if (result.status !== 0) throw new Error("Cannot enumerate tracked deployment source.");
const directory = workspace();
for (const file of result.stdout.split("\0").filter(Boolean)) {
const source = path.resolve(root, file);
if (!source.startsWith(root + path.sep) || fs.lstatSync(source).isSymbolicLink()) {
throw new Error("Deployment source must contain ordinary repository files.");
}
const target = path.join(directory, file);
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.copyFileSync(source, target);
}
return directory;
}
async function check(url, inspect) {
for (let attempt = 0; attempt < 3; attempt++) {
try {
const response = await fetch(url, { redirect: "manual", signal: AbortSignal.timeout(30_000) });
if (await inspect(response)) return;
} catch { /* Cold starts can need one retry. */ }
if (attempt < 2) await new Promise(resolve => setTimeout(resolve, 2000));
}
throw new Error("A deployed endpoint failed its release check.");
}
async function main() {
if (localStaging && process.env.GITHUB_ACTIONS === "true") throw new Error("Local staging mode is unavailable in Actions.");
if (!["dev", "main"].includes(branch)) throw new Error("Only dev and main have deployment environments.");
if (!localStaging && (process.env.GITHUB_EVENT_NAME !== "push" || process.env.GITHUB_REPOSITORY !== "procoderji-com/procoderji")) {
throw new Error("Only checked pushes to the canonical repository can deploy.");
}
const publicOrigin = origin("PUBLIC_ORIGIN");
const backendOrigin = origin("BACKEND_ORIGIN");
if (branch === "main" && publicOrigin !== "https://procoderji.com") throw new Error("Unexpected production origin.");
if (branch === "dev" && publicOrigin === "https://procoderji.com") throw new Error("Staging must have its own origin.");
const sha = spawnSync("git", ["rev-parse", "HEAD"], { cwd: root, encoding: "utf8", windowsHide: true }).stdout.trim();
if (!/^[a-f0-9]{40}$/.test(sha) || (!localStaging && sha !== process.env.GITHUB_SHA)) throw new Error("The checkout must match the checked commit.");
const backendProject = requireValue("VERCEL_BACKEND_PROJECT_ID");
const frontendProject = requireValue("VERCEL_FRONTEND_PROJECT_ID");
if (backendProject === frontendProject) throw new Error("Backend and frontend projects must be separate.");
const backendSource = deploySource();
const frontendSource = deploySource();
fs.copyFileSync(path.join(backendSource, "vercel.backend.json"), path.join(backendSource, "vercel.json"));
const deployArgs = ["deploy", "--prod", "--yes", "--json", "--meta", `sourceCommit=${sha}`, "--meta", `sourceBranch=${branch}`];
const backend = vercel([...deployArgs, "--project", backendProject], backendSource, backendProject, authentication("VERCEL_BACKEND_TOKEN"));
if (backend.status === "error" || backend.deployment?.readyState !== "READY") throw new Error("Backend deployment did not become ready.");
await check(`${backendOrigin}/api/platform/health`, async r => r.ok && (await r.json()).status === "ok");
await check(`${backendOrigin}/api/platform/auth/session`, async r => {
if (!r.ok) return false;
const session = await r.json();
return !!session.csrfToken && (branch !== "main" || session.configured === true);
});
console.log("Backend is ready; database-backed session check passed.");
const frontend = vercel([...deployArgs, "--project", frontendProject], frontendSource, frontendProject, authentication("VERCEL_FRONTEND_TOKEN"));
if (frontend.status === "error" || frontend.deployment?.readyState !== "READY") throw new Error("Frontend deployment did not become ready.");
await check(publicOrigin, async r => r.ok && (branch === "main" || r.headers.get("x-robots-tag")?.includes("noindex")));
console.log(`Frontend is ready: ${publicOrigin}`);
const summary = `### ${branch === "main" ? "Production" : "Staging"} deployment\n\nCommit: \`${sha}\`\n\n- Frontend: ${publicOrigin}\n- Backend: ${backendOrigin}\n- Health and database-backed session checks passed.\n`;
if (process.env.GITHUB_STEP_SUMMARY) fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, summary);
}
try {
await main();
} catch (error) {
console.error(error instanceof Error ? error.message : "Deployment failed.");
process.exitCode = 1;
} finally {
const parent = fs.realpathSync(os.tmpdir());
for (const directory of temporary) {
const resolved = path.resolve(directory);
if (path.dirname(resolved) === parent && path.basename(resolved).startsWith("procoderji-deploy-")) {
fs.rmSync(resolved, { recursive: true, force: true });
}
}
}