Files
clean-architecture-frontend…/scripts/lib/build-environment.ts
T
DongHyeonka b693600103 fix(ci): 테스트가 만드는 로컬 빌드는 CI=true에서도 release 빌드로 검사되지 않는다
문제
workflow는 모든 job에 CI=true와 CI 빌드 식별값을 준다. 메인 세션이 깨끗한
clone에서 그 env 그대로(CI=true 포함) merge 단계 14개를 돌리자 FE-GATE-005·
008·020이 실패했다.
- vitest 안의 fixture 빌드(tests/e2e/support/fixture-builds.ts →
  build-frontend.ts)가 CI=true를 물려받아 release 빌드처럼 검사된다.
  af312de가 vitest에서 VITE_COMMIT_SHA를 지워 "missing required CI build
  environment: VITE_COMMIT_SHA"로 멈춘다(service-worker-build-output 7건이
  돌지 않음, 제거 드릴 안에서도 같다).
- 시각 골든 build(9bddfa5)가 VITE_COMMIT_SHA="local"을 받는데 CI=true가
  40·64자리 hex를 요구해 webServer가 뜨지 않는다.
내 X4가 CI=true 없이 돌아 놓쳤다. 1103/11-07과 같은 원인이다.

고른 방법
- scripts/lib/build-environment.ts에 둘을 둔다.
  - localBuildEnvironment(env): spawn하는 로컬 빌드용. CI와 CI 빌드 식별값을
    뺀 사본. fixture-builds.ts, service-worker-builds.ts가 쓴다.
  - LOCAL_BUILD_IDENTITY: Playwright web server용. Playwright가 env를
    process.env 위에 합치므로 CI를 지우지 않고 ""로 덮는다(검사는
    CI === "true"일 때만). visual·capabilities·dev 설정이 쓴다.
- assertCiBuildEnvironment의 CI=true 검사는 그대로다. job 자신의 release
  빌드(build:release-candidate 등)는 계속 검사된다.
- 버린 대안: 로컬 빌드에 유효한 가짜 커밋 id(0×40)를 주기. CI=true 검사의
  다른 칸(CI_RUNNER_IMAGE, SOURCE_DATE_EPOCH)도 함께 맞춰야 하고, 로컬
  빌드가 release 빌드인 척한다.

위험·한계
- 로컬 빌드 안에서 CI를 읽어 동작을 바꾸는 코드가 있으면 그 빌드는 이제
  CI가 아닌 쪽으로 돈다(지금은 build-environment 검사뿐이다).

검증
- tests/unit/local-build-environment.test.ts 3건: 구현 전 2건 실패(RED),
  CI job 자체 검사는 유지.
- CI=true·VITE_COMMIT_SHA·VITE_BUILD_ID=gitea-41-1·RELEASE_ID·
  CI_RUNNER_IMAGE·SOURCE_DATE_EPOCH로: service-worker-build-output 7건,
  test:visual 6/6, test:unit 2,883, test:component 270, test:integration 189
  통과. 수정 전 service-worker-build-output은 7건 미실행.
- check:types, lint 통과.

Refs: docs/reviews/2026-11-03-maturity-review/README.md 13절 11-08
2026-11-05 04:26:44 +09:00

123 lines
3.9 KiB
TypeScript

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: "local-build",
VITE_COMMIT_SHA: "local",
RELEASE_ID: "local-release",
});
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);
}