fix(deploy): 上传前生成 nginx web root 镜像,并把部署验证升级为资源级断言

2026-10-08 生产事故:deploy.sh 打出「🎉 部署成功」而线上 CSS/JS/图标全 404。
根因是 nginx 站点根就是上传后的 dist/(nginx-static-production.conf:132 的
root /var/www/novalon + :136-142 的 try_files $uri =404),要求磁盘上存在
dist/_next/static/**;而 next build 出的是 dist/static/**,postbuild 只补
standalone 容器形态。历史镜像由旧流程产出,被本次 rsync --delete 抹掉。

- 步骤 1.5:上传前本地生成镜像(dist/static → dist/_next/static、public/. → dist/),
  dist/static 缺失即终止部署而非静默继续
- verify_remote:从首页 HTML 实际引用的 URL 取前 5 个 /_next/static/*.{js,css}
  加 /favicon.ico 逐个断言 200,任一非 200 即 return 1。负对照实测:换成不存在
  的取样 URL ⇒ RED 404 且 failed=1,断言可失败
- 只查五个 HTML 路径的门禁,在破站状态下会判绿——这是本次事故的直接放大原因
This commit is contained in:
zhangxiang committed 2026-10-08 17:41:35 +08:00
1 parent 19079e189d
commit 722f08a159
1 file changed
+45
+45
View File
@@ -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 连接"