diff --git a/scripts/deploy.sh b/scripts/deploy.sh index b2a971b..d3ee319 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -117,6 +117,34 @@ verify_remote() { fi done + # 资源级断言:只查 HTML 页会把「CSS/JS 全 404 的破站」判成部署成功 + # (2026-10-08 事故:nginx web root 镜像缺失,5 个页面全 200 而站点无样式)。 + log "" + log "📋 静态资源断言(从首页 HTML 实际引用的 URL 取样)" + local html assets asset status checked=0 + html=$(curl -fsSL --max-time 15 "${DOMAIN}/" 2>/dev/null || echo "") + if [[ -z "$html" ]]; then + log " ❌ 首页 HTML 取回为空,无法取样资源" + failed=1 + else + assets=$(printf '%s\n' "$html" | grep -o '/_next/static/[^"]*\.\(js\|css\)' | sort -u | head -5) + if [[ -z "$assets" ]]; then + log " ❌ 首页 HTML 里没出现 /_next/static 引用(构建异常)" + failed=1 + fi + for asset in $assets "/favicon.ico"; do + checked=$((checked + 1)) + status=$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 "${DOMAIN}${asset}" 2>/dev/null || echo "000") + if [[ "$status" == "200" ]]; then + log " ✅ ${asset} ($status)" + else + log " ❌ ${asset} ($status)" + failed=1 + fi + done + log " 取样数: $checked" + fi + if [[ "$failed" == "1" ]]; then log "❌ 生产环境验证失败" return 1 @@ -201,6 +229,23 @@ cmd_deploy() { DIST_SIZE=$(du -sh "$DIST_DIR" | cut -f1) log "✅ $DIST_DIR 目录存在,大小: $DIST_SIZE" + # 步骤 1.5: 生成 nginx web root 镜像 + # 远端 nginx 的 root 就是本脚本上传的 dist/(nginx-static-production.conf:132,136-142 的 + # location /_next/static/ 用 try_files $uri =404),而 next build 的布局是 + # dist/static/**(无 _next 前缀)+ 独立的 public/**。不同步这份镜像,rsync --delete 就会 + # 抹掉上一次部署留下的镜像 ⇒ HTML 全 200 而 CSS/JS/图标全 404(2026-10-08 线上事故根因)。 + log "" + log "📋 步骤 1.5: 生成 nginx web root 镜像 (dist/_next/static + public → dist 根)" + if [[ ! -d "$DIST_DIR/static" ]]; then + log "❌ $DIST_DIR/static 不存在,产物未装配,终止部署" + exit 1 + fi + rm -rf "$DIST_DIR/_next/static" + mkdir -p "$DIST_DIR/_next" + cp -R "$DIST_DIR/static" "$DIST_DIR/_next/static" + [[ -d "$PROJECT_ROOT/public" ]] && cp -R "$PROJECT_ROOT/public/." "$DIST_DIR/" + log "✅ 镜像完成: _next=$(du -sh "$DIST_DIR/_next" | cut -f1) dist=$(du -sh "$DIST_DIR" | cut -f1)" + # 步骤 2: 验证 SSH 连接 log "" log "📋 步骤 2: 验证 SSH 连接"