#!/usr/bin/env bash # # UniCut 站点构建发布脚本(带断网 lint 门禁) # # bash scripts/build.sh # # 流程:拿构建锁 → 刷新本地素材 → 构建镜像 → 发布容器 → 导出产物跑 lint → curl 健康检查 # # 环境变量: # SKIP_LOCALIZE=1 跳过素材下载(默认会跑 scripts/localize-assets.py,可重复执行、已存在则跳过) # SKIP_LINT=1 跳过产物 lint(只做构建发布) # # 说明:构建锁放在 /tmp/unicut_build.lock,避免多个任务并发 build 互相覆盖镜像与容器; # 构建日志统一写 /tmp/unicut_build_site.log。 set -euo pipefail SRC=/vol1/1000/HD1/docker/unicut-src LOCK=/tmp/unicut_build.lock LOG=/tmp/unicut_build_site.log SITE=https://127.0.0.1:3005 WAIT_MAX=${LOCK_WAIT_MAX:-600} cd "$SRC" # ---------- 1. 构建锁 ---------- waited=0 until mkdir "$LOCK" 2>/dev/null; do if [ "$waited" -ge "$WAIT_MAX" ]; then echo "[build] 等待构建锁超时($LOCK 已被占用 ${WAIT_MAX}s)" >&2 exit 1 fi echo "[build] 构建锁被占用,${waited}s 后重试…" sleep 15 waited=$((waited + 15)) done trap 'rmdir "$LOCK" 2>/dev/null || true' EXIT echo "[build] 拿到构建锁(等待了 ${waited}s)" # ---------- 2. 本地素材(可重复执行) ---------- if [ "${SKIP_LOCALIZE:-0}" != "1" ]; then echo "[build] 刷新本地素材 scripts/localize-assets.py" python3 scripts/localize-assets.py || echo "[build] 素材刷新有失败项,继续构建(详见 data-quality/reports/site/assets-localize.json)" fi # ---------- 3. 构建 + 发布 ---------- echo "[build] docker compose build unicut(日志:$LOG)" { docker compose build unicut docker compose up -d --no-build unicut } 2>&1 | tee "$LOG" # ---------- 4. 导出产物并跑断网 lint ---------- if [ "${SKIP_LINT:-0}" != "1" ]; then DIST=$(mktemp -d /tmp/unicut-dist-XXXXXX) docker exec unicut tar -C /usr/share/nginx/html -cf - . | tar -C "$DIST" -xf - echo "[build] 断网 lint:dist 导出到 $DIST" set +e python3 data-quality/scripts/site/00_lint_dist.py --dist "$DIST" 2>&1 | tee -a "$LOG" LINT_RC=${PIPESTATUS[0]} set -e else LINT_RC=0 fi # ---------- 5. 健康检查:首页与详情页必须 200 ---------- echo "[build] 健康检查 $SITE" for path in / /article/local-deploy-guide /article/offline-ready /offline-notice.html; do code=$(curl -sk -o /dev/null -w '%{http_code}' "$SITE$path") echo "[build] $code $SITE$path" if [ "$code" != "200" ]; then echo "[build] ❌ $path 返回 $code" >&2 exit 1 fi done if [ "$LINT_RC" -ne 0 ]; then echo "[build] ❌ 断网 lint 未通过(external_refs != 0),详见上方报告" >&2 exit "$LINT_RC" fi echo "[build] ✅ 构建、发布、lint、健康检查全部通过"