chore: 固化一键生产部署流程
CI / Static checks (push) Has been cancelled
CI / Production build (push) Has been cancelled
CI / API integration (push) Has been cancelled
CI / Browser regression (push) Has been cancelled

This commit is contained in:
que01 committed 2026-10-13 14:56:16 +08:00
1 parent c78deafee3
commit 47a24e0d53
6 files changed
+189 -40

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
import { access } from "node:fs/promises";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const target = process.env.DEPLOY_TARGET ?? "oracle_arm";
const remoteRoot = process.env.DEPLOY_ROOT ?? "/home/ubuntu/persional-brand";
const publicBaseUrl = process.env.PUBLIC_BASE_URL ?? "https://wechat.app.que01.top";
const timestamp = new Date().toISOString().replace(/\D/g, "").slice(0, 14);
const git = spawnSync("git", ["rev-parse", "--short=12", "HEAD"], {
cwd: root,
encoding: "utf8",
});
if (git.status !== 0) throw new Error("Cannot determine source revision");
const version = process.env.RELEASE_VERSION ?? timestamp + "-" + git.stdout.trim();
const releasePrefix = "personal-brand-" + version;
const archiveName = releasePrefix + ".tar.gz";
const archive = path.join(root, "dist/releases", archiveName);
function run(command, args, options = {}) {
const stdio = options.input === undefined ? "inherit" : ["pipe", "inherit", "inherit"];
const result = spawnSync(command, args, { cwd: root, stdio, ...options });
if (result.status !== 0) throw new Error(command + " failed with exit " + result.status);
}
if (!/^[a-zA-Z0-9_.@:-]+$/.test(target)) throw new Error("Invalid deployment SSH target");
if (!/^\/[a-zA-Z0-9_./-]+$/.test(remoteRoot)) throw new Error("Invalid deployment root");
const publicUrl = new URL(publicBaseUrl);
if (publicUrl.protocol !== "https:") throw new Error("PUBLIC_BASE_URL must use HTTPS");
const packageResult = spawnSync("pnpm", ["deploy:package"], {
cwd: root,
stdio: "inherit",
env: { ...process.env, RELEASE_VERSION: version },
});
if (packageResult.status !== 0) process.exit(packageResult.status ?? 1);
await access(archive);
await access(archive + ".sha256");
run("scp", [archive, archive + ".sha256", target + ":/tmp/"]);
const remoteScript = [
"set -eu",
'export NVM_DIR="$HOME/.nvm"',
'. "$NVM_DIR/nvm.sh"',
'release="/tmp/' + releasePrefix + '"',
'rm -rf "$release"',
'mkdir -p "$release"',
"cd /tmp",
'sha256sum -c "' + releasePrefix + '.tar.gz.sha256"',
'tar -xzf "' + archiveName + '" -C "$release"',
"pnpm_bin=$(find \"$HOME/.cache/pnpm/dlx\" -path '*/pnpm@9.15.0/node_modules/pnpm/bin/pnpm.cjs' -print -quit)",
'test -n "$pnpm_bin"',
"mkdir -p /tmp/pnpm-9.15-bin",
'ln -sf "$pnpm_bin" /tmp/pnpm-9.15-bin/pnpm',
"export PATH=/tmp/pnpm-9.15-bin:$PATH",
'export DEPLOY_ROOT="' + remoteRoot + '"',
'export PUBLIC_BASE_URL="' + publicBaseUrl + '"',
"export TRAEFIK_DYNAMIC_DIR=/home/ubuntu/traefik/dynamic",
'cd "$release"',
"node deploy/install-release.mjs .",
].join("; ");
run("ssh", [target, "bash -s"], { input: remoteScript + "\n" });
const checks = ["/admin/", "/template-preview/", "/api/health", "/media/seed/food-package-02.jpg"];
for (const route of checks) {
run("curl", [
"--fail",
"--silent",
"--show-error",
"--max-time",
"10",
"-o",
"/dev/null",
"-w",
route + " %{http_code}\\n",
publicBaseUrl + route,
]);
}
console.log("release published: " + version);
+29 -20
View File
@@ -21,21 +21,24 @@ const contentTypes = new Map([
]);
async function checkResource(url, expectedType, label) {
const response = await fetch(url);
const response = await fetch(url, { signal: AbortSignal.timeout(10000) });
const contentType = response.headers.get("content-type") ?? "";
assert.equal(response.status, 200, label + " returned " + response.status);
assert.ok(contentType.includes(expectedType), label + " has content type " + contentType);
await response.body?.cancel();
}
async function checkHtmlAssets(relativeEntry, localPath, publicPath) {
const html = await readFile(localPath, "utf8");
const checks = [];
for (const match of html.matchAll(/(?:src|href)=["']([^"']+)["']/g)) {
const asset = match[1];
if (!/\.(?:js|css)(?:[?#]|$)/.test(asset)) continue;
const url = new URL(asset, baseUrl + publicPath);
const expectedType = /\.css(?:[?#]|$)/.test(url.pathname) ? "text/css" : "javascript";
await checkResource(url, expectedType, relativeEntry + " -> " + asset);
checks.push(checkResource(url, expectedType, relativeEntry + " -> " + asset));
}
await Promise.all(checks);
}
async function listFiles(directory, prefix = "") {
@@ -49,7 +52,9 @@ async function listFiles(directory, prefix = "") {
return files;
}
const apiHealthResponse = await fetch(baseUrl + "/api/health");
const apiHealthResponse = await fetch(baseUrl + "/api/health", {
signal: AbortSignal.timeout(10000),
});
assert.equal(apiHealthResponse.status, 200, "public API health check failed");
assert.match(apiHealthResponse.headers.get("content-type") ?? "", /application\/json/);
assert.equal((await apiHealthResponse.json()).status, "ok");
@@ -67,23 +72,27 @@ for (const [entry, localFile, publicPath] of [
}
const assetRoot = path.join(root, "apps/admin/dist/assets");
for (const asset of await listFiles(assetRoot)) {
const extension = path.extname(asset).toLowerCase();
const expectedType = contentTypes.get(extension);
if (!expectedType) continue;
await checkResource(
baseUrl + "/assets/" + asset.split("/").map(encodeURIComponent).join("/"),
expectedType,
"static asset " + asset,
);
}
await Promise.all(
(await listFiles(assetRoot)).map(async (asset) => {
const extension = path.extname(asset).toLowerCase();
const expectedType = contentTypes.get(extension);
if (!expectedType) return;
await checkResource(
baseUrl + "/assets/" + asset.split("/").map(encodeURIComponent).join("/"),
expectedType,
"static asset " + asset,
);
}),
);
for (const entry of await readdir(path.join(root, "seed-media"))) {
await checkResource(
baseUrl + "/media/seed/" + encodeURIComponent(entry),
"image/jpeg",
"seed media " + entry,
);
}
await Promise.all(
(await readdir(path.join(root, "seed-media"))).map((entry) =>
checkResource(
baseUrl + "/media/seed/" + encodeURIComponent(entry),
"image/jpeg",
"seed media " + entry,
),
),
);
console.log("public release verified: admin, preview, assets, and seed media");