chore: 完善生产发布与部署校验
This commit is contained in:
1 parent
048c42207c
commit
5c6ad02f46
18 files changed
+1013
-47
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { readdir, readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
async function listFiles(directory, prefix = "") {
|
||||
const files = [];
|
||||
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
||||
const relative = path.posix.join(prefix, entry.name);
|
||||
if (entry.isDirectory())
|
||||
files.push(...(await listFiles(path.join(directory, entry.name), relative)));
|
||||
else if (entry.isFile()) files.push(relative);
|
||||
else throw new Error("Unsupported file: " + relative);
|
||||
}
|
||||
return files.sort();
|
||||
}
|
||||
|
||||
export async function checkPackage(directory) {
|
||||
const manifest = JSON.parse(await readFile(path.join(directory, "manifest.json"), "utf8"));
|
||||
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,100}$/.test(manifest.version))
|
||||
throw new Error("Invalid release version");
|
||||
const actual = await listFiles(directory);
|
||||
const expected = manifest.files
|
||||
.map((file) => file.path)
|
||||
.concat("manifest.json")
|
||||
.sort();
|
||||
if (JSON.stringify(actual) !== JSON.stringify(expected))
|
||||
throw new Error("Release file list mismatch");
|
||||
for (const file of manifest.files) {
|
||||
if (file.path.startsWith("/") || file.path.split("/").includes(".."))
|
||||
throw new Error("Unsafe release path");
|
||||
const content = await readFile(path.join(directory, file.path));
|
||||
const digest = createHash("sha256").update(content).digest("hex");
|
||||
if (content.byteLength !== file.bytes || digest !== file.sha256)
|
||||
throw new Error("Release checksum mismatch: " + file.path);
|
||||
}
|
||||
return manifest;
|
||||
}
|
||||
|
||||
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
||||
const manifest = await checkPackage(path.resolve(process.argv[2] ?? "."));
|
||||
console.log("release verified: " + manifest.version + " (" + manifest.files.length + " files)");
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import { checkPackage } from "./check-package.mjs";
|
||||
|
||||
async function createPackage() {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-release-"));
|
||||
await mkdir(path.join(directory, "apps"), { recursive: true });
|
||||
const content = Buffer.from("release fixture");
|
||||
await writeFile(path.join(directory, "apps/api.js"), content);
|
||||
await writeFile(
|
||||
path.join(directory, "manifest.json"),
|
||||
JSON.stringify({
|
||||
version: "fixture-1",
|
||||
files: [
|
||||
{
|
||||
path: "apps/api.js",
|
||||
bytes: content.byteLength,
|
||||
sha256: createHash("sha256").update(content).digest("hex"),
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
return directory;
|
||||
}
|
||||
|
||||
test("release package checker accepts a matching file manifest", async () => {
|
||||
const directory = await createPackage();
|
||||
try {
|
||||
const manifest = await checkPackage(directory);
|
||||
assert.equal(manifest.version, "fixture-1");
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("release package checker rejects modified package files", async () => {
|
||||
const directory = await createPackage();
|
||||
try {
|
||||
await writeFile(path.join(directory, "apps/api.js"), "modified");
|
||||
await assert.rejects(checkPackage(directory), /checksum mismatch/);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("release package checker rejects files missing from the manifest", async () => {
|
||||
const directory = await createPackage();
|
||||
try {
|
||||
await writeFile(path.join(directory, "extra.txt"), "not in manifest");
|
||||
await assert.rejects(checkPackage(directory), /file list mismatch/);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
@@ -1,50 +1,150 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { cp, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { checkPackage } from "./check-package.mjs";
|
||||
|
||||
const root = path.resolve(import.meta.dirname, "../..");
|
||||
const source = path.join(root, "apps/admin/dist");
|
||||
const output = path.join(root, "dist/release");
|
||||
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
||||
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 revision = git.stdout.trim();
|
||||
const version = process.env.RELEASE_VERSION ?? timestamp + "-" + revision;
|
||||
const output = path.join(root, "dist/releases", version);
|
||||
const archive = path.join(root, "dist/releases", "personal-brand-" + version + ".tar.gz");
|
||||
|
||||
function run(command, args, environment = {}) {
|
||||
const result = spawnSync(command, args, {
|
||||
cwd: root,
|
||||
stdio: "inherit",
|
||||
env: { ...process.env, ...environment },
|
||||
});
|
||||
function run(command, args) {
|
||||
const result = spawnSync(command, args, { cwd: root, stdio: "inherit" });
|
||||
if (result.status !== 0) process.exit(result.status ?? 1);
|
||||
}
|
||||
|
||||
const requiredFiles = ["index.html", "template-preview/index.html"];
|
||||
const manifest = { createdAt: new Date().toISOString(), files: [] };
|
||||
|
||||
for (const file of requiredFiles) {
|
||||
await readFile(path.join(source, file));
|
||||
async function listFiles(directory, prefix = "") {
|
||||
const files = [];
|
||||
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
||||
const relative = path.posix.join(prefix, entry.name);
|
||||
if (entry.isDirectory())
|
||||
files.push(...(await listFiles(path.join(directory, entry.name), relative)));
|
||||
else if (entry.isFile()) files.push(relative);
|
||||
else throw new Error("Unsupported release file: " + relative);
|
||||
}
|
||||
return files.sort();
|
||||
}
|
||||
|
||||
run("pnpm", ["--filter", "@personal-brand/api", "db:migrate:deploy"]);
|
||||
async function verifyStaticEntry(dist, entry, prefix) {
|
||||
const html = await readFile(path.join(dist, entry), "utf8");
|
||||
for (const match of html.matchAll(/(?:src|href)=["']([^"']+)["']/g)) {
|
||||
const reference = match[1];
|
||||
if (!/\.(?:js|css)(?:[?#]|$)/.test(reference)) continue;
|
||||
const url = new URL(reference, "https://release.invalid" + prefix);
|
||||
if (url.origin !== "https://release.invalid" || !url.pathname.startsWith(prefix)) {
|
||||
throw new Error("Invalid static base path in " + entry + ": " + reference);
|
||||
}
|
||||
const relative = decodeURIComponent(url.pathname.slice(prefix.length));
|
||||
const base = entry === "index.html" ? dist : path.join(dist, "template-preview");
|
||||
await readFile(path.join(base, relative));
|
||||
}
|
||||
}
|
||||
|
||||
await rm(output, { recursive: true, force: true });
|
||||
await mkdir(path.join(output, "api"), { recursive: true });
|
||||
await cp(source, path.join(output, "admin"), { recursive: true });
|
||||
await cp(path.join(root, "apps/api/dist"), path.join(output, "api"), { recursive: true });
|
||||
async function copyWorkspace(workspace) {
|
||||
const source = path.join(root, workspace);
|
||||
const target = path.join(output, workspace);
|
||||
await mkdir(target, { recursive: true });
|
||||
await cp(path.join(source, "package.json"), path.join(target, "package.json"));
|
||||
await cp(path.join(source, "src"), path.join(target, "src"), { recursive: true });
|
||||
await cp(path.join(source, "dist"), path.join(target, "dist"), { recursive: true });
|
||||
await cp(path.join(source, "tsconfig.json"), path.join(target, "tsconfig.json"));
|
||||
if (workspace === "apps/api") {
|
||||
await cp(path.join(source, "prisma/schema.prisma"), path.join(target, "prisma/schema.prisma"));
|
||||
await cp(path.join(source, "prisma/migrations"), path.join(target, "prisma/migrations"), {
|
||||
recursive: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const relativePath of [
|
||||
"api/main.js",
|
||||
"admin/index.html",
|
||||
"admin/template-preview/index.html",
|
||||
]) {
|
||||
const file = path.join(output, relativePath);
|
||||
const content = await readFile(file);
|
||||
manifest.files.push({
|
||||
path: relativePath,
|
||||
bytes: content.byteLength,
|
||||
sha256: createHash("sha256").update(content).digest("hex"),
|
||||
async function main() {
|
||||
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,100}$/.test(version))
|
||||
throw new Error("Invalid release version");
|
||||
const adminDist = path.join(root, "apps/admin/dist");
|
||||
await verifyStaticEntry(adminDist, "index.html", "/admin/");
|
||||
await verifyStaticEntry(adminDist, "template-preview/index.html", "/template-preview/");
|
||||
await mkdir(path.dirname(output), { recursive: true });
|
||||
await mkdir(output, { recursive: false });
|
||||
for (const file of [
|
||||
"package.json",
|
||||
"pnpm-lock.yaml",
|
||||
"pnpm-workspace.yaml",
|
||||
"tsconfig.base.json",
|
||||
]) {
|
||||
await cp(path.join(root, file), path.join(output, file));
|
||||
}
|
||||
for (const workspace of [
|
||||
"apps/api",
|
||||
"packages/config",
|
||||
"packages/types",
|
||||
"packages/validation",
|
||||
]) {
|
||||
await copyWorkspace(workspace);
|
||||
}
|
||||
await cp(adminDist, path.join(output, "apps/admin/dist"), { recursive: true });
|
||||
await cp(path.join(root, "apps/api/prisma/assets"), path.join(output, "seed-media"), {
|
||||
recursive: true,
|
||||
});
|
||||
await cp(path.join(root, "deploy"), path.join(output, "deploy"), { recursive: true });
|
||||
for (const file of ["check-package.mjs", "verify-public-release.mjs"]) {
|
||||
await mkdir(path.join(output, "scripts/deploy"), { recursive: true });
|
||||
await cp(path.join(root, "scripts/deploy", file), path.join(output, "scripts/deploy", file));
|
||||
}
|
||||
const validationPath = path.join(output, "packages/validation/package.json");
|
||||
const validation = JSON.parse(await readFile(validationPath, "utf8"));
|
||||
validation.exports["."].import = "./dist/index.js";
|
||||
validation.exports["."].types = "./dist/index.d.ts";
|
||||
await writeFile(validationPath, JSON.stringify(validation, null, 2) + "\n");
|
||||
const schemaPath = path.join(output, "apps/api/prisma/schema.prisma");
|
||||
const schema = await readFile(schemaPath, "utf8");
|
||||
await writeFile(
|
||||
schemaPath,
|
||||
schema.replace(/binaryTargets\s*=\s*\[[^\]]*\]/, 'binaryTargets = ["native"]'),
|
||||
);
|
||||
const status = spawnSync("git", ["status", "--porcelain"], { cwd: root, encoding: "utf8" });
|
||||
if (status.status !== 0) throw new Error("Cannot determine working tree state");
|
||||
const manifest = {
|
||||
version,
|
||||
revision,
|
||||
dirty: Boolean(status.stdout.trim()),
|
||||
createdAt: new Date().toISOString(),
|
||||
nodeMajor: 24,
|
||||
packageManager: "pnpm@9.15.0",
|
||||
files: [],
|
||||
};
|
||||
for (const file of await listFiles(output)) {
|
||||
if (/(?:^|\/)\.env(?:\.|$)/.test(file) && !file.endsWith(".example")) {
|
||||
throw new Error("Refusing to package environment file: " + file);
|
||||
}
|
||||
const content = await readFile(path.join(output, file));
|
||||
manifest.files.push({
|
||||
path: file,
|
||||
bytes: content.byteLength,
|
||||
sha256: createHash("sha256").update(content).digest("hex"),
|
||||
});
|
||||
}
|
||||
await writeFile(path.join(output, "manifest.json"), JSON.stringify(manifest, null, 2) + "\n");
|
||||
await checkPackage(output);
|
||||
run("tar", ["-czf", archive, "-C", output, "."]);
|
||||
const checksum = createHash("sha256")
|
||||
.update(await readFile(archive))
|
||||
.digest("hex");
|
||||
await writeFile(archive + ".sha256", checksum + " " + path.basename(archive) + "\n");
|
||||
console.log("release: " + version);
|
||||
console.log("files: " + manifest.files.length);
|
||||
console.log("archive: " + path.relative(root, archive));
|
||||
console.log("checksum: " + path.relative(root, archive + ".sha256"));
|
||||
}
|
||||
|
||||
await writeFile(path.join(output, "manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`);
|
||||
console.log(
|
||||
`release package: ${path.relative(root, output)} (${manifest.files.length} checked files)`,
|
||||
);
|
||||
run("pnpm", ["build:admin:preview"]);
|
||||
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", "exec", "prisma", "generate"]);
|
||||
run("pnpm", ["--filter", "@personal-brand/api", "build"]);
|
||||
await main();
|
||||
@@ -0,0 +1,89 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readdir, readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
const baseUrl = (process.env.PUBLIC_BASE_URL ?? "https://wechat.app.que01.top").replace(/\/+$/, "");
|
||||
const root = path.resolve(process.argv[2] ?? ".");
|
||||
const contentTypes = new Map([
|
||||
[".avif", "image/avif"],
|
||||
[".css", "text/css"],
|
||||
[".gif", "image/gif"],
|
||||
[".ico", "image/"],
|
||||
[".jpeg", "image/jpeg"],
|
||||
[".jpg", "image/jpeg"],
|
||||
[".js", "javascript"],
|
||||
[".json", "application/json"],
|
||||
[".png", "image/png"],
|
||||
[".svg", "image/svg+xml"],
|
||||
[".webp", "image/webp"],
|
||||
[".woff", "font/woff"],
|
||||
[".woff2", "font/woff2"],
|
||||
]);
|
||||
|
||||
async function checkResource(url, expectedType, label) {
|
||||
const response = await fetch(url);
|
||||
const contentType = response.headers.get("content-type") ?? "";
|
||||
assert.equal(response.status, 200, label + " returned " + response.status);
|
||||
assert.ok(contentType.includes(expectedType), label + " has content type " + contentType);
|
||||
}
|
||||
|
||||
async function checkHtmlAssets(relativeEntry, localPath, publicPath) {
|
||||
const html = await readFile(localPath, "utf8");
|
||||
for (const match of html.matchAll(/(?:src|href)=["']([^"']+)["']/g)) {
|
||||
const asset = match[1];
|
||||
if (!/\.(?:js|css)(?:[?#]|$)/.test(asset)) continue;
|
||||
const url = new URL(asset, baseUrl + publicPath);
|
||||
const expectedType = /\.css(?:[?#]|$)/.test(url.pathname) ? "text/css" : "javascript";
|
||||
await checkResource(url, expectedType, relativeEntry + " -> " + asset);
|
||||
}
|
||||
}
|
||||
|
||||
async function listFiles(directory, prefix = "") {
|
||||
const files = [];
|
||||
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
||||
const relative = path.posix.join(prefix, entry.name);
|
||||
if (entry.isDirectory())
|
||||
files.push(...(await listFiles(path.join(directory, entry.name), relative)));
|
||||
else if (entry.isFile()) files.push(relative);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
const apiHealthResponse = await fetch(baseUrl + "/api/health");
|
||||
assert.equal(apiHealthResponse.status, 200, "public API health check failed");
|
||||
assert.match(apiHealthResponse.headers.get("content-type") ?? "", /application\/json/);
|
||||
assert.equal((await apiHealthResponse.json()).status, "ok");
|
||||
|
||||
for (const [entry, localFile, publicPath] of [
|
||||
["admin", path.join(root, "apps/admin/dist/index.html"), "/admin/"],
|
||||
[
|
||||
"template preview",
|
||||
path.join(root, "apps/admin/dist/template-preview/index.html"),
|
||||
"/template-preview/",
|
||||
],
|
||||
]) {
|
||||
await checkResource(baseUrl + publicPath, "text/html", entry);
|
||||
await checkHtmlAssets(entry, localFile, publicPath);
|
||||
}
|
||||
|
||||
const assetRoot = path.join(root, "apps/admin/dist/assets");
|
||||
for (const asset of await listFiles(assetRoot)) {
|
||||
const extension = path.extname(asset).toLowerCase();
|
||||
const expectedType = contentTypes.get(extension);
|
||||
if (!expectedType) continue;
|
||||
await checkResource(
|
||||
baseUrl + "/assets/" + asset.split("/").map(encodeURIComponent).join("/"),
|
||||
expectedType,
|
||||
"static asset " + asset,
|
||||
);
|
||||
}
|
||||
|
||||
for (const entry of await readdir(path.join(root, "seed-media"))) {
|
||||
await checkResource(
|
||||
baseUrl + "/media/seed/" + encodeURIComponent(entry),
|
||||
"image/jpeg",
|
||||
"seed media " + entry,
|
||||
);
|
||||
}
|
||||
|
||||
console.log("public release verified: admin, preview, assets, and seed media");
|
||||
Reference in new issue
Block a user