Files
clean-architecture-frontend…/scripts/lib/build-environment.ts
T
DongHyeonka c1a98e9743 fix(build): 빌드 식별값의 기본값을 한 함수로 읽는다
문제: 빌드 ID·커밋·release ID의 기본값을 일곱 곳에서 따로 계산하고
`||`와 `??`가 섞였다. 그래서 VITE_BUILD_ID=""이면 runtime config는
local-build가 되고, build manifest·Service Worker·재현 빌드 기록은 ""가
된다(R-2).

고른 방법: src/contracts/env.ts에 BUILD_IDENTITY_PLACEHOLDERS와
readBuildIdentity(environment)를 두고(빈 값은 없는 것과 같다), 페이지의
getBuildConfig와 빌드 스크립트 다섯·vite.service-worker.config.ts가 모두
그것을 쓴다. ENV_REGISTRY 기본값과 LOCAL_BUILD_IDENTITY도 같은 상수에서
나온다. 시험이 빈 값 규칙과, 읽는 파일마다 자기 기본값을 다시 쓰지
않는지를 본다.
버린 대안: 빈 값을 거절하기 — local 빌드가 쓰는 placeholder 규칙을 바꾸고
admission이 이미 placeholder를 공개 대상에서 거절한다.

위험·한계: generate-build-manifest.ts·verify-reproducible-build.ts는 그룹 C도
고치는 파일이다. 줄은 겹치지 않는다.

검증: desktop 결과는 PR 본문.

Refs: R-2
2026-11-13 20:17:04 +09:00

125 lines
4.1 KiB
TypeScript

import { BUILD_IDENTITY_PLACEHOLDERS } from "../../src/contracts/env.ts";
export const CI_BUILD_ENVIRONMENT_VARIABLES = Object.freeze([
"VITE_BUILD_ID",
"VITE_COMMIT_SHA",
"RELEASE_ID",
"CI_RUNNER_IMAGE",
"SOURCE_DATE_EPOCH",
]);
export function ciBuildEnvironmentFailures(
environment: Readonly<Record<string, string | undefined>>,
) {
if (environment.CI !== "true") return [];
const failures = CI_BUILD_ENVIRONMENT_VARIABLES.filter(
(name) => !environment[name]?.trim(),
).map((name) => `missing required CI build environment: ${name}`);
const commitSha = environment.VITE_COMMIT_SHA?.trim();
if (commitSha && !isValidCommitSha(commitSha)) {
failures.push(
"VITE_COMMIT_SHA must be a full 40- or 64-character hexadecimal commit ID",
);
}
const sourceDateEpoch = environment.SOURCE_DATE_EPOCH?.trim();
if (sourceDateEpoch && !isValidSourceDateEpoch(sourceDateEpoch)) {
failures.push("SOURCE_DATE_EPOCH must be non-negative epoch seconds");
}
const runnerImage = environment.CI_RUNNER_IMAGE?.trim();
if (
runnerImage &&
!/@sha256:[0-9a-f]{64}$/i.test(runnerImage)
) {
failures.push(
"CI_RUNNER_IMAGE must end with an immutable @sha256 image digest",
);
}
return failures;
}
/**
* 1103/11-08. The workflow sets CI=true and the CI build identity for every
* job. A build a test makes of a copy (a fixture build, the visual goldens'
* build, a dev server) is a local build, never the job's release build, so it
* starts without them. The job's own release build keeps the check above.
*
* `localBuildEnvironment` is for a process this code spawns: the caller's
* environment without CI and the CI identity. `LOCAL_BUILD_IDENTITY` is for a
* Playwright web server, whose env Playwright merges over process.env, so CI
* is overridden there instead of removed.
*/
export const LOCAL_BUILD_IDENTITY = Object.freeze({
CI: "",
VITE_BUILD_ID: BUILD_IDENTITY_PLACEHOLDERS.buildId,
VITE_COMMIT_SHA: BUILD_IDENTITY_PLACEHOLDERS.commitSha,
RELEASE_ID: BUILD_IDENTITY_PLACEHOLDERS.releaseId,
});
export function localBuildEnvironment(
environment: Readonly<Record<string, string | undefined>> = process.env,
): Record<string, string | undefined> {
const local: Record<string, string | undefined> = { ...environment };
for (const name of ["CI", ...CI_BUILD_ENVIRONMENT_VARIABLES]) delete local[name];
return local;
}
export function assertCiBuildEnvironment(
environment: Readonly<Record<string, string | undefined>>,
) {
const failures = ciBuildEnvironmentFailures(environment);
if (failures.length > 0) {
throw new Error(failures.join("; "));
}
}
export function isValidCommitSha(value: string) {
return /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/i.test(value);
}
export function isValidSourceDateEpoch(value: string) {
if (!/^\d+$/.test(value)) return false;
const milliseconds = Number(value) * 1_000;
return Number.isSafeInteger(milliseconds) && Number.isFinite(
new Date(milliseconds).getTime(),
);
}
export function ciCheckoutIdentityFailures(
environment: Readonly<Record<string, string | undefined>>,
checkout: { commitSha: string; sourceDateEpoch: string },
) {
if (environment.CI !== "true") return [];
const failures = [];
const configuredCommitSha = environment.VITE_COMMIT_SHA?.trim();
if (
configuredCommitSha &&
configuredCommitSha.toLowerCase() !== checkout.commitSha.toLowerCase()
) {
failures.push("VITE_COMMIT_SHA does not identify the checked-out commit");
}
const configuredEpoch = environment.SOURCE_DATE_EPOCH?.trim();
if (configuredEpoch && configuredEpoch !== checkout.sourceDateEpoch) {
failures.push(
"SOURCE_DATE_EPOCH does not match the checked-out commit timestamp",
);
}
return failures;
}
export function buildDate(
environment: Readonly<Record<string, string | undefined>>,
) {
const sourceDateEpoch = environment.SOURCE_DATE_EPOCH?.trim();
if (!sourceDateEpoch) return new Date();
if (!isValidSourceDateEpoch(sourceDateEpoch)) {
throw new Error("SOURCE_DATE_EPOCH must be non-negative epoch seconds");
}
return new Date(Number(sourceDateEpoch) * 1_000);
}