WEB/deploy.sh
2026-03-28 17:55:54 +08:00

37 lines
1.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

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
# --- 配置 ---
USER="webdev"
IP="112.124.21.102" # 服务器 IP
BASE_PATH="/www/wwwroot/web" # 请修改为您的项目在服务器上的存放路径
RELEASE_NAME="v$(date +'%Y%m%d_%H%M%S')" # 生成版本号:如 v20260328_1445
LOCAL_DIST="./dist"
# SSH 密钥提示:请确保本地已将 id_ed25519.pub 添加到服务器的 ~/.ssh/authorized_keys
# 密钥内容:ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJtwa+MFLQEgkEbcGQ6dkQOiFydRvjVeidJ/7uBsF2e 3313488199@qq.com
echo "🚀 开始发布版本: $RELEASE_NAME"
# 1. 本地打包
npm run build
# 2. 压缩并上传
tar -czf dist.tar.gz -C $LOCAL_DIST .
ssh $USER@$IP "mkdir -p $BASE_PATH/releases/$RELEASE_NAME"
scp dist.tar.gz $USER@$IP:$BASE_PATH/releases/$RELEASE_NAME
# 3. 远程解压、更新软链接、清理旧版本
ssh $USER@$IP << EOF
cd $BASE_PATH/releases/$RELEASE_NAME
tar -xzf dist.tar.gz
rm dist.tar.gz
# 原子化更新软链接:先创建临时链接再重命名(防止切换瞬间服务中断)
ln -sfn $BASE_PATH/releases/$RELEASE_NAME $BASE_PATH/current_new
mv -Tf $BASE_PATH/current_new $BASE_PATH/current
# 只保留最近 5 个版本,删除更旧的
cd $BASE_PATH/releases && ls -t | tail -n +6 | xargs rm -rf
EOF
echo "✨ 发布成功!当前版本: $RELEASE_NAME"