Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e1d4d0326 | ||
|
|
92227e1311 | ||
|
|
e75c524343 | ||
|
|
1a4f71996a | ||
|
|
a2fa6c3c31 | ||
|
|
015947f8fb | ||
|
|
072a1037ac | ||
|
|
447c224fce | ||
|
|
f2fc1e0810 | ||
|
|
12a3bf2f3b | ||
|
|
ca6d1712bd | ||
|
|
e7ef0b3ce5 | ||
|
|
b82aa12875 | ||
|
|
b43b6f2785 | ||
|
|
965dd10219 | ||
|
|
371689484e |
No files matched your search
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"scope": "模板完成证据登记,保留命令、日志与产物哈希",
|
||||
"generatedAt": "2026-10-23T06:35:59.337Z",
|
||||
"generatedAt": "2026-10-23T11:27:49.824Z",
|
||||
"templates": {
|
||||
"acheng-campus-food": {
|
||||
"real-manifest-interaction": {
|
||||
@@ -256,5 +256,5 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"sourceTreeHash": "f39e31a1959e24c07bf60776a0d3c5f28e5745962ca004301ff304daa1430041"
|
||||
"sourceTreeHash": "1dbd5f3a24cc1f86925b4bcd05c99b45cc00d6a39dabe2302ad07ba7f763c79c"
|
||||
}
|
||||
@@ -16,6 +16,12 @@ API_PORT=3001
|
||||
API_PREFIX=
|
||||
CORS_ORIGINS=
|
||||
|
||||
# 发布备份保留 7 份;定时备份默认保留 14 天
|
||||
BACKUP_RETENTION_DAYS=14
|
||||
# 可选:配置 rsync 目标后,每次定时备份会额外复制一份到异地
|
||||
# 需要预先为 ubuntu 用户配置 SSH key;未配置时备份只保留在本机
|
||||
# BACKUP_OFFSITE_TARGET=backup@example-host:/srv/personal-brand-backups/
|
||||
|
||||
LOCAL_MEDIA_ROOT=/home/ubuntu/persional-brand/media
|
||||
LOCAL_MEDIA_BASE_URL=/media
|
||||
OBJECT_STORAGE_ENDPOINT=
|
||||
@@ -25,6 +31,8 @@ OBJECT_STORAGE_ACCESS_KEY=
|
||||
OBJECT_STORAGE_SECRET_KEY=
|
||||
OBJECT_STORAGE_REGION=us-east-1
|
||||
|
||||
# 访客微信登录配置,与后台「小程序上传配置」相互独立:
|
||||
# AppID 必须与上传配置一致;AppSecret 只放在服务器环境变量中
|
||||
WECHAT_APP_ID=
|
||||
WECHAT_APP_SECRET=
|
||||
|
||||
|
||||
+73
-27
@@ -3,7 +3,7 @@ name: CI
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
# 纯文档提交不消耗 2 核 runner;需要状态时用 workflow_dispatch 手动触发。
|
||||
# 纯文档提交不占用 runner;需要状态时用 workflow_dispatch 手动触发。
|
||||
paths-ignore:
|
||||
- "**.md"
|
||||
# PR 不设置 paths-ignore:branch protection 的 required checks 必须始终上报,
|
||||
@@ -18,6 +18,8 @@ concurrency:
|
||||
env:
|
||||
NODE_VERSION: "24"
|
||||
PNPM_VERSION: "9.15.0"
|
||||
# runner 任务容器把该路径 bind mount 到宿主机共享 store,避免每个 job 重复下载依赖。
|
||||
npm_config_store_dir: /workspace/.pnpm-store
|
||||
|
||||
jobs:
|
||||
static:
|
||||
@@ -26,16 +28,32 @@ jobs:
|
||||
timeout-minutes: 40
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: pnpm/action-setup@v4
|
||||
- name: Verify preinstalled toolchain
|
||||
# 任务镜像已内置 Node 与 pnpm;不在这里下载 Node,避免 nodejs.org 慢连接拖死整个 job。
|
||||
run: |
|
||||
actual_node="$(node -p 'process.versions.node')"
|
||||
actual_pnpm="$(pnpm --version)"
|
||||
test "${actual_node%%.*}" = "$NODE_VERSION" || { echo "expected Node $NODE_VERSION.x, got $actual_node" >&2; exit 1; }
|
||||
test "$actual_pnpm" = "$PNPM_VERSION" || { echo "expected pnpm $PNPM_VERSION, got $actual_pnpm" >&2; exit 1; }
|
||||
- name: Restore Prisma engine cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
version: ${{ env.PNPM_VERSION }}
|
||||
- uses: actions/setup-node@v4
|
||||
path: ~/.cache/prisma
|
||||
key: prisma-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}
|
||||
restore-keys: |
|
||||
prisma-${{ runner.os }}-${{ runner.arch }}-
|
||||
- name: Restore Turbo cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
node-version: ${{ env.NODE_VERSION }}
|
||||
cache: pnpm
|
||||
path: .turbo/cache
|
||||
key: turbo-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}-${{ github.sha }}
|
||||
restore-keys: |
|
||||
turbo-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}-
|
||||
turbo-${{ runner.os }}-${{ runner.arch }}-
|
||||
- run: pnpm install --frozen-lockfile
|
||||
- run: pnpm check
|
||||
- run: pnpm test:api-routes
|
||||
- run: pnpm test:deploy
|
||||
- run: pnpm validate:templates
|
||||
- run: pnpm template:compatibility
|
||||
- run: pnpm --filter @personal-brand/api exec tsx --test test/template-preview-matrix.test.ts
|
||||
@@ -49,13 +67,28 @@ jobs:
|
||||
needs: [static]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: pnpm/action-setup@v4
|
||||
- name: Verify preinstalled toolchain
|
||||
# 任务镜像已内置 Node 与 pnpm;不在这里下载 Node,避免 nodejs.org 慢连接拖死整个 job。
|
||||
run: |
|
||||
actual_node="$(node -p 'process.versions.node')"
|
||||
actual_pnpm="$(pnpm --version)"
|
||||
test "${actual_node%%.*}" = "$NODE_VERSION" || { echo "expected Node $NODE_VERSION.x, got $actual_node" >&2; exit 1; }
|
||||
test "$actual_pnpm" = "$PNPM_VERSION" || { echo "expected pnpm $PNPM_VERSION, got $actual_pnpm" >&2; exit 1; }
|
||||
- name: Restore Prisma engine cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
version: ${{ env.PNPM_VERSION }}
|
||||
- uses: actions/setup-node@v4
|
||||
path: ~/.cache/prisma
|
||||
key: prisma-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}
|
||||
restore-keys: |
|
||||
prisma-${{ runner.os }}-${{ runner.arch }}-
|
||||
- name: Restore Turbo cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
node-version: ${{ env.NODE_VERSION }}
|
||||
cache: pnpm
|
||||
path: .turbo/cache
|
||||
key: turbo-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}-${{ github.sha }}
|
||||
restore-keys: |
|
||||
turbo-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}-
|
||||
turbo-${{ runner.os }}-${{ runner.arch }}-
|
||||
- run: pnpm install --frozen-lockfile
|
||||
- run: pnpm build
|
||||
- run: pnpm build:admin:preview
|
||||
@@ -67,24 +100,31 @@ jobs:
|
||||
if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 30
|
||||
needs: [static]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: pnpm/action-setup@v4
|
||||
- name: Verify preinstalled toolchain
|
||||
# 任务镜像已内置 Node 与 pnpm;不在这里下载 Node,避免 nodejs.org 慢连接拖死整个 job。
|
||||
run: |
|
||||
actual_node="$(node -p 'process.versions.node')"
|
||||
actual_pnpm="$(pnpm --version)"
|
||||
test "${actual_node%%.*}" = "$NODE_VERSION" || { echo "expected Node $NODE_VERSION.x, got $actual_node" >&2; exit 1; }
|
||||
test "$actual_pnpm" = "$PNPM_VERSION" || { echo "expected pnpm $PNPM_VERSION, got $actual_pnpm" >&2; exit 1; }
|
||||
- name: Restore Prisma engine cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
version: ${{ env.PNPM_VERSION }}
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: ${{ env.NODE_VERSION }}
|
||||
cache: pnpm
|
||||
path: ~/.cache/prisma
|
||||
key: prisma-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}
|
||||
restore-keys: |
|
||||
prisma-${{ runner.os }}-${{ runner.arch }}-
|
||||
- run: pnpm install --frozen-lockfile
|
||||
- name: Cache Playwright browsers
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: ~/.cache/ms-playwright
|
||||
key: playwright-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
|
||||
key: playwright-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}
|
||||
- name: Install Chromium
|
||||
run: pnpm --filter @personal-brand/taro exec playwright install --with-deps chromium
|
||||
# 系统依赖与同版本 Chromium 已预装在 personal-brand-ci:node24 中;这里只做版本兜底。
|
||||
run: pnpm --filter @personal-brand/taro exec playwright install chromium
|
||||
- name: Run PR browser smoke
|
||||
run: pnpm test:browser:smoke
|
||||
|
||||
@@ -92,7 +132,6 @@ jobs:
|
||||
name: API integration
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
needs: [static]
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16-alpine
|
||||
@@ -112,13 +151,20 @@ jobs:
|
||||
SEED_ADMIN_PASSWORD: ci-integration-admin-password
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: pnpm/action-setup@v4
|
||||
- name: Verify preinstalled toolchain
|
||||
# 任务镜像已内置 Node 与 pnpm;不在这里下载 Node,避免 nodejs.org 慢连接拖死整个 job。
|
||||
run: |
|
||||
actual_node="$(node -p 'process.versions.node')"
|
||||
actual_pnpm="$(pnpm --version)"
|
||||
test "${actual_node%%.*}" = "$NODE_VERSION" || { echo "expected Node $NODE_VERSION.x, got $actual_node" >&2; exit 1; }
|
||||
test "$actual_pnpm" = "$PNPM_VERSION" || { echo "expected pnpm $PNPM_VERSION, got $actual_pnpm" >&2; exit 1; }
|
||||
- name: Restore Prisma engine cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
version: ${{ env.PNPM_VERSION }}
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: ${{ env.NODE_VERSION }}
|
||||
cache: pnpm
|
||||
path: ~/.cache/prisma
|
||||
key: prisma-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}
|
||||
restore-keys: |
|
||||
prisma-${{ runner.os }}-${{ runner.arch }}-
|
||||
- run: pnpm install --frozen-lockfile
|
||||
- name: Prepare API test env
|
||||
working-directory: apps/api
|
||||
|
||||
+7
-3
@@ -37,10 +37,10 @@ ADR 0033 已落地:
|
||||
当前生产 release:
|
||||
|
||||
```text
|
||||
20261021034700-adcaa12970af
|
||||
20261023100112-a2fa6c3c3140
|
||||
```
|
||||
|
||||
该 release 由提交 `adcaa12` 构建,包含后台渲染异常上报(`POST /admin/client-errors`,含去重与限流)、Admin 列表接口运行时响应契约校验,以及前序 release 的创建品牌与页面方案交互修复、Admin 渲染错误边界和异常数据容忍。部署后已完成公开路由健康检查,并确认未登录调用异常上报接口返回 `401 HTTP_401`,说明新接口已进入生产路由。
|
||||
该 release 由提交 `a2fa6c3` 构建,新增生产数据库每日定时备份、备份可读性校验、发布/定时备份保留策略、`backup-timer` 与 `backup-age-hours` 部署状态门禁。部署后已手动触发一次 `personal-brand-backup.service`,生成 `scheduled-20261023T100426Z-1029929.dump`,并通过 `pg_restore -l` 校验可读。
|
||||
|
||||
生产健康检查:
|
||||
|
||||
@@ -49,7 +49,11 @@ api=online
|
||||
pm2-autostart=enabled
|
||||
admin=active
|
||||
database=running
|
||||
backups=7
|
||||
backups=8
|
||||
latest-backup=2026-10-23 10:04:27 +0000 scheduled-20261023T100426Z-1029929.dump
|
||||
backup-timer=active
|
||||
backup-age-hours=0
|
||||
backup-temp-files=0
|
||||
/admin/ 200
|
||||
/template-preview/ 200
|
||||
/api/health 200
|
||||
|
||||
+6
-1
@@ -18,7 +18,12 @@ RUN pnpm --filter @personal-brand/config build
|
||||
RUN pnpm --filter @personal-brand/types build
|
||||
RUN pnpm --filter @personal-brand/validation build
|
||||
RUN pnpm --filter @personal-brand/api-client build
|
||||
RUN pnpm add -w @tarojs/binding-linux-arm64-gnu@4.3.0
|
||||
ARG TARGETARCH
|
||||
RUN case "${TARGETARCH:-$(uname -m)}" in \
|
||||
arm64 | aarch64) pnpm add -w @tarojs/binding-linux-arm64-gnu@4.3.0 ;; \
|
||||
amd64 | x86_64) pnpm add -w @tarojs/binding-linux-x64-gnu@4.3.0 ;; \
|
||||
*) echo "unsupported TARGETARCH: ${TARGETARCH:-$(uname -m)}" >&2; exit 1 ;; \
|
||||
esac
|
||||
RUN pnpm build:admin:preview
|
||||
|
||||
FROM alpine:latest AS runtime
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { mkdir, rm } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import {
|
||||
backupPostgresDatabase,
|
||||
pruneBackups,
|
||||
scheduledBackupName,
|
||||
verifyPostgresBackup,
|
||||
} from "./database-backup.mjs";
|
||||
|
||||
const root = process.env.DEPLOY_ROOT;
|
||||
if (!root || !path.isAbsolute(root)) throw new Error("DEPLOY_ROOT must be absolute");
|
||||
process.loadEnvFile(path.join(root, "shared/.env.production"));
|
||||
|
||||
const container = process.env.POSTGRES_CONTAINER;
|
||||
if (!container || !/^[a-zA-Z0-9_.-]+$/.test(container)) {
|
||||
throw new Error("POSTGRES_CONTAINER is required");
|
||||
}
|
||||
const retentionDays = Number(process.env.BACKUP_RETENTION_DAYS ?? "14");
|
||||
if (!Number.isInteger(retentionDays) || retentionDays < 1) {
|
||||
throw new Error("BACKUP_RETENTION_DAYS must be a positive integer");
|
||||
}
|
||||
|
||||
const directory = path.join(root, "shared/backups");
|
||||
await mkdir(directory, { recursive: true });
|
||||
const destination = path.join(directory, scheduledBackupName());
|
||||
|
||||
await backupPostgresDatabase({ container, destination });
|
||||
try {
|
||||
await verifyPostgresBackup({ container, file: destination });
|
||||
} catch (error) {
|
||||
await rm(destination, { force: true });
|
||||
throw error;
|
||||
}
|
||||
await pruneBackups(directory, { retentionDays });
|
||||
|
||||
const offsiteTarget = process.env.BACKUP_OFFSITE_TARGET?.trim();
|
||||
if (offsiteTarget) {
|
||||
if (
|
||||
!/^[a-zA-Z0-9_.@:/-]+$/.test(offsiteTarget) ||
|
||||
offsiteTarget.startsWith("-") ||
|
||||
offsiteTarget.includes("..")
|
||||
) {
|
||||
throw new Error("BACKUP_OFFSITE_TARGET contains unsupported characters");
|
||||
}
|
||||
const result = spawnSync("rsync", ["-a", "--partial", destination, offsiteTarget], {
|
||||
stdio: "inherit",
|
||||
env: process.env,
|
||||
});
|
||||
if (result.status !== 0) throw new Error("Offsite backup copy failed");
|
||||
}
|
||||
|
||||
console.log(`scheduled backup written: ${destination}`);
|
||||
@@ -0,0 +1,138 @@
|
||||
import { createReadStream, createWriteStream } from "node:fs";
|
||||
import { readdir, rename, rm, stat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
const SCHEDULED_BACKUP = /^scheduled-\d{8}T\d{6}Z-\d+\.dump$/;
|
||||
|
||||
/**
|
||||
* 定时备份文件名包含 UTC 秒级时间和进程号,避免同一秒内重复执行时覆盖已有备份。
|
||||
*/
|
||||
export function scheduledBackupName(date = new Date(), pid = process.pid) {
|
||||
const stamp = date
|
||||
.toISOString()
|
||||
.replace(/[-:]/g, "")
|
||||
.replace(/\.\d{3}Z$/, "Z");
|
||||
if (!/^\d{8}T\d{6}Z$/.test(stamp)) throw new Error("Invalid backup timestamp");
|
||||
return `scheduled-${stamp}-${pid}.dump`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保留策略:
|
||||
* - 发布前备份按文件名保留最近 7 份,保持原有发布回滚能力;
|
||||
* - 定时备份按修改时间保留最近 retentionDays 天;
|
||||
* - 同名临时文件由 pruneBackups 统一清理。
|
||||
*/
|
||||
export function selectExpiredBackups(
|
||||
entries,
|
||||
{ now = Date.now(), retentionDays = 14, keepReleaseBackups = 7 } = {},
|
||||
) {
|
||||
if (!Number.isInteger(retentionDays) || retentionDays < 1) {
|
||||
throw new Error("retentionDays must be a positive integer");
|
||||
}
|
||||
if (!Number.isInteger(keepReleaseBackups) || keepReleaseBackups < 1) {
|
||||
throw new Error("keepReleaseBackups must be a positive integer");
|
||||
}
|
||||
|
||||
const cutoff = now - retentionDays * DAY_MS;
|
||||
const expired = [];
|
||||
const releaseBackups = entries
|
||||
.filter((entry) => entry.name.endsWith(".dump") && !SCHEDULED_BACKUP.test(entry.name))
|
||||
.sort((left, right) => right.name.localeCompare(left.name));
|
||||
|
||||
for (const entry of releaseBackups.slice(keepReleaseBackups)) expired.push(entry.name);
|
||||
for (const entry of entries) {
|
||||
if (SCHEDULED_BACKUP.test(entry.name) && entry.mtimeMs < cutoff) expired.push(entry.name);
|
||||
}
|
||||
|
||||
return [...new Set(expired)];
|
||||
}
|
||||
|
||||
export async function backupPostgresDatabase({
|
||||
container,
|
||||
destination,
|
||||
docker = "docker",
|
||||
env = process.env,
|
||||
}) {
|
||||
const temporary = destination + ".tmp";
|
||||
const child = spawn(
|
||||
docker,
|
||||
["exec", container, "sh", "-c", 'exec pg_dump -Fc -U "$POSTGRES_USER" -d "$POSTGRES_DB"'],
|
||||
{ stdio: ["ignore", "pipe", "inherit"], env },
|
||||
);
|
||||
const output = createWriteStream(temporary, { flags: "wx", mode: 0o600 });
|
||||
child.stdout.pipe(output);
|
||||
try {
|
||||
await Promise.all([
|
||||
new Promise((resolve, reject) => {
|
||||
child.on("error", reject);
|
||||
child.on("exit", (code) =>
|
||||
code === 0 ? resolve() : reject(new Error("Database backup failed")),
|
||||
);
|
||||
}),
|
||||
new Promise((resolve, reject) => {
|
||||
output.on("finish", resolve);
|
||||
output.on("error", reject);
|
||||
}),
|
||||
]);
|
||||
await rename(temporary, destination);
|
||||
} catch (error) {
|
||||
child.kill("SIGTERM");
|
||||
output.destroy();
|
||||
await rm(temporary, { force: true });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function verifyPostgresBackup({
|
||||
container,
|
||||
file,
|
||||
docker = "docker",
|
||||
env = process.env,
|
||||
}) {
|
||||
const child = spawn(docker, ["exec", "-i", container, "pg_restore", "-l"], {
|
||||
stdio: ["pipe", "ignore", "pipe"],
|
||||
env,
|
||||
});
|
||||
const input = createReadStream(file);
|
||||
let stderr = "";
|
||||
child.stderr.setEncoding("utf8");
|
||||
child.stderr.on("data", (chunk) => {
|
||||
stderr += chunk;
|
||||
});
|
||||
input.pipe(child.stdin);
|
||||
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
child.on("error", reject);
|
||||
input.on("error", reject);
|
||||
child.stdin.on("error", (error) => {
|
||||
if (error.code !== "EPIPE") reject(error);
|
||||
});
|
||||
child.on("exit", (code) => {
|
||||
if (code === 0) resolve();
|
||||
else reject(new Error(`Backup is not readable: ${stderr.trim() || "pg_restore failed"}`));
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
input.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
export async function pruneBackups(directory, options = {}) {
|
||||
const entries = await readdir(directory);
|
||||
const withStats = [];
|
||||
for (const name of entries) {
|
||||
const info = await stat(path.join(directory, name));
|
||||
if (!info.isFile()) continue;
|
||||
withStats.push({ name, mtimeMs: info.mtimeMs });
|
||||
}
|
||||
|
||||
for (const name of selectExpiredBackups(withStats, options)) {
|
||||
await rm(path.join(directory, name));
|
||||
}
|
||||
for (const name of entries) {
|
||||
if (name.endsWith(".dump.tmp")) await rm(path.join(directory, name), { force: true });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { chmod, mkdtemp, readFile, rm, stat, utimes, writeFile } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import {
|
||||
backupPostgresDatabase,
|
||||
pruneBackups,
|
||||
scheduledBackupName,
|
||||
selectExpiredBackups,
|
||||
verifyPostgresBackup,
|
||||
} from "./database-backup.mjs";
|
||||
|
||||
test("定时备份文件名使用 UTC 时间和进程号", () => {
|
||||
assert.equal(
|
||||
scheduledBackupName(new Date("2026-10-23T09:45:00.123Z"), 42),
|
||||
"scheduled-20261023T094500Z-42.dump",
|
||||
);
|
||||
});
|
||||
|
||||
test("发布备份只保留最近 7 份,定时备份按保留天数清理", () => {
|
||||
const now = Date.parse("2026-10-23T12:00:00Z");
|
||||
const day = 24 * 60 * 60 * 1000;
|
||||
const entries = [
|
||||
...Array.from({ length: 8 }, (_, index) => ({
|
||||
name: `2026100${index + 1}000000-release${index}.dump`,
|
||||
mtimeMs: now,
|
||||
})),
|
||||
{ name: "scheduled-20261001T000000Z-1.dump", mtimeMs: now - 30 * day },
|
||||
{ name: "scheduled-20261020T000000Z-2.dump", mtimeMs: now - 3 * day },
|
||||
];
|
||||
|
||||
assert.deepEqual(selectExpiredBackups(entries, { now, retentionDays: 14 }), [
|
||||
"20261001000000-release0.dump",
|
||||
"scheduled-20261001T000000Z-1.dump",
|
||||
]);
|
||||
});
|
||||
|
||||
test("非法保留天数会被拒绝", () => {
|
||||
assert.throws(() => selectExpiredBackups([], { retentionDays: 0 }), /positive integer/);
|
||||
});
|
||||
|
||||
test("pruneBackups 删除过期备份和残留临时文件", async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-backup-"));
|
||||
const old = path.join(directory, "scheduled-20260101T000000Z-1.dump");
|
||||
const current = path.join(directory, "scheduled-20261023T000000Z-2.dump");
|
||||
const temporary = path.join(directory, "scheduled-20261023T000000Z-3.dump.tmp");
|
||||
try {
|
||||
await writeFile(old, "old");
|
||||
await writeFile(current, "current");
|
||||
await writeFile(temporary, "partial");
|
||||
const oldTime = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
|
||||
await utimes(old, oldTime, oldTime);
|
||||
|
||||
await pruneBackups(directory, { retentionDays: 14 });
|
||||
|
||||
await assert.rejects(stat(old), { code: "ENOENT" });
|
||||
await assert.rejects(stat(temporary), { code: "ENOENT" });
|
||||
assert.equal((await stat(current)).size, 7);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
async function writeFakeDocker(directory, body) {
|
||||
const file = path.join(directory, "docker");
|
||||
await writeFile(file, "#!/bin/sh\n" + body, { mode: 0o755 });
|
||||
await chmod(file, 0o755);
|
||||
return file;
|
||||
}
|
||||
|
||||
test("备份写入权限受限的 dump 文件并清理临时文件", async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-dump-"));
|
||||
const destination = path.join(directory, "release.dump");
|
||||
try {
|
||||
const docker = await writeFakeDocker(directory, "printf 'dump-bytes'\n");
|
||||
await backupPostgresDatabase({ container: "postgres", destination, docker });
|
||||
|
||||
assert.equal(await readFile(destination, "utf8"), "dump-bytes");
|
||||
assert.equal((await stat(destination)).mode & 0o777, 0o600);
|
||||
await assert.rejects(stat(destination + ".tmp"), { code: "ENOENT" });
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("pg_dump 失败时删除半成品备份", async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-dump-"));
|
||||
const destination = path.join(directory, "release.dump");
|
||||
try {
|
||||
const docker = await writeFakeDocker(directory, "printf 'partial'; exit 1\n");
|
||||
await assert.rejects(
|
||||
backupPostgresDatabase({ container: "postgres", destination, docker }),
|
||||
/Database backup failed/,
|
||||
);
|
||||
|
||||
await assert.rejects(stat(destination), { code: "ENOENT" });
|
||||
await assert.rejects(stat(destination + ".tmp"), { code: "ENOENT" });
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("备份可被 pg_restore 读取校验", async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-verify-"));
|
||||
const file = path.join(directory, "release.dump");
|
||||
try {
|
||||
await writeFile(file, "dump-bytes");
|
||||
const docker = await writeFakeDocker(directory, "cat >/dev/null; exit 0\n");
|
||||
await verifyPostgresBackup({ container: "postgres", file, docker });
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("pg_restore 校验失败时报告不可读", async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-verify-"));
|
||||
const file = path.join(directory, "release.dump");
|
||||
try {
|
||||
await writeFile(file, "broken");
|
||||
const docker = await writeFakeDocker(
|
||||
directory,
|
||||
"cat >/dev/null; echo 'invalid header' >&2; exit 1\n",
|
||||
);
|
||||
await assert.rejects(
|
||||
verifyPostgresBackup({ container: "postgres", file, docker }),
|
||||
/invalid header/,
|
||||
);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
+39
-51
@@ -1,4 +1,3 @@
|
||||
import { createWriteStream } from "node:fs";
|
||||
import {
|
||||
copyFile,
|
||||
cp,
|
||||
@@ -14,9 +13,10 @@ import {
|
||||
writeFile,
|
||||
} from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { checkPackage } from "../scripts/deploy/check-package.mjs";
|
||||
import { assertReleaseEnvironment } from "./release-environment.mjs";
|
||||
import { backupPostgresDatabase, pruneBackups, verifyPostgresBackup } from "./database-backup.mjs";
|
||||
|
||||
const root = process.env.DEPLOY_ROOT;
|
||||
if (!root || !path.isAbsolute(root)) throw new Error("DEPLOY_ROOT must be absolute");
|
||||
@@ -98,6 +98,37 @@ async function installPm2Service() {
|
||||
run("systemctl", ["--user", "enable", pm2ServiceName]);
|
||||
}
|
||||
|
||||
const backupTimerName = "personal-brand-backup.timer";
|
||||
|
||||
async function installBackupTimer(release) {
|
||||
const directory = path.dirname(adminServiceFile);
|
||||
const serviceFile = path.join(directory, "personal-brand-backup.service");
|
||||
const timerFile = path.join(directory, backupTimerName);
|
||||
await mkdir(directory, { recursive: true });
|
||||
// 定时任务从 shared 读取稳定副本;发布回滚后 current 变了,备份任务仍然可用。
|
||||
await copyFile(
|
||||
path.join(release, "deploy/backup-db.mjs"),
|
||||
path.join(root, "shared/backup-db.mjs"),
|
||||
);
|
||||
await copyFile(
|
||||
path.join(release, "deploy/database-backup.mjs"),
|
||||
path.join(root, "shared/database-backup.mjs"),
|
||||
);
|
||||
await writeFile(
|
||||
serviceFile,
|
||||
`[Unit]\nDescription=Personal Brand daily database backup\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\nType=oneshot\nEnvironment=DEPLOY_ROOT=${root}\nEnvironment=HOME=${process.env.HOME ?? "/home/ubuntu"}\nEnvironment=PATH=${process.env.PATH}\nExecStart=${process.execPath} ${root}/shared/backup-db.mjs\n`,
|
||||
{ mode: 0o644 },
|
||||
);
|
||||
await writeFile(
|
||||
timerFile,
|
||||
`[Unit]\nDescription=Daily Personal Brand database backup\n\n[Timer]\nOnCalendar=*-*-* 03:30:00\nPersistent=true\nRandomizedDelaySec=10m\n\n[Install]\nWantedBy=timers.target\n`,
|
||||
{ mode: 0o644 },
|
||||
);
|
||||
run("systemctl", ["--user", "daemon-reload"]);
|
||||
run("systemctl", ["--user", "enable", "--now", backupTimerName]);
|
||||
run("systemctl", ["--user", "restart", backupTimerName]);
|
||||
}
|
||||
|
||||
async function waitForAdminHealth() {
|
||||
const url = "http://127.0.0.1:" + (process.env.ADMIN_STATIC_PORT ?? "3010") + "/admin/";
|
||||
for (let attempt = 0; attempt < 20; attempt++) {
|
||||
@@ -134,53 +165,6 @@ async function switchCurrent(target) {
|
||||
await rename(temporary, current);
|
||||
}
|
||||
|
||||
async function backupDatabase(destination) {
|
||||
const temporary = destination + ".tmp";
|
||||
const child = spawn(
|
||||
"docker",
|
||||
[
|
||||
"exec",
|
||||
postgresContainer,
|
||||
"sh",
|
||||
"-c",
|
||||
'exec pg_dump -Fc -U "$POSTGRES_USER" -d "$POSTGRES_DB"',
|
||||
],
|
||||
{ stdio: ["ignore", "pipe", "inherit"] },
|
||||
);
|
||||
const output = createWriteStream(temporary, { flags: "wx", mode: 0o600 });
|
||||
child.stdout.pipe(output);
|
||||
try {
|
||||
await Promise.all([
|
||||
new Promise((resolve, reject) => {
|
||||
child.on("error", reject);
|
||||
child.on("exit", (code) =>
|
||||
code === 0 ? resolve() : reject(new Error("Database backup failed")),
|
||||
);
|
||||
}),
|
||||
new Promise((resolve, reject) => {
|
||||
output.on("finish", resolve);
|
||||
output.on("error", reject);
|
||||
}),
|
||||
]);
|
||||
await rename(temporary, destination);
|
||||
} catch (error) {
|
||||
await rm(temporary, { force: true });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
for (const entry of await readdir(directory)) {
|
||||
if (entry.endsWith(".dump.tmp")) await rm(path.join(directory, entry), { force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function pruneReleases(directory, currentRelease, previousRelease) {
|
||||
const releases = (await readdir(directory, { withFileTypes: true }))
|
||||
.filter((entry) => entry.isDirectory())
|
||||
@@ -367,8 +351,11 @@ try {
|
||||
release,
|
||||
);
|
||||
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"));
|
||||
const backupDirectory = path.join(root, "shared/backups");
|
||||
const releaseBackup = path.join(backupDirectory, manifest.version + ".dump");
|
||||
await backupPostgresDatabase({ container: postgresContainer, destination: releaseBackup });
|
||||
await verifyPostgresBackup({ container: postgresContainer, file: releaseBackup });
|
||||
await pruneBackups(backupDirectory);
|
||||
// 旧数据的模板包声明必须先收敛,migration 才能安全地删除兼容字段。
|
||||
run(process.execPath, ["deploy/prepare-template-contract.mjs", ".", "--apply"], release);
|
||||
run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "migrate", "deploy"], release);
|
||||
@@ -390,6 +377,7 @@ try {
|
||||
await writeFile(path.join(root, "shared/previous-release"), previous + "\n");
|
||||
run("pm2", ["save"]);
|
||||
await pruneReleases(path.join(root, "releases"), release, previous);
|
||||
await installBackupTimer(release);
|
||||
console.log("release installed: " + manifest.version);
|
||||
} catch (error) {
|
||||
if (routeUpdated) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
# 本地 CI 任务容器基础镜像:Node 24 + pnpm + Playwright 运行依赖。
|
||||
# 构建(在仓库根目录执行):
|
||||
# docker build -f deploy/runner/Dockerfile -t personal-brand-ci:node24 .
|
||||
# Playwright 版本需与 apps/taro/package.json 保持一致;升级 Playwright 后重建本镜像。
|
||||
FROM node:24-bookworm
|
||||
|
||||
ARG PNPM_VERSION=9.15.0
|
||||
ARG PLAYWRIGHT_VERSION=1.58.2
|
||||
# 必须与 pnpm-lock.yaml 中解析出的 Prisma 版本一致;升级 Prisma 后同步修改并重建镜像。
|
||||
ARG PRISMA_VERSION=6.19.3
|
||||
|
||||
ENV PNPM_HOME=/usr/local/share/pnpm
|
||||
ENV PATH=${PNPM_HOME}:${PATH}
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends jq unzip \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN corepack enable \
|
||||
&& corepack prepare pnpm@${PNPM_VERSION} --activate \
|
||||
&& pnpm --version
|
||||
|
||||
# 预装 Chromium 的系统依赖和浏览器本体,避免每个 job 重复执行 apt 与下载。
|
||||
RUN npx --yes playwright@${PLAYWRIGHT_VERSION} install --with-deps chromium \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# 预热 Prisma engines 到 /root/.cache/prisma。CI job 每次都是新容器,若不预热,
|
||||
# 每个 job 的 prisma generate 都会重新访问 binaries.prisma.sh,网络抖动会直接拖垮 CI。
|
||||
COPY apps/api/prisma/schema.prisma /tmp/prisma-warmup/schema.prisma
|
||||
RUN printf '%s\n' '{"name":"prisma-warmup","private":true}' > /tmp/prisma-warmup/package.json \
|
||||
&& cd /tmp/prisma-warmup \
|
||||
&& DATABASE_URL=postgresql://warmup:warmup@127.0.0.1:5432/warmup npx --yes prisma@${PRISMA_VERSION} generate --schema schema.prisma \
|
||||
&& rm -rf /tmp/prisma-warmup /root/.npm
|
||||
|
||||
WORKDIR /workspace
|
||||
@@ -10,22 +10,32 @@
|
||||
# RUNNER_TOKEN 仓库 Settings → Actions → Runners 里生成的注册 token
|
||||
# 可选:
|
||||
# RUNNER_NAME runner 名称,默认取 hostname
|
||||
# RUNNER_LABELS 默认 ubuntu-latest,oracle-arm
|
||||
# RUNNER_LABELS 默认 ubuntu-latest
|
||||
# RUNNER_VERSION 默认 5.2.0
|
||||
# RUNNER_ROOT 默认 $HOME/gitea-runner
|
||||
# RUNNER_CAPACITY 默认 1,按 runner 主机资源设置并发数
|
||||
# RUNNER_CONTAINER_OPTIONS 默认 "--cpus=1.5 --memory=4g"
|
||||
# RUNNER_PNPM_STORE 宿主机 pnpm store 目录,默认 $RUNNER_ROOT/pnpm-store
|
||||
# RUNNER_IMAGE 任务容器镜像,默认 node:24-bookworm;本地预构建镜像可传 personal-brand-ci:node24
|
||||
# CACHE_PORT 默认 8193
|
||||
# RUNNER_CACHE_HOST 默认 172.17.0.1(Docker 默认 bridge 网关)
|
||||
# SKIP_LINGER 默认 0;设为 1 时不尝试用 sudo 开启 linger
|
||||
set -euo pipefail
|
||||
|
||||
GITEA_URL=${GITEA_URL:?需要设置 GITEA_URL}
|
||||
RUNNER_TOKEN=${RUNNER_TOKEN:?需要设置 RUNNER_TOKEN}
|
||||
RUNNER_NAME=${RUNNER_NAME:-$(hostname -s)}
|
||||
RUNNER_LABELS=${RUNNER_LABELS:-ubuntu-latest,oracle-arm}
|
||||
RUNNER_LABELS=${RUNNER_LABELS:-ubuntu-latest}
|
||||
RUNNER_VERSION=${RUNNER_VERSION:-5.2.0}
|
||||
RUNNER_ROOT=${RUNNER_ROOT:-$HOME/gitea-runner}
|
||||
RUNNER_CAPACITY=${RUNNER_CAPACITY:-1}
|
||||
RUNNER_CONTAINER_OPTIONS=${RUNNER_CONTAINER_OPTIONS:-"--cpus=1.5 --memory=4g"}
|
||||
RUNNER_PNPM_STORE=${RUNNER_PNPM_STORE:-$RUNNER_ROOT/pnpm-store}
|
||||
RUNNER_IMAGE=${RUNNER_IMAGE:-node:24-bookworm}
|
||||
CACHE_PORT=${CACHE_PORT:-8193}
|
||||
RUNNER_CACHE_HOST=${RUNNER_CACHE_HOST:-172.17.0.1}
|
||||
CACHE_DIR=${CACHE_DIR:-$RUNNER_ROOT/cache-data}
|
||||
SKIP_LINGER=${SKIP_LINGER:-0}
|
||||
DRY_RUN=${DRY_RUN:-0}
|
||||
|
||||
if [ "$DRY_RUN" != "1" ]; then
|
||||
@@ -50,22 +60,33 @@ run() {
|
||||
archive="gitea-runner-${RUNNER_VERSION}-linux-${ARCH}.xz"
|
||||
base="https://dl.gitea.com/gitea-runner/${RUNNER_VERSION}"
|
||||
|
||||
run mkdir -p "$RUNNER_ROOT" "$CACHE_DIR"
|
||||
run mkdir -p "$RUNNER_ROOT" "$CACHE_DIR" "$RUNNER_PNPM_STORE"
|
||||
if [ ! -x "$RUNNER_ROOT/gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" ]; then
|
||||
run curl -fsSL -o "$RUNNER_ROOT/$archive" "$base/$archive"
|
||||
run curl -fsSL -o "$RUNNER_ROOT/$archive.sha256" "$base/$archive.sha256"
|
||||
run env -C "$RUNNER_ROOT" sha256sum -c "$archive.sha256"
|
||||
run xz -d -f "$RUNNER_ROOT/$archive"
|
||||
fi
|
||||
run chmod +x "$RUNNER_ROOT/gitea-runner-${RUNNER_VERSION}-linux-${ARCH}"
|
||||
|
||||
# 把 pnpm store 挂到所有任务容器的固定路径;workflow 通过 npm_config_store_dir 指向它。
|
||||
case " $RUNNER_CONTAINER_OPTIONS " in
|
||||
*"/workspace/.pnpm-store"*) ;;
|
||||
*) RUNNER_CONTAINER_OPTIONS="$RUNNER_CONTAINER_OPTIONS --volume=${RUNNER_PNPM_STORE}:/workspace/.pnpm-store" ;;
|
||||
esac
|
||||
run ln -sf "gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" "$RUNNER_ROOT/gitea-runner"
|
||||
|
||||
label_specs() {
|
||||
printf '%s\n' "$RUNNER_LABELS" | tr ',' '\n' | sed "s|^\(.*\)$|\1:docker://${RUNNER_IMAGE}|"
|
||||
}
|
||||
|
||||
if [ ! -f "$RUNNER_ROOT/.runner" ]; then
|
||||
run env -C "$RUNNER_ROOT" ./gitea-runner register \
|
||||
--no-interactive \
|
||||
--instance "$GITEA_URL" \
|
||||
--token "$RUNNER_TOKEN" \
|
||||
--name "$RUNNER_NAME" \
|
||||
--labels "$RUNNER_LABELS"
|
||||
--labels "$(label_specs | paste -sd, -)"
|
||||
fi
|
||||
if [ -f "$RUNNER_ROOT/.runner" ]; then
|
||||
run chmod 600 "$RUNNER_ROOT/.runner"
|
||||
@@ -76,20 +97,21 @@ log:
|
||||
level: debug
|
||||
runner:
|
||||
file: .runner
|
||||
capacity: 1
|
||||
capacity: ${RUNNER_CAPACITY}
|
||||
timeout: 2h
|
||||
fetch_timeout: 10s
|
||||
fetch_interval: 2s
|
||||
labels:
|
||||
$(printf '%s\n' "$RUNNER_LABELS" | tr ',' '\n' | sed 's|^\(.*\)$| - "\1:docker://node:24-bookworm"|')
|
||||
$(label_specs | sed 's|^| - "|; s|$|"|')
|
||||
cache:
|
||||
enabled: true
|
||||
dir: ${CACHE_DIR}
|
||||
host: ${RUNNER_CACHE_HOST}
|
||||
port: ${CACHE_PORT}
|
||||
container:
|
||||
options: "--cpus=1.5 --memory=4g"
|
||||
valid_volumes: []
|
||||
options: "${RUNNER_CONTAINER_OPTIONS}"
|
||||
valid_volumes:
|
||||
- ${RUNNER_PNPM_STORE}
|
||||
docker_host: "-"
|
||||
CONFIG
|
||||
|
||||
@@ -111,8 +133,11 @@ WantedBy=default.target
|
||||
UNIT
|
||||
|
||||
# 用户级 systemd 服务默认在最后一个会话退出后停止;开启 linger 才能在重启后自动拉起 runner。
|
||||
if command -v loginctl >/dev/null 2>&1; then
|
||||
run sudo loginctl enable-linger "$USER"
|
||||
if [ "$SKIP_LINGER" != "1" ] && command -v loginctl >/dev/null 2>&1; then
|
||||
linger=$(loginctl show-user "$USER" -p Linger --value 2>/dev/null || true)
|
||||
if [ "$linger" != "yes" ]; then
|
||||
run sudo loginctl enable-linger "$USER"
|
||||
fi
|
||||
fi
|
||||
run systemctl --user daemon-reload
|
||||
run systemctl --user enable --now gitea-runner.service
|
||||
|
||||
+22
-13
@@ -1,22 +1,23 @@
|
||||
# CI(Gitea Actions)
|
||||
|
||||
远端仓库是 Gitea,CI 由 `.gitea/workflows/ci.yml` 定义,运行在 `oracle_arm` 主机上的 self-hosted runner。
|
||||
远端仓库是 Gitea,CI 由 `.gitea/workflows/ci.yml` 定义,运行在本地工作站 `Elitebook` 上的 self-hosted runner(x86_64、Docker、双并发)。旧的 `oracle_arm` runner 已停止并禁用,避免两个 runner 争抢 `ubuntu-latest` 标签。
|
||||
|
||||
## Runner
|
||||
|
||||
安装与运维步骤见 `docs/runner-setup.md`(`deploy/runner/install-runner.sh` 可复现整套配置)。
|
||||
|
||||
- 程序:`gitea-runner` v5.2.0(linux/arm64),位于 `/home/ubuntu/gitea-runner`
|
||||
- 程序:`gitea-runner` v5.2.0(linux/amd64),位于 `~/gitea-runner`
|
||||
- 服务:systemd user unit `gitea-runner.service`
|
||||
- 标签:`ubuntu-latest`、`oracle-arm`
|
||||
- 执行模式:Docker,单并发,任务容器限制 `--cpus=1.5 --memory=4g`
|
||||
- 标签:`ubuntu-latest`、`local-x64`
|
||||
- 执行模式:Docker,双并发,任务容器限制 `--cpus=4 --memory=8g`
|
||||
- 任务镜像:`personal-brand-ci:node24`(预装 pnpm 与 Playwright 依赖,见 `deploy/runner/Dockerfile`)
|
||||
- 作用域:仅 `que01/persionalBrand` 仓库
|
||||
|
||||
查看状态:
|
||||
|
||||
```bash
|
||||
ssh oracle_arm 'systemctl --user status gitea-runner.service'
|
||||
ssh oracle_arm 'journalctl --user -u gitea-runner -n 100 --no-pager'
|
||||
systemctl --user status gitea-runner.service
|
||||
journalctl --user -u gitea-runner -n 100 --no-pager
|
||||
```
|
||||
|
||||
## 当前门禁
|
||||
@@ -26,17 +27,25 @@ ssh oracle_arm 'journalctl --user -u gitea-runner -n 100 --no-pager'
|
||||
- `api`:PostgreSQL service container 上的 API integration
|
||||
- `browser-smoke`:PR 和手动触发时执行会话恢复、创建品牌与页面方案 smoke;只在 PR 和 `workflow_dispatch` 运行,main 合并后不重复执行
|
||||
|
||||
完整浏览器回归仍作为发布前本地门禁执行,避免在 2 核生产主机上运行完整 Playwright 套件抢占资源。PR smoke 只跑两条关键流程,并使用已构建的 Admin 静态产物与 mocked API。
|
||||
完整浏览器回归仍作为发布前本地门禁执行,避免在共享开发机上运行过重的 Playwright 套件。PR smoke 只跑两条关键流程,并使用已构建的 Admin 静态产物与 mocked API。
|
||||
|
||||
## 注意
|
||||
|
||||
Runner 与生产 API 共用 `oracle_arm` 主机。任务容器已限制 CPU 与内存;如果 CI 期间生产延迟明显,应暂停 runner 或迁移到独立主机。
|
||||
Runner 与本地开发工作共用 `Elitebook` 主机,不再与生产 API 共用资源。任务容器限制 4 CPU / 8 GB、并发 2;本地机器关机或休眠时 CI 会排队,required checks 不会自动完成。
|
||||
|
||||
## 工具链与外部下载
|
||||
|
||||
任务镜像 `personal-brand-ci:node24` 已内置 Node 24 和 pnpm 9.15.0;workflow 不再调用 `actions/setup-node` 或 `pnpm/action-setup`,而是在每个 job 开头执行 `Verify preinstalled toolchain`。这样不会因为 nodejs.org 下载缓慢或中断而把 job 卡到超时。
|
||||
|
||||
Prisma engines 在构建镜像时预热到 `/root/.cache/prisma`,job 内 `pnpm install` 触发的 `prisma generate` 直接命中缓存,不再访问 `binaries.prisma.sh`。workflow 仍用 `actions/cache` 缓存该目录,作为更换镜像或镜像未预热时的兜底。升级 Prisma 后必须同步 `deploy/runner/Dockerfile` 的 `PRISMA_VERSION` 并重建镜像。
|
||||
|
||||
## 缓存
|
||||
|
||||
Runner 内置 cache server(`cache.host=172.17.0.1`、`cache.port=8193`、`cache.dir=/home/ubuntu/gitea-runner/cache-data`),供 `actions/setup-node` 的 pnpm 缓存使用。
|
||||
Runner 内置 cache server(`cache.host=172.17.0.1`、`cache.port=8193`、`cache.dir=~/gitea-runner/cache-data`),供 `actions/cache` 使用(Turbo 缓存等)。
|
||||
|
||||
任务容器与主机之间被 `ufw` 的 `INPUT DROP` 默认策略拦截,需要显式放行 Docker 使用的三个私网网段:
|
||||
pnpm 依赖不再走 actions/cache:宿主机 `~/gitea-runner/pnpm-store` 被 bind mount 到任务容器的 `/workspace/.pnpm-store`,workflow 的 `npm_config_store_dir` 指向该路径。同一轮 CI 里的 static、build、api、browser-smoke 共享一份 store,`pnpm install` 只做链接/校验,不再重复下载 1600+ 个包。
|
||||
|
||||
如果 runner 主机启用了 `ufw`,任务容器到 cache server 的流量会被 `INPUT DROP` 拦截,需要显式放行 Docker 使用的三个私网网段;本地工作站未启用 `ufw`,无需配置:
|
||||
|
||||
```bash
|
||||
sudo ufw allow from 172.16.0.0/12 to any port 8193 proto tcp comment "gitea actions cache"
|
||||
@@ -56,7 +65,7 @@ docker exec <job-container> node -e 'fetch("http://172.17.0.1:8193/").then(r=>co
|
||||
|
||||
Runner 日志级别保持 `debug`:`info` 级别不会记录各 step 的 `Success - Main ...` 与告警,排障时只能依赖 Gitea 页面。
|
||||
|
||||
## 首次完整通过
|
||||
## 迁移前基线(`oracle_arm`)
|
||||
|
||||
| 项 | 值 |
|
||||
| ---------------- | ----------------------------- |
|
||||
@@ -78,11 +87,11 @@ Runner 日志级别保持 `debug`:`info` 级别不会记录各 step 的 `Succe
|
||||
| Production build | 11.7 min | 7.6 min |
|
||||
| API integration | 9.7 min | 3.8 min |
|
||||
|
||||
缓存内容为 pnpm store,首次落盘约 268 MB,位于 `/home/ubuntu/gitea-runner/cache-data`。
|
||||
缓存内容为 pnpm store,首次落盘约 1.1 GB,位于 `~/gitea-runner/pnpm-store`(共享 bind mount)。
|
||||
|
||||
## 触发范围
|
||||
|
||||
- `push` 到 `main` 时忽略纯文档改动(`**.md`),避免 2 核主机为文档提交跑完整 CI。
|
||||
- `push` 到 `main` 时忽略纯文档改动(`**.md`),避免为文档提交跑完整 CI。
|
||||
- `pull_request` 不设置 `paths-ignore`。branch protection 要求的 status check 必须每次都上报,否则纯文档 PR 会一直停在「等待检查」而无法合并。
|
||||
- 需要给纯文档提交补一次状态时,用 `workflow_dispatch` 手动触发。
|
||||
|
||||
|
||||
+26
-2
@@ -30,9 +30,33 @@ DEPLOY_TARGET=oracle_arm PUBLIC_BASE_URL=https://wechat.app.que01.top pnpm deplo
|
||||
pnpm deploy:status
|
||||
```
|
||||
|
||||
数据库备份自动保留最近 7 份;备份归档在状态检查中通过 `pg_restore -l` 校验可读性。备份失败会清理未完成的临时文件,不自动删除数据库数据。发布成功后保留当前版本、上一版本和最近 5 个发布目录。状态检查同时报告 PM2 自启、磁盘空间、备份时间/可读性及发布目录数量。
|
||||
数据库备份分两类:
|
||||
|
||||
备份当前仍在同一台 VPS 上,不等同于灾难恢复副本。应将 `shared/backups/*.dump` 定期复制到独立主机或对象存储,并至少每季度在隔离数据库执行一次恢复演练;不得在生产数据库上测试恢复。整机重启演练需在维护窗口执行,确认 Admin、API、PostgreSQL 和公网路由恢复后再结束维护。
|
||||
- 发布备份:每次 `pnpm deploy:release` 在 migration 前创建,按版本名保留最近 7 份。
|
||||
- 定时备份:由 user-level `personal-brand-backup.timer` 每天 03:30(VPS 本地时区)执行,写入 `shared/backups/scheduled-*.dump`,默认保留 14 天。
|
||||
|
||||
定时任务的脚本副本位于 `shared/backup-db.mjs` 和 `shared/database-backup.mjs`,不依赖当前 release 目录,回滚后仍可运行。每次备份先用 `pg_dump -Fc` 写入临时文件,再用 `pg_restore -l` 校验;校验失败会删除损坏文件并让本次任务失败。备份失败会清理未完成的临时文件,不自动删除数据库数据。发布成功后保留当前版本、上一版本和最近 5 个发布目录。
|
||||
|
||||
状态检查会验证备份定时器处于 active、最新备份不超过 36 小时(可用 `BACKUP_MAX_AGE_HOURS` 调整),并通过 `pg_restore -l` 校验可读性。状态检查同时报告 PM2 自启、磁盘空间、备份时间和发布目录数量。
|
||||
|
||||
如需异地副本,在 `shared/.env.production` 配置 `BACKUP_OFFSITE_TARGET`(`rsync` 目标,例如 `backup@example-host:/srv/personal-brand-backups/`),并为 `ubuntu` 用户预先配置 SSH key。未配置时备份只保留在同一台 VPS 上,不等同于灾难恢复副本。至少每季度应在隔离数据库执行一次恢复演练,不得在生产数据库上测试恢复;整机重启演练需在维护窗口执行,确认 Admin、API、PostgreSQL 和公网路由恢复后再结束维护。
|
||||
|
||||
## 微信登录配置
|
||||
|
||||
后台「小程序上传配置」中的 AppID 和上传私钥只用于构建、上传小程序代码,不等于访客登录配置。体验版或正式版调用 `wx.login` 后,API 还需要以下两个环境变量才能通过微信 `jscode2session` 换取 OpenID:
|
||||
|
||||
```bash
|
||||
WECHAT_APP_ID=wxd75d946d6978d183
|
||||
WECHAT_APP_SECRET=<小程序 AppSecret>
|
||||
```
|
||||
|
||||
两项写入 `shared/.env.production`,不要提交到仓库。`WECHAT_APP_ID` 必须与后台上传配置中的 AppID 一致;不一致时微信会返回 `40029` 或 `40163`。修改后重启 API:
|
||||
|
||||
```bash
|
||||
pm2 restart personal-brand-api
|
||||
```
|
||||
|
||||
API 通过 `node --env-file=shared/.env.production` 启动,重启后会读取新值。`pnpm deploy:status` 会检查两项是否非空;缺失时状态检查失败。
|
||||
|
||||
## Nginx 反向代理
|
||||
|
||||
|
||||
+43
-11
@@ -1,11 +1,11 @@
|
||||
# CI Runner 安装与运维
|
||||
|
||||
CI 跑在自托管 Gitea Actions runner 上(当前主机 `oracle_arm`,与生产 API 共用 2 核)。本文件记录可复现的安装步骤,避免换机器时重新摸索。
|
||||
CI 跑在自托管 Gitea Actions runner 上。当前使用本地工作站 `Elitebook`(x86_64、12 核、30 GB,Docker 容器执行),旧的 `oracle_arm` runner 已停止并禁用。本文件记录可复现的安装步骤,避免换机器时重新摸索。
|
||||
|
||||
## 一次性准备
|
||||
|
||||
1. 在 Gitea 仓库 Settings → Actions → Runners 里创建注册 token(只对单个仓库或整个实例生效)。
|
||||
2. 目标主机需要:Docker、`curl`、`xz`、`ufw`、systemd、可用 sudo、Node 不需要(任务容器自带)。
|
||||
2. 目标主机需要:Docker、`curl`、`xz`、systemd;Node 不需要(任务容器自带)。`ufw` 和 `sudo` 仅在需要配置防火墙或 linger 时使用。
|
||||
|
||||
## 安装
|
||||
|
||||
@@ -14,8 +14,13 @@ CI 跑在自托管 Gitea Actions runner 上(当前主机 `oracle_arm`,与生
|
||||
```bash
|
||||
GITEA_URL=https://git.example.com \
|
||||
RUNNER_TOKEN=<注册 token> \
|
||||
RUNNER_NAME=oracle-arm \
|
||||
RUNNER_LABELS=ubuntu-latest,oracle-arm \
|
||||
RUNNER_NAME=elitebook-local \
|
||||
RUNNER_LABELS=ubuntu-latest,local-x64 \
|
||||
RUNNER_CAPACITY=2 \
|
||||
RUNNER_CONTAINER_OPTIONS='--cpus=4 --memory=8g' \
|
||||
RUNNER_IMAGE=personal-brand-ci:node24 \
|
||||
RUNNER_PNPM_STORE=$HOME/gitea-runner/pnpm-store \
|
||||
SKIP_LINGER=1 \
|
||||
./install-runner.sh
|
||||
```
|
||||
|
||||
@@ -25,9 +30,32 @@ RUNNER_LABELS=ubuntu-latest,oracle-arm \
|
||||
|
||||
1. 按架构下载 `gitea-runner-<version>-linux-<arch>`,校验 `.sha256` 后解压到 `~/gitea-runner`。
|
||||
2. 用 `--no-interactive` 注册,写出 `.runner`(已存在则跳过,可重复执行)。
|
||||
3. 生成 `config.yaml`:单并发、`docker://node:24-bookworm` 标签、内置 cache server 监听 `172.17.0.1:8193`、任务容器限制 `--cpus=1.5 --memory=4g`。
|
||||
4. 装 systemd user unit `gitea-runner.service`,执行 `loginctl enable-linger` 后 `enable --now`。
|
||||
5. 放行任务容器到 cache 端口的 ufw 规则。
|
||||
3. 生成 `config.yaml`:按 `RUNNER_CAPACITY` 设置并发,使用 `RUNNER_IMAGE`(默认 `node:24-bookworm`)作为任务容器镜像,内置 cache server 监听 `172.17.0.1:8193`,并按 `RUNNER_CONTAINER_OPTIONS` 限制任务容器资源。
|
||||
4. 把 `RUNNER_PNPM_STORE`(默认 `~/gitea-runner/pnpm-store`)bind mount 到任务容器的 `/workspace/.pnpm-store`;workflow 通过 `npm_config_store_dir` 指向同一路径,所有 job 复用这份 pnpm store,避免重复下载依赖。
|
||||
5. 为下载的 runner 二进制补上可执行权限,避免归档解压后出现 `Permission denied`。
|
||||
6. 装 systemd user unit `gitea-runner.service`,执行 `loginctl enable-linger` 后 `enable --now`。
|
||||
7. 放行任务容器到 cache 端口的 ufw 规则。
|
||||
|
||||
## 专用 CI 镜像
|
||||
|
||||
`browser-smoke` 需要 Chromium 及其系统库。为避免每个 job 重复执行 `playwright install --with-deps`,本机使用预构建镜像:
|
||||
|
||||
```bash
|
||||
docker build -f deploy/runner/Dockerfile -t personal-brand-ci:node24 .
|
||||
```
|
||||
|
||||
镜像包含 Node 24、pnpm 9.15.0、git/curl/jq/unzip,以及 Playwright 1.58.2 的 Chromium、headless shell、FFmpeg 和系统依赖;构建时还会预热 Prisma engines 到 `/root/.cache/prisma`。构建产物是本机 amd64 镜像(约 3 GB),不推送到 registry。
|
||||
|
||||
workflow 不再使用 `actions/setup-node` 或 `pnpm/action-setup`,而是直接校验镜像内置的 Node/pnpm 版本。因此替换 `RUNNER_IMAGE` 时必须保证镜像内已有 Node 24 和 pnpm 9.15.0,否则 job 会在 `Verify preinstalled toolchain` 步骤失败。
|
||||
|
||||
`apps/taro/package.json` 升级 Playwright 后,同步修改 `deploy/runner/Dockerfile` 的 `PLAYWRIGHT_VERSION`;升级 Prisma 后,同步修改 `PRISMA_VERSION`。然后重建镜像并重启 runner:
|
||||
|
||||
```bash
|
||||
docker build -f deploy/runner/Dockerfile -t personal-brand-ci:node24 .
|
||||
systemctl --user restart gitea-runner.service
|
||||
```
|
||||
|
||||
镜像只在当前主机存在;换 runner 主机时需要重新构建。若在 ARM 主机构建,镜像会按该主机架构原生构建,不需要额外参数。
|
||||
|
||||
## 为什么 ufw 要放行三个网段
|
||||
|
||||
@@ -38,10 +66,12 @@ RUNNER_LABELS=ubuntu-latest,oracle-arm \
|
||||
```bash
|
||||
systemctl --user status gitea-runner.service
|
||||
journalctl --user -u gitea-runner -n 100 --no-pager
|
||||
du -sh ~/gitea-runner/cache-data
|
||||
du -sh ~/gitea-runner/cache-data ~/gitea-runner/pnpm-store
|
||||
```
|
||||
|
||||
仓库侧 `pnpm deploy:status` 也会打印 `ci-runner=` 一行。
|
||||
`pnpm-store` 会持续增长。确认没有 job 运行时,可以执行 `pnpm store prune` 清理未被引用的包(在任意任务容器里对 `/workspace/.pnpm-store` 执行,或直接删除旧目录后让下一次 job 重新填充)。
|
||||
|
||||
`pnpm deploy:status` 只检查 VPS 服务、备份和公网路由,不再把 CI runner 当作生产健康条件。runner 已迁到本地工作站,脚本会额外打印非阻断的 `ci-runner-local=`,也可以直接用上面的 `systemctl --user status gitea-runner.service` 检查。
|
||||
|
||||
## 排障
|
||||
|
||||
@@ -50,11 +80,13 @@ du -sh ~/gitea-runner/cache-data
|
||||
| push 后 CI 一直 queued 不启动 | runner 服务是否 active;Gitea 仓库 Runners 页面是否显示 online |
|
||||
| 日志出现 `Failed to GetCacheEntryDownloadURL ... ETIMEDOUT` | ufw 是否放行三个私网段;`docker exec <job-container> node -e 'fetch("http://172.17.0.1:8193/")'` 应返回 404 |
|
||||
| 一轮 CI 突然变慢 | 缓存是否命中;宿主机负载与磁盘余量 |
|
||||
| 生产 API 变慢 | CI 与生产共用主机,检查是否在跑 build/api job;必要时暂停 runner |
|
||||
| 本机开发明显变慢 | 是否同时运行 build/api job;用 `docker stats` 检查任务容器资源 |
|
||||
| 重启后 runner 没起来 | `loginctl show-user "$USER" -p Linger` 应为 `Linger=yes`;否则执行 `sudo loginctl enable-linger "$USER"` |
|
||||
|
||||
## 约束
|
||||
|
||||
- `capacity: 1`:不要提高并发,2 核主机上并发只会让生产和 CI 一起变慢。
|
||||
- 本地 runner 使用 `capacity: 2`,每个任务容器限制 `4 CPU / 8 GB`;按宿主资源调整,不要超过实际可用内存。
|
||||
- `~/gitea-runner/pnpm-store` 是所有 job 共享的 pnpm store。不要删除正在被 job 使用的 store;换 runner 主机时整目录拷贝或重新预热即可。
|
||||
- 本地 runner 关机或休眠时,required checks 会保持 queued。需要开机自启时执行 `sudo loginctl enable-linger "$USER"`,否则用户会话退出后服务会停止。
|
||||
- 日志级别保持 `debug`:`info` 级别不记录各 step 的结果行,排障时等于没有日志。
|
||||
- 完整浏览器回归不进入定时或 main 推送 CI;PR 只运行轻量 browser smoke(见 `docs/ci.md`),完整套件仍在发布前执行。
|
||||
@@ -8,6 +8,8 @@
|
||||
>
|
||||
> 架构决策:ADR 0029:模板管理、导入与分发边界
|
||||
|
||||
> 状态校正(2026-10-23):当前 `pnpm template:doctor` 对 `acheng-campus-food`、`atelier-solopreneur`、`linjing-editorial` 均返回 `staticStatus=pass`、`completionStatus=complete`、`unresolvedIssues=0`。下方 2026-10-17/18 记录中的 `unverified` 是历史证据,不代表当前状态。该报告只证明自动化证据齐全,不替代 Phase 8 的真实品牌操作、微信外部验证和运营审阅。
|
||||
|
||||
本文档把模板从本地开发、导出、线上导入、校验、预览、安装到小程序发布的完整工作固化为一个可追踪任务。后续代码、迁移、接口、后台页面、测试证据和已知缺口都必须回到本文登记。本文描述的是目标和实施门禁;没有对应代码、测试和运行证据的条目不能标记为完成。
|
||||
|
||||
## 1. 背景和目标
|
||||
|
||||
@@ -75,4 +75,4 @@ Admin 直接复用 Taro renderer,模板原始 CSS 按 750 设计宽度转换
|
||||
|
||||
## 本轮边界
|
||||
|
||||
本次先制定计划,功能实施尚未开始。聚焦内容编辑,不做任意拖拽布局或 HTML 入库。保留模板视觉和 H5 回退;不重置数据库或 seed,必要结构调整先制定兼容迁移。
|
||||
截至 2026-10-23,`.codex-tasks/visual-editor-closure/SUBTASKS.csv` 中的 5 个子任务均已标记 `DONE`;本文保留为实现范围、验收标准和后续回归依据,当前状态以该 CSV 和实际验证记录为准。本轮仍聚焦内容编辑,不做任意拖拽布局或 HTML 入库。保留模板视觉和 H5 回退;不重置数据库或 seed,必要结构调整先制定兼容迁移。
|
||||
+68
-36
@@ -1,9 +1,10 @@
|
||||
import path from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
|
||||
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
||||
const target = process.env.DEPLOY_TARGET ?? "oracle_arm";
|
||||
const deployRoot = process.env.DEPLOY_ROOT ?? "/home/ubuntu/persional-brand";
|
||||
const publicBaseUrl = (process.env.PUBLIC_BASE_URL ?? "https://wechat.app.que01.top").replace(
|
||||
/\/$/,
|
||||
"",
|
||||
@@ -21,41 +22,72 @@ function shellQuote(value) {
|
||||
return "'" + value.replace(/'/g, "'\\''") + "'";
|
||||
}
|
||||
|
||||
const remoteScript = [
|
||||
"set -euo pipefail",
|
||||
'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 "pm2-autostart="; systemctl --user is-enabled personal-brand-pm2.service',
|
||||
'printf "admin="; systemctl --user is-active personal-brand-admin-static.service',
|
||||
'printf "database="; docker inspect -f "{{.State.Status}}" personal-brand-postgres-1',
|
||||
'printf "ci-runner="; systemctl --user is-active gitea-runner.service',
|
||||
'printf "backups="; find /home/ubuntu/persional-brand/shared/backups -maxdepth 1 -name "*.dump" | wc -l',
|
||||
'printf "latest-backup="; newest=$(find /home/ubuntu/persional-brand/shared/backups -maxdepth 1 -type f -name "*.dump" -printf "%T@ %p\\n" | sort -nr | head -1 | cut -d" " -f2-); test -n "$newest"; stat -c "%y %s bytes" "$newest"; docker exec -i personal-brand-postgres-1 pg_restore -l < "$newest" >/dev/null; echo readable',
|
||||
'printf "backup-temp-files="; find /home/ubuntu/persional-brand/shared/backups -maxdepth 1 -type f -name "*.tmp" | wc -l',
|
||||
'printf "disk="; df -h / | awk \'NR==2 {print $5 " used, " $4 " free"}\'',
|
||||
'printf "release-count="; find /home/ubuntu/persional-brand/releases -mindepth 1 -maxdepth 1 -type d | wc -l',
|
||||
].join("; ");
|
||||
/**
|
||||
* 生产状态检查只覆盖 VPS 上的服务和公网路由。
|
||||
* CI runner 已迁到本地工作站,不能再把远端 gitea-runner.service 当成生产健康条件。
|
||||
*/
|
||||
export function buildRemoteStatusScript({ deployRoot: rootPath = deployRoot } = {}) {
|
||||
if (!/^\/[a-zA-Z0-9_./-]+$/.test(rootPath)) throw new Error("Invalid deployment root");
|
||||
|
||||
run("ssh", [target, "bash -lc " + shellQuote(remoteScript)]);
|
||||
return [
|
||||
"set -euo pipefail",
|
||||
'source "$HOME/.nvm/nvm.sh"',
|
||||
`export DEPLOY_ROOT=${shellQuote(rootPath)}`,
|
||||
'printf "current=%s\\n" "$(readlink "$DEPLOY_ROOT/current")"',
|
||||
'printf "api="; test "$(pm2 pid personal-brand-api)" != "0"; echo online',
|
||||
'printf "pm2-autostart="; systemctl --user is-enabled personal-brand-pm2.service',
|
||||
'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 "$DEPLOY_ROOT/shared/backups" -maxdepth 1 -name "*.dump" | wc -l',
|
||||
'printf "latest-backup="; newest=$(find "$DEPLOY_ROOT/shared/backups" -maxdepth 1 -type f -name "*.dump" -printf "%T@ %p\\n" | sort -nr | head -1 | cut -d" " -f2-); test -n "$newest"; stat -c "%y %s bytes" "$newest"; docker exec -i personal-brand-postgres-1 pg_restore -l < "$newest" >/dev/null; echo readable',
|
||||
'printf "backup-timer="; systemctl --user is-active personal-brand-backup.timer',
|
||||
'printf "backup-age-hours="; newest=$(find "$DEPLOY_ROOT/shared/backups" -maxdepth 1 -type f -name "*.dump" -printf "%T@ %p\\n" | sort -nr | head -1 | cut -d" " -f2-); age=$(( ( $(date +%s) - $(stat -c %Y "$newest") ) / 3600 )); echo "$age"; test "$age" -le "${BACKUP_MAX_AGE_HOURS:-36}"',
|
||||
'printf "backup-temp-files="; find "$DEPLOY_ROOT/shared/backups" -maxdepth 1 -type f -name "*.tmp" | wc -l',
|
||||
'printf "wechat-login="; if grep -q "^WECHAT_APP_ID=." "$DEPLOY_ROOT/shared/.env.production" && grep -q "^WECHAT_APP_SECRET=." "$DEPLOY_ROOT/shared/.env.production"; then echo configured; else echo missing; exit 1; fi',
|
||||
'printf "disk="; df -h / | awk \'NR==2 {print $5 " used, " $4 " free"}\'',
|
||||
'printf "release-count="; find "$DEPLOY_ROOT/releases" -mindepth 1 -maxdepth 1 -type d | 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,
|
||||
]);
|
||||
/**
|
||||
* CI runner 与生产状态是两件事。这里只做本地提示,runner 不活跃不应阻断部署状态检查。
|
||||
*/
|
||||
function printLocalRunnerStatus() {
|
||||
const result = spawnSync("systemctl", ["--user", "is-active", "gitea-runner.service"], {
|
||||
encoding: "utf8",
|
||||
});
|
||||
const state = (result.stdout || "").trim();
|
||||
process.stdout.write(`ci-runner-local=${state || "unknown"}\n`);
|
||||
}
|
||||
|
||||
function main() {
|
||||
if (!/^[a-zA-Z0-9_.@:-]+$/.test(target)) throw new Error("Invalid deployment SSH target");
|
||||
if (!/^\/[a-zA-Z0-9_./-]+$/.test(deployRoot)) throw new Error("Invalid deployment root");
|
||||
|
||||
run("ssh", [target, "bash -lc " + shellQuote(buildRemoteStatusScript())]);
|
||||
printLocalRunnerStatus();
|
||||
|
||||
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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
main();
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import test from "node:test";
|
||||
import { buildRemoteStatusScript } from "./status.mjs";
|
||||
|
||||
test("生产状态脚本不再要求 VPS 上运行 CI runner", () => {
|
||||
const script = buildRemoteStatusScript();
|
||||
|
||||
assert.doesNotMatch(script, /gitea-runner/);
|
||||
assert.match(script, /pm2 pid personal-brand-api/);
|
||||
assert.match(script, /pg_restore -l/);
|
||||
assert.match(script, /personal-brand-backup\.timer/);
|
||||
assert.match(script, /backup-age-hours/);
|
||||
assert.match(script, /BACKUP_MAX_AGE_HOURS/);
|
||||
assert.match(script, /wechat-login/);
|
||||
assert.match(script, /WECHAT_APP_SECRET/);
|
||||
});
|
||||
|
||||
test("生产状态脚本语法有效", () => {
|
||||
const result = spawnSync("bash", ["-n"], {
|
||||
input: buildRemoteStatusScript(),
|
||||
encoding: "utf8",
|
||||
});
|
||||
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
});
|
||||
|
||||
test("生产状态脚本使用调用方指定的部署目录", () => {
|
||||
const script = buildRemoteStatusScript({ deployRoot: "/srv/personal-brand" });
|
||||
|
||||
assert.match(script, /DEPLOY_ROOT='\/srv\/personal-brand'/);
|
||||
assert.match(script, /"\$DEPLOY_ROOT\/current"/);
|
||||
});
|
||||
|
||||
test("非法部署目录会被拒绝", () => {
|
||||
assert.throws(() => buildRemoteStatusScript({ deployRoot: "/srv/personal brand" }), /Invalid/);
|
||||
});
|
||||
Reference in new issue
Block a user