chore: 增加部署状态与备份保留
This commit is contained in:
1 parent
d2ca528543
commit
7f67f4d685
5 files changed
+75
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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 反向代理
|
||||
|
||||
生产环境建议使用同源路径:
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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,
|
||||
]);
|
||||
}
|
||||
Reference in new issue
Block a user