chore(deploy): add build number release flow
This commit is contained in:
1 parent
6accb4b166
commit
6e0afe84cd
3 files changed
+159
-26
No files matched your search
@@ -3,43 +3,159 @@ import { existsSync, readFileSync } from "node:fs";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const scriptPath = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(scriptPath);
|
||||
const repoRoot = resolve(__dirname, "..");
|
||||
const webDir = resolve(repoRoot, "apps/web");
|
||||
|
||||
const target = process.argv[2];
|
||||
if (target !== "staging" && target !== "prod") {
|
||||
if (process.argv[1] === scriptPath) {
|
||||
main(process.argv.slice(2));
|
||||
}
|
||||
|
||||
export function main(args = process.argv.slice(2), env = process.env) {
|
||||
const target = args[0];
|
||||
if (target !== "staging" && target !== "prod") {
|
||||
console.error(
|
||||
"Usage: node scripts/deploy-cloudflare-pages.mjs <staging|prod>",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const viteMode = target === "prod" ? "production" : "staging";
|
||||
const projectName =
|
||||
env.CF_PAGES_PROJECT_NAME ??
|
||||
env.CLOUDFLARE_PAGES_PROJECT_NAME ??
|
||||
"lolympus";
|
||||
const stagingBranch =
|
||||
target === "staging" ? resolveStagingBranch(env) : undefined;
|
||||
|
||||
validateEnv(viteMode, env);
|
||||
run("pnpm", ["--filter", "web", "run", `build:${target}`], repoRoot);
|
||||
|
||||
const deployArgs = [
|
||||
"exec",
|
||||
"wrangler",
|
||||
"pages",
|
||||
"deploy",
|
||||
"dist",
|
||||
"--project-name",
|
||||
projectName,
|
||||
];
|
||||
|
||||
if (target === "staging") {
|
||||
deployArgs.push("--branch", stagingBranch);
|
||||
console.error(
|
||||
`Deploying staging to Cloudflare Pages branch ${stagingBranch}`,
|
||||
);
|
||||
}
|
||||
|
||||
run("pnpm", deployArgs, webDir);
|
||||
}
|
||||
|
||||
export function resolveStagingBranch(env = process.env) {
|
||||
const configuredBranch = env.CF_PAGES_STAGING_BRANCH?.trim();
|
||||
if (configuredBranch) return configuredBranch;
|
||||
|
||||
refreshRemoteReleaseBranches();
|
||||
|
||||
const latestReleaseBranch = selectLatestReleaseBranch(
|
||||
listReleaseBranchRefs(),
|
||||
);
|
||||
if (latestReleaseBranch) return latestReleaseBranch;
|
||||
|
||||
console.error(
|
||||
"Usage: node scripts/deploy-cloudflare-pages.mjs <staging|prod>",
|
||||
[
|
||||
"No release/* branch found for staging deploy.",
|
||||
"Create a release branch, for example: git push origin main:release/66",
|
||||
"Or set CF_PAGES_STAGING_BRANCH explicitly.",
|
||||
].join("\n"),
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const viteMode = target === "prod" ? "production" : "staging";
|
||||
const projectName =
|
||||
process.env.CF_PAGES_PROJECT_NAME ??
|
||||
process.env.CLOUDFLARE_PAGES_PROJECT_NAME ??
|
||||
"lolympus";
|
||||
const stagingBranch = process.env.CF_PAGES_STAGING_BRANCH ?? "staging";
|
||||
export function selectLatestReleaseBranch(refRows) {
|
||||
const latestByBranch = new Map();
|
||||
|
||||
validateEnv(viteMode);
|
||||
run("pnpm", ["--filter", "web", "run", `build:${target}`], repoRoot);
|
||||
for (const row of refRows) {
|
||||
const [timestampText, refName] = row.split("\t");
|
||||
const branch = normalizeReleaseBranch(refName);
|
||||
const timestamp = Number(timestampText);
|
||||
if (!branch || !Number.isFinite(timestamp)) continue;
|
||||
|
||||
const deployArgs = [
|
||||
"exec",
|
||||
"wrangler",
|
||||
"pages",
|
||||
"deploy",
|
||||
"dist",
|
||||
"--project-name",
|
||||
projectName,
|
||||
];
|
||||
const current = latestByBranch.get(branch);
|
||||
if (!current || timestamp > current.timestamp) {
|
||||
latestByBranch.set(branch, { branch, timestamp });
|
||||
}
|
||||
}
|
||||
|
||||
if (target === "staging") {
|
||||
deployArgs.push("--branch", stagingBranch);
|
||||
const [latest] = [...latestByBranch.values()].sort((left, right) => {
|
||||
if (left.timestamp !== right.timestamp) {
|
||||
return right.timestamp - left.timestamp;
|
||||
}
|
||||
return right.branch.localeCompare(left.branch, "en", { numeric: true });
|
||||
});
|
||||
|
||||
return latest?.branch;
|
||||
}
|
||||
|
||||
run("pnpm", deployArgs, webDir);
|
||||
export function normalizeReleaseBranch(refName) {
|
||||
const branch = refName?.trim();
|
||||
if (!branch) return undefined;
|
||||
if (branch.startsWith("origin/release/")) {
|
||||
return branch.slice("origin/".length);
|
||||
}
|
||||
if (branch.startsWith("release/")) return branch;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function listReleaseBranchRefs() {
|
||||
const result = spawnSync(
|
||||
"git",
|
||||
[
|
||||
"for-each-ref",
|
||||
"--sort=-committerdate",
|
||||
"--format=%(committerdate:unix)%09%(refname:short)",
|
||||
"refs/remotes/origin/release",
|
||||
"refs/heads/release",
|
||||
],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
encoding: "utf8",
|
||||
shell: process.platform === "win32",
|
||||
},
|
||||
);
|
||||
|
||||
if (result.status !== 0) {
|
||||
const stderr = result.stderr?.trim();
|
||||
if (stderr) console.error(stderr);
|
||||
return [];
|
||||
}
|
||||
|
||||
return result.stdout.split(/\r?\n/).filter(Boolean);
|
||||
}
|
||||
|
||||
function refreshRemoteReleaseBranches() {
|
||||
spawnSync("git", ["remote", "prune", "origin"], {
|
||||
cwd: repoRoot,
|
||||
stdio: "ignore",
|
||||
shell: process.platform === "win32",
|
||||
});
|
||||
|
||||
spawnSync(
|
||||
"git",
|
||||
[
|
||||
"fetch",
|
||||
"--prune",
|
||||
"origin",
|
||||
"+refs/heads/release/*:refs/remotes/origin/release/*",
|
||||
],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
stdio: "ignore",
|
||||
shell: process.platform === "win32",
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function run(command, args, cwd) {
|
||||
const result = spawnSync(command, args, {
|
||||
@@ -52,7 +168,7 @@ function run(command, args, cwd) {
|
||||
}
|
||||
}
|
||||
|
||||
function validateEnv(mode) {
|
||||
function validateEnv(mode, env) {
|
||||
const fileEnv = [
|
||||
".env",
|
||||
".env.local",
|
||||
@@ -72,7 +188,7 @@ function validateEnv(mode) {
|
||||
"VITE_MSAL_REDIRECT_URI",
|
||||
];
|
||||
const missing = requiredKeys.filter((key) => {
|
||||
const value = process.env[key] ?? fileEnv[key];
|
||||
const value = env[key] ?? fileEnv[key];
|
||||
return !value || value.includes("<");
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user