WEB/scripts/remote/deploy.sh

52 lines
1.9 KiB
Bash
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

set -euo pipefail
cd "$RELEASE"
# 4.1 解压
tar -xzf dist.tar.gz
rm -f dist.tar.gz
# 4.2 完整性校验:新版本必须完整,残缺则取消切换(当前线上版本不受影响)
if [ ! -s index.html ]; then
echo "❌ 新版本 index.html 缺失或为空,取消切换"
exit 1
fi
missing=""
for ref in $(grep -oE '/assets/[A-Za-z0-9._-]+\.(js|css)' index.html | sort -u || true); do
[ -f ".$ref" ] || missing="$missing $ref"
done
if [ -n "$missing" ]; then
echo "❌ 新版本缺少以下资源,取消切换:$missing"
exit 1
fi
# 4.3 补齐旧版本静态资源(无感切换的关键)
# 把最近几个历史版本中、本版本缺失的文件(旧 chunk、hikvision、logo 等)合并进新版本,
# 保证切换后正在使用旧版本页面的用户,动态 import 旧资源依然能找到,不会 404。
for src in $(ls -td "$BASE"/releases/*/ 2>/dev/null | head -n "$((KEEP + 1))" || true); do
[ -d "$src" ] || continue
[ "$(readlink -f "$src" 2>/dev/null)" = "$(pwd)" ] && continue
cp -rn "$src/." "$RELEASE/" 2>/dev/null || true
done
# 4.4 原子切换软链接(先建临时链接再改名,切换瞬间服务不中断)
ln -sfn "$RELEASE" "$BASE/current_new"
mv -Tf "$BASE/current_new" "$BASE/current"
# 4.5 健康检查(仅提示,不阻断发布)
if command -v curl >/dev/null 2>&1; then
code="$(curl -skL -o /dev/null -w '%{http_code}' -H "Host: $HOST" "https://127.0.0.1/" 2>/dev/null || true)"
echo "ℹ️ 新版本首页 HTTP 状态码: ${code:-未知}"
fi
# 4.6 清理旧版本:只保留最近 KEEP 个,且绝不删除 current 指向的版本
CURRENT_TARGET="$(readlink -f "$BASE/current" 2>/dev/null || true)"
count=0
for d in $(ls -t "$BASE/releases" 2>/dev/null || true); do
count=$((count+1))
[ "$count" -le "$KEEP" ] && continue
target="$(readlink -f "$BASE/releases/$d" 2>/dev/null || echo "$BASE/releases/$d")"
[ "$target" = "$CURRENT_TARGET" ] && continue
rm -rf "$BASE/releases/$d"
done