diff --git a/docs/deployment.md b/docs/deployment.md index bb58fee..a313f60 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -103,3 +103,15 @@ curl -fsS "$API_BASE_URL/health" ```bash curl -fsS "$API_BASE_URL/public/workspace?brandSlug=$BRAND_SLUG" ``` + +上述四步验收可以用统一脚本执行: + +```bash +API_BASE_URL=https://api.example.com \ +BRAND_SLUG=$BRAND_SLUG \ +ADMIN_USERNAME=$ADMIN_USERNAME \ +ADMIN_PASSWORD=$ADMIN_PASSWORD \ +pnpm deploy:verify +``` + +脚本要求账号已完成初始密码修改,品牌发布检查全部通过,公开工作区已发布且至少有一项业务。凭据只从环境变量读取,不能写入仓库。 diff --git a/package.json b/package.json index 8b305f4..a96d024 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,8 @@ "preview:food": "pnpm --filter @personal-brand/taro dev:h5", "preview:linjing": "TEMPLATE_KEY=linjing-editorial PREVIEW_PORT=3003 pnpm --filter @personal-brand/taro dev:h5", "preview:templates": "pnpm --filter @personal-brand/taro dev:preview", + "deploy:verify": "node scripts/deploy/verify-release.mjs", + "test:deploy-verify": "node --test scripts/deploy/verify-release.test.mjs", "build:admin:preview": "PREVIEW_ALL_TEMPLATES=true API_BASE_URL=/api PREVIEW_PUBLIC_PATH=/template-preview/ pnpm --filter @personal-brand/taro build:preview && node scripts/package-template-preview.cjs && VITE_APP_BASE_PATH=/admin/ VITE_API_BASE_URL=/api pnpm --filter @personal-brand/admin build", "build:taro:food": "pnpm --filter @personal-brand/taro build:h5 && pnpm --filter @personal-brand/taro build:weapp", "build:taro:linjing": "TEMPLATE_KEY=linjing-editorial pnpm --filter @personal-brand/taro build:h5 && TEMPLATE_KEY=linjing-editorial pnpm --filter @personal-brand/taro build:weapp", diff --git a/scripts/deploy/verify-release.mjs b/scripts/deploy/verify-release.mjs new file mode 100644 index 0000000..b07f027 --- /dev/null +++ b/scripts/deploy/verify-release.mjs @@ -0,0 +1,59 @@ +import assert from "node:assert/strict"; + +function requiredEnvironment(name) { + const value = process.env[name]; + if (!value) throw new Error(`${name} is required`); + return value; +} + +async function requestJson(url, options = {}) { + const response = await fetch(url, options); + const body = await response.json().catch(() => null); + assert.ok( + response.ok && body, + `${options.method ?? "GET"} ${url} failed with status ${response.status}`, + ); + return body; +} + +const apiBaseUrl = requiredEnvironment("API_BASE_URL").replace(/\/$/, ""); +const brandSlug = requiredEnvironment("BRAND_SLUG"); +const username = requiredEnvironment("ADMIN_USERNAME"); +const password = requiredEnvironment("ADMIN_PASSWORD"); + +const health = await requestJson(`${apiBaseUrl}/health`); +assert.equal(health.status, "ok"); +console.log(`health: ok (${health.service})`); + +const session = await requestJson(`${apiBaseUrl}/admin/session/login`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ username, password }), +}); +assert.equal(session.mustChangePassword, false, "admin must change the initial password"); +const token = session.token ?? session.accessToken; +assert.equal(typeof token, "string", "login response does not contain a token"); +console.log("admin login: ok"); + +const readiness = await requestJson(`${apiBaseUrl}/admin/releases/readiness`, { + headers: { authorization: `Bearer ${token}` }, +}); +const missingChecks = readiness.checks.filter((check) => !check.passed); +assert.deepEqual( + missingChecks.map((check) => check.label), + [], + `release readiness failed: ${missingChecks.map((check) => check.label).join(", ")}`, +); +console.log(`release readiness: ok (${readiness.checks.length} checks)`); + +const workspace = await requestJson( + `${apiBaseUrl}/public/workspace?brandSlug=${encodeURIComponent(brandSlug)}`, +); +assert.equal(workspace.publication?.status, "published", "brand is not published"); +assert.ok( + Array.isArray(workspace.offerings) && workspace.offerings.length > 0, + "published workspace has no offerings", +); +console.log( + `published workspace: ok (${brandSlug}, v${workspace.publication.version}, ${workspace.offerings.length} offerings)`, +); diff --git a/scripts/deploy/verify-release.test.mjs b/scripts/deploy/verify-release.test.mjs new file mode 100644 index 0000000..d26b9d6 --- /dev/null +++ b/scripts/deploy/verify-release.test.mjs @@ -0,0 +1,52 @@ +import test from "node:test"; +import { spawn } from "node:child_process"; +import { once } from "node:events"; +import { createServer } from "node:http"; + +function runScript(environment) { + const child = spawn(process.execPath, ["scripts/deploy/verify-release.mjs"], { + env: { ...process.env, ...environment }, + stdio: ["ignore", "pipe", "pipe"], + }); + return once(child, "close").then(([code]) => code); +} + +test("release verifier requires deployment variables", async () => { + assertEqualExit(await runScript({}), 1); +}); + +test("release verifier rejects an unpublished brand", async () => { + const server = createServer((request, response) => { + if (request.url === "/health") { + response.writeHead(200, { "content-type": "application/json" }); + response.end(JSON.stringify({ status: "ok", service: "fixture" })); + return; + } + if (request.url === "/admin/session/login") { + response.writeHead(200, { "content-type": "application/json" }); + response.end(JSON.stringify({ token: "fixture-token", mustChangePassword: false })); + return; + } + if (request.url === "/admin/releases/readiness") { + response.writeHead(200, { "content-type": "application/json" }); + response.end(JSON.stringify({ checks: [{ key: "brand", passed: true }] })); + return; + } + response.writeHead(200, { "content-type": "application/json" }); + response.end(JSON.stringify({ publication: { status: "unpublished" } })); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const { port } = server.address(); + const exitCode = await runScript({ + API_BASE_URL: `http://127.0.0.1:${port}`, + BRAND_SLUG: "fixture", + ADMIN_USERNAME: "admin", + ADMIN_PASSWORD: "fixture-password", + }); + server.close(); + assertEqualExit(exitCode, 1); +}); + +function assertEqualExit(actual, expected) { + if (actual !== expected) throw new Error(`expected exit ${expected}, received ${actual}`); +}