Files
marqdo-site/scripts/build-release.mjs
T
cflmy 8f074e9258
ci/woodpecker/push/ci Pipeline was successful
ci/woodpecker/push/deploy-edgeone Pipeline failed
feat(site): consume the verified root projection and ship /data with production
- build:release reads the immutable root projection from the root Release
  assets (MARQDO_DATA_RELEASE) or the production origin, verifies it against
  MARQDO_DATA_ROOT_SHA, and deploys the same pointer/snapshot under /data/;
- production pipeline runs prepare-example + build:release; the manual trigger
  supplies the accepted root commit and release base, failing closed without them;
- verify-dist enforces /data consistency in release builds and absence in safe
  builds; verify-deploy probes the pointer, snapshot, both product offers and the
  MQDH download entry;
- pin the site example and evidence to the accepted CLI v3.0.0-rc.7 and document
  the MQDH beta install path in the quick start;
- drop the temporary screenshot helper and keep only durable baseline facts in
  docs/BASELINE.md; progress lives in the repository issues.
2026-12-02 17:27:49 +08:00

79 lines
3.9 KiB
JavaScript

import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';
import { packageRelease } from './package-release.mjs';
import { parsePointer, parseSnapshot } from '../src/lib/releases.mts';
const origin = 'https://marqdo.com';
// A release build consumes the immutable projection published by the root
// repository. `MARQDO_DATA_RELEASE` is the root Release download base (for a
// first publication that also seeds `/data/` on this origin); without it the
// last verified copy is read from the production origin.
const releaseBase = (process.env.MARQDO_DATA_RELEASE ?? '').replace(/\/+$/, '');
async function get(url, limit, label) {
const response = await fetch(url, { redirect: 'follow', signal: AbortSignal.timeout(60_000) });
if (!response.ok || !response.body) throw new Error(`${label}: HTTP ${response.status}`);
const chunks = [];
let length = 0;
for await (const chunk of response.body) {
length += chunk.byteLength;
if (length > limit) throw new Error(`Public data exceeds limit at ${label}`);
chunks.push(chunk);
}
return Buffer.concat(chunks);
}
async function fetchProjection() {
if (releaseBase === '') {
const pointerBytes = await get(new URL('/data/current.json', origin).href, 4096, '/data/current.json');
const pointer = parsePointer(JSON.parse(pointerBytes.toString('utf8')));
const snapshotBytes = await get(new URL(pointer.snapshot, origin).href, 16 * 1024 * 1024, pointer.snapshot);
return { pointerBytes, pointer, snapshotBytes };
}
const pointerBytes = await get(`${releaseBase}/current.json`, 4096, 'release current.json');
const pointer = parsePointer(JSON.parse(pointerBytes.toString('utf8')));
const name = pointer.snapshot.slice('/data/snapshots/'.length);
if (!/^[a-f0-9]{64}\.json$/.test(name)) throw new Error(`Invalid snapshot name: ${name}`);
const snapshotBytes = await get(`${releaseBase}/${name}`, 16 * 1024 * 1024, `release snapshot ${name}`);
return { pointerBytes, pointer, snapshotBytes };
}
const rootSha = process.env.MARQDO_DATA_ROOT_SHA;
if (!/^[a-f0-9]{40}$/.test(rootSha ?? '')) throw new Error('Set MARQDO_DATA_ROOT_SHA to the root identity expected by the trusted build');
const websiteSha = process.env.CI_COMMIT_SHA;
if (!/^[a-f0-9]{40}$/.test(websiteSha ?? '')) throw new Error('Release build requires the website source SHA in CI_COMMIT_SHA');
const { pointerBytes, pointer, snapshotBytes } = await fetchProjection();
parseSnapshot(pointer, snapshotBytes, rootSha);
const dir = await mkdtemp(join(tmpdir(), 'marqdo-site-data-'));
try {
const pointerFile = join(dir, 'pointer.json');
const snapshotFile = join(dir, 'snapshot.json');
await writeFile(pointerFile, pointerBytes);
await writeFile(snapshotFile, snapshotBytes);
const result = spawnSync('npm', ['run', 'build'], {
stdio: 'inherit',
env: { ...process.env, MARQDO_RELEASE_BUILD: '1', MARQDO_POINTER_FILE: pointerFile, MARQDO_SNAPSHOT_FILE: snapshotFile },
});
if (result.error) throw result.error;
if (result.status !== 0) process.exitCode = result.status ?? 1;
else {
// The verified projection is deployed with the same immutable site build,
// so the production origin keeps serving `/data/` from one authority.
await mkdir(join('dist', 'data', 'snapshots'), { recursive: true });
await writeFile(join('dist', 'data', 'current.json'), pointerBytes);
await writeFile(join('dist', 'data', 'snapshots', `${pointer.sha256}.json`), snapshotBytes);
const verify = spawnSync('node', ['scripts/verify-dist.mjs'], {
stdio: 'inherit',
env: { ...process.env, MARQDO_RELEASE_BUILD: '1', MARQDO_DATA_READY: '1' },
});
if (verify.error) throw verify.error;
if (verify.status !== 0) process.exitCode = verify.status ?? 1;
else packageRelease(websiteSha, rootSha, pointer);
}
} finally {
await rm(dir, { recursive: true, force: true });
}