From 7f67f4d6853a8081ae7b5e124a524f0d9f68efe7 Mon Sep 17 00:00:00 2001 From: que01 Date: Tue, 13 Oct 2026 15:14:27 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=A2=9E=E5=8A=A0=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=B8=8E=E5=A4=87=E4=BB=BD=E4=BF=9D=E7=95=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/install-release.mjs | 10 ++++++ docs/deployment.md | 8 +++++ package.json | 1 + scripts/deploy/publish-release.mjs | 5 +++ scripts/deploy/status.mjs | 51 ++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 scripts/deploy/status.mjs diff --git a/deploy/install-release.mjs b/deploy/install-release.mjs index 7a4609c..30bbebf 100644 --- a/deploy/install-release.mjs +++ b/deploy/install-release.mjs @@ -157,6 +157,15 @@ async function backupDatabase(destination) { await rename(temporary, destination); } +async function pruneBackups(directory) { + const backups = (await readdir(directory, { withFileTypes: true })) + .filter((entry) => entry.isFile() && entry.name.endsWith(".dump")) + .map((entry) => entry.name) + .sort() + .reverse(); + for (const backup of backups.slice(7)) await rm(path.join(directory, backup)); +} + async function copyMissingFiles(sourceDirectory, destinationDirectory) { await mkdir(destinationDirectory, { recursive: true }); for (const entry of await readdir(sourceDirectory, { withFileTypes: true })) { @@ -318,6 +327,7 @@ try { ); run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "generate"], release); await backupDatabase(path.join(root, "shared/backups", manifest.version + ".dump")); + await pruneBackups(path.join(root, "shared/backups")); run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "migrate", "deploy"], release); const seedDirectory = path.join(process.env.LOCAL_MEDIA_ROOT, "seed"); await copyMissingFiles(path.join(release, "seed-media"), seedDirectory); diff --git a/docs/deployment.md b/docs/deployment.md index a42507c..d59d546 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -24,6 +24,14 @@ pnpm deploy:release DEPLOY_TARGET=oracle_arm PUBLIC_BASE_URL=https://wechat.app.que01.top pnpm deploy:release ``` +查看线上状态: + +```bash +pnpm deploy:status +``` + +数据库备份自动保留最近 7 份,发布失败时会保留备份文件,不自动删除数据库数据。 + ## Nginx 反向代理 生产环境建议使用同源路径: diff --git a/package.json b/package.json index 187c3b2..b7009ec 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "deploy:verify": "node scripts/deploy/verify-release.mjs", "deploy:package": "node scripts/deploy/package-release.mjs", "deploy:release": "node scripts/deploy/publish-release.mjs", + "deploy:status": "node scripts/deploy/status.mjs", "deploy:images": "node scripts/deploy/export-images.mjs", "test:deploy-verify": "node --test scripts/deploy/verify-release.test.mjs", "test:deploy": "node --test scripts/deploy/*.test.mjs deploy/*.test.mjs", diff --git a/scripts/deploy/publish-release.mjs b/scripts/deploy/publish-release.mjs index 26e4df5..dc4332d 100644 --- a/scripts/deploy/publish-release.mjs +++ b/scripts/deploy/publish-release.mjs @@ -4,6 +4,11 @@ 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 remoteRoot = process.env.DEPLOY_ROOT ?? "/home/ubuntu/persional-brand"; const publicBaseUrl = process.env.PUBLIC_BASE_URL ?? "https://wechat.app.que01.top"; diff --git a/scripts/deploy/status.mjs b/scripts/deploy/status.mjs new file mode 100644 index 0000000..0c6ccec --- /dev/null +++ b/scripts/deploy/status.mjs @@ -0,0 +1,51 @@ +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 publicBaseUrl = (process.env.PUBLIC_BASE_URL ?? "https://wechat.app.que01.top").replace( + /\/$/, + "", +); + +function run(command, args) { + const result = spawnSync(command, args, { cwd: root, encoding: "utf8" }); + if (result.status !== 0) throw new Error(result.stderr || command + " failed"); + process.stdout.write(result.stdout); +} + +run("ssh", [ + target, + "bash", + "-lc", + [ + 'source "$HOME/.nvm/nvm.sh"', + "export DEPLOY_ROOT=/home/ubuntu/persional-brand", + 'printf "current=%s\\n" "$(readlink /home/ubuntu/persional-brand/current)"', + 'printf "api="; test "$(pm2 pid personal-brand-api)" != "0"; echo online', + 'printf "admin="; systemctl --user is-active personal-brand-admin-static.service', + 'printf "database="; docker inspect -f "{{.State.Status}}" personal-brand-postgres-1', + 'printf "backups="; find /home/ubuntu/persional-brand/shared/backups -maxdepth 1 -name "*.dump" | wc -l', + ].join("; "), +]); + +for (const route of [ + "/admin/", + "/template-preview/", + "/api/health", + "/media/seed/food-package-02.jpg", +]) { + run("curl", [ + "--fail", + "--silent", + "--show-error", + "--max-time", + "10", + "-o", + "/dev/null", + "-w", + route + " %{http_code}\\n", + publicBaseUrl + route, + ]); +}