92 lines
3.5 KiB
JavaScript
92 lines
3.5 KiB
JavaScript
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)), "../..");
|
|
if (process.argv.includes("--status")) {
|
|
const statusScript = path.join(root, "scripts/deploy/status.mjs");
|
|
const result = spawnSync(process.execPath, [statusScript], { cwd: root, stdio: "inherit" });
|
|
process.exit(result.status ?? 1);
|
|
}
|
|
const target = process.env.DEPLOY_TARGET ?? "oracle_arm";
|
|
const sshOptions = process.env.DEPLOY_SSH_OPTIONS
|
|
? process.env.DEPLOY_SSH_OPTIONS.split(" ").filter(Boolean)
|
|
: [];
|
|
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", [...sshOptions, 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", [...sshOptions, 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);
|