#!/usr/bin/env bash # 검증 진입점. 사람·CI·에이전트가 같은 명령을 쓴다(docs/work/reference/gates.md). # # scripts/verify.sh <단계> [대상…] [--on here|<호스트>] [--log <이름>] [--apply-back] [--dry-run] [-- 인자…] # # 단계 무엇 기본 위치 # docs 작업 매뉴얼 검사(check-work-docs.py)와 장부 형식 여기 # hooks repo 훅·검사기·장부 시험(python3 unittest) 여기 # gate … 게이트마다 `corepack pnpm ci:gate `, CI env로 desktop # parts 그 게이트의 명령을 하나씩 끝까지(첫 실패에서 멈추지 않음) desktop # script <이름>… [-- 인자] package script를 하나씩. `--` 뒤 인자는 script가 하나일 때만 desktop # exec -- <명령…> 명령 하나(예: node scripts/run-vitest.ts run <파일>) desktop # config/ci/gates.json stages의 gateIds 전부(merge 등) desktop # # 게이트·stage는 실패가 있어도 끝까지 돌고, 끝에 게이트마다 rc와 초를 적는다. 실패가 하나라도 있으면 rc=1. # --on <호스트>: 지금 작업 트리(커밋 안 된 변경 포함)를 패치로 그 호스트에 옮긴다. 기준 커밋에서 새 worktree를 # 만들어 패치를 적용하고 트리 해시가 여기와 같은지 본 뒤, 그 트리를 커밋 하나로 만들어(깨끗한 HEAD) 의존성을 # 오프라인으로 설치하고 잠금 아래에서 같은 단계를 돌린다. 로그를 남기고 worktree를 지운다. push하지 않는다. # 원격에서 바뀐 파일은 로그 폴더의 changes.patch에 남는다. --apply-back이면 그 패치를 여기 작업 트리에 얹는다 # (create:feature, 기준선 다시 쓰기처럼 파일을 만드는 명령). VERIFY_SNAPSHOT=1이면 artifacts·dist·test-results를 # 로그 폴더의 snapshot/에 복사한다(리뷰어가 함께 읽는 사본). # 환경 변수: VERIFY_HOST(desktop 단계의 --on 기본값, 기본 desktop), VERIFY_REMOTE_REPO(원격 checkout, 기본 # workspace/, 홈 기준), VERIFY_BASE_REF(기본 origin/main), VERIFY_LOCK(기본 /tmp/claude-gates/heavy.lock), # VERIFY_LOG_ROOT(원격 로그 폴더, 기본 /tmp/claude-gates), VERIFY_SSH(기본 ssh), VERIFY_NODE_BIN(원격 node 폴더, # 기본 ~/.nvm/versions/node/v<.nvmrc>/bin), VERIFY_INSTALL(0이면 원격 의존성 설치를 건너뛴다), # VERIFY_HOST_ENVIRONMENT(off면 hostEnvironment 게이트도 시스템 컨테이너 없이 이 호스트에서 돈다). # 종료 코드: 0 통과, 1 실패, 2 사용법, 4 원격 준비 실패. set -euo pipefail ROOT=$(git rev-parse --show-toplevel) cd "$ROOT" TMP_FILES=() trap '[ ${#TMP_FILES[@]} -eq 0 ] || rm -f "${TMP_FILES[@]}"' EXIT # 빈 배열 + set -u는 옛 bash에서 오류다 DRY_RUN=0 APPLY_BACK=0 usage() { sed -n '2,27p' "$0" | sed 's/^# \{0,1\}//' >&2 exit 2 } # ---------------------------------------------------------------- config/ci/gates.json 읽기 gates_json() { python3 - "$@" <<'PY' import json, sys contract = json.load(open("config/ci/gates.json", encoding="utf-8")) kind, *names = sys.argv[1:] if kind == "stages": print("\n".join(stage["id"] for stage in contract["stages"])) elif kind == "stage": found = [stage for stage in contract["stages"] if stage["id"] == names[0]] if not found: sys.exit(1) print("\n".join(found[0]["gateIds"])) elif kind == "gate": if not any(gate["id"] == names[0] for gate in contract["gates"]): sys.exit(1) elif kind == "host": gate = next((gate for gate in contract["gates"] if gate["id"] == names[0]), None) if gate is None: sys.exit(1) print(gate.get("hostEnvironment", "")) elif kind == "scripts": gate = next((gate for gate in contract["gates"] if gate["id"] == names[0]), None) if gate is None: sys.exit(1) commands = {command["id"]: command for command in contract["commands"]} for command_id in gate["commandIds"]: command = commands[command_id] # \x1f between fields keeps an empty exit code a field; \x1e between the command's args. args = "\x1e".join(command.get("args", [])) print(f'{command["script"]}\x1f{command.get("expect", "pass")}\x1f{command.get("expectedExitCode", "")}\x1f{args}') PY } is_stage() { gates_json stages | grep -qx -- "$1"; } # ---------------------------------------------------------------- 단계 본문 run_docs() { local rc=0 python3 scripts/check-work-docs.py || rc=1 if [ -s docs/ledger/estimates.jsonl ] && ! python3 -c ' import json, sys for number, line in enumerate(open(sys.argv[1], encoding="utf-8"), 1): if line.strip(): json.loads(line) ' docs/ledger/estimates.jsonl; then echo "docs/ledger/estimates.jsonl에 JSON이 아닌 줄이 있다." >&2 rc=1 fi return "$rc" } run_hooks() { python3 -m unittest discover -s scripts/tests; } # CI workflow의 전역 env(.gitea/workflows/quality-gates.yml)와 같은 꼴. CI=true가 있어야 빌드 식별값을 엄격하게 본다. ci_env() { local sha sha=$(git rev-parse HEAD) export CI=true VITE_COMMIT_SHA="$sha" VITE_BUILD_ID="gitea-${VERIFY_RUN:-1}-1" \ RELEASE_ID="refs/heads/${VERIFY_REF:-verify}-${VERIFY_RUN:-1}-1" \ CI_RUNNER_IMAGE="local/runner@sha256:$(printf 'a%.0s' $(seq 64))" \ SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)" # ci:gate makes SOURCE_DATE_EPOCH from HEAD itself; parts and script call the commands directly. } # 한 줄 결과: "<이름> rc= <초>s". 로그는 VERIFY_STEP_LOGS가 있으면 그 폴더에 이름별로. step() { local name=$1 rc start log shift if [ "$DRY_RUN" = 1 ]; then local shown shown=$(printf '%q ' "$@") echo "$name: ${shown% }" return 0 fi start=$(date +%s) log=${VERIFY_STEP_LOGS:+$VERIFY_STEP_LOGS/$(printf '%s' "$name" | tr ' /:' '___').log} set +e if [ -n "$log" ]; then "$@" > "$log" 2>&1; else "$@"; fi rc=$? set -e echo "$name rc=$rc $(( $(date +%s) - start ))s" | tee -a "${VERIFY_SUMMARY:-/dev/null}" return "$rc" } # 계약이 hostEnvironment를 정한 게이트(FE-GATE-013)는 workflow와 같이 그 환경에서 돈다. # VERIFY_HOST_ENVIRONMENT=off면 이 호스트에서 바로 돈다(컨테이너를 띄울 수 없는 호스트, gates.md 3절). host_prefix() { [ "${VERIFY_HOST_ENVIRONMENT:-on}" != off ] || return 0 case "$(gates_json host "$1")" in system-container) echo scripts/run-in-system-container.sh ;; esac } run_gates() { local failed=0 gate prefix [ "$DRY_RUN" = 1 ] || ci_env for gate in "$@"; do gates_json gate "$gate" || { echo "verify: config/ci/gates.json에 없는 게이트: $gate" >&2; return 2; } prefix=$(host_prefix "$gate") # shellcheck disable=SC2086 # 접두어는 공백 없는 경로 하나이거나 비어 있다 step "$gate" $prefix corepack pnpm -s ci:gate "$gate" || failed=1 done return "$failed" } run_parts() { local gate=$1 failed=0 script expect code argtext rc args prefix [ "$DRY_RUN" = 1 ] || ci_env gates_json scripts "$gate" > /dev/null || { echo "verify: config/ci/gates.json에 없는 게이트: $gate" >&2; return 2; } prefix=$(host_prefix "$gate") while IFS=$'\x1f' read -r script expect code argtext; do rc=0 args=() # The command's args, as run-ci-gate.ts passes them (`pnpm