WEB/deploy.sh
2026-08-26 18:27:37 +08:00

95 lines
3.6 KiB
Bash
Raw 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.

#!/bin/bash
set -eu
# --- 配置 ---
USER="webdev"
IP="112.124.21.102" # 服务器 IP
BASE_PATH="/www/wwwroot/web" # 项目在服务器上的存放路径
RELEASE_NAME="v$(date +'%Y%m%d_%H%M%S')_$$" # 版本号(带 PID,避免并发/重复发布冲突)
LOCAL_DIST="./dist"
KEEP_RELEASES=5 # 服务器上保留的最近版本数
SITE_HOST="wms.wms.gztbyc.com" # 发版后健康检查域名
# SSH 密钥提示:请确保本地已将 id_ed25519.pub 添加到服务器的 ~/.ssh/authorized_keys
# 密钥内容:ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJtwa+MFLQEgkEbcGQ6dkQOiFydRvjVeidJ/7uBsF2e 3313488199@qq.com
SSH_KEY="$HOME/.ssh/id_ed25519_admin"
SSH_OPTS="-i $SSH_KEY -o StrictHostKeyChecking=no"
REMOTE_RELEASE="$BASE_PATH/releases/$RELEASE_NAME"
# 无论成功失败都清理本地临时压缩包
trap 'rm -f dist.tar.gz' EXIT
echo "🚀 开始发布版本: $RELEASE_NAME"
# 1. 本地打包(失败立即中止,防止把坏产物发到线上)
npm run build
# 2. 校验本地产物完整性
if [ ! -f "$LOCAL_DIST/index.html" ] || [ ! -d "$LOCAL_DIST/assets" ]; then
echo "❌ 本地构建产物不完整(缺少 index.html 或 assets),中止发布"
exit 1
fi
# 3. 压缩并上传到独立的新版本目录(全程不触碰当前运行目录)
tar -czf dist.tar.gz -C "$LOCAL_DIST" .
ssh $SSH_OPTS $USER@$IP "mkdir -p '$REMOTE_RELEASE'"
scp $SSH_OPTS dist.tar.gz $USER@$IP:"$REMOTE_RELEASE/dist.tar.gz"
# 4. 远程:解压 → 校验 → 补齐旧资源 → 原子切换 → 健康检查 → 清理
ssh $SSH_OPTS $USER@$IP "RELEASE='$REMOTE_RELEASE' BASE='$BASE_PATH' KEEP=$KEEP_RELEASES HOST='$SITE_HOST' bash -s" << 'REMOTE_EOF'
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
REMOTE_EOF
echo "✨ 发布成功!当前版本: $RELEASE_NAME"