Files
plates-server/deploy-optimized.sh
2025-08-13 15:17:33 +08:00

105 lines
2.8 KiB
Bash
Executable File
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
# 优化版发布脚本 - 只上传源码,服务器端构建
SERVER_HOST="119.91.211.52"
SERVER_USER="root"
SERVER_PATH="/usr/local/web/love-tips-server"
# 定义颜色
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
print_message() {
echo -e "${GREEN}[INFO] $1${NC}"
}
print_warning() {
echo -e "${YELLOW}[WARN] $1${NC}"
}
print_error() {
echo -e "${RED}[ERROR] $1${NC}"
}
echo "🚀 开始优化部署流程..."
# 1. 测试SSH连接
print_message "测试SSH连接..."
if ! ssh -o ConnectTimeout=10 "$SERVER_USER@$SERVER_HOST" "echo 'SSH连接成功'" 2>/dev/null; then
print_error "无法连接到服务器请检查SSH配置"
exit 1
fi
# 2. 停止现有服务(如果存在)
print_message "停止现有服务..."
ssh "$SERVER_USER@$SERVER_HOST" "
cd $SERVER_PATH 2>/dev/null
if [ -f 'ecosystem.config.js' ]; then
pm2 stop ecosystem.config.js 2>/dev/null || true
pm2 delete ecosystem.config.js 2>/dev/null || true
echo '已停止现有服务'
fi
" 2>/dev/null
# 3. 备份现有部署
print_message "备份现有部署..."
ssh "$SERVER_USER@$SERVER_HOST" "
if [ -d '$SERVER_PATH' ] && [ -f '$SERVER_PATH/package.json' ]; then
backup_dir='$SERVER_PATH.backup.$(date +%Y%m%d_%H%M%S)'
cp -r '$SERVER_PATH' \$backup_dir
echo \"已备份到: \$backup_dir\"
fi
"
# 4. 创建目标目录
print_message "准备目标目录..."
ssh "$SERVER_USER@$SERVER_HOST" "mkdir -p $SERVER_PATH"
# 5. 同步源代码和配置文件(排除不必要的文件)
print_message "同步源代码到服务器..."
rsync -avz --delete \
--exclude='node_modules' \
--exclude='dist' \
--exclude='.git' \
--exclude='.env' \
--exclude='.env.local' \
--exclude='.env.*.local' \
--exclude='*.log' \
--exclude='logs' \
--exclude='.DS_Store' \
--exclude='coverage' \
--exclude='.nyc_output' \
./ "$SERVER_USER@$SERVER_HOST:$SERVER_PATH/"
if [ $? -ne 0 ]; then
print_error "文件同步失败"
exit 1
fi
print_message "文件同步完成"
# 6. 在服务器上执行start.sh它会处理依赖安装、构建和启动
print_message "在服务器上执行start.sh..."
ssh "$SERVER_USER@$SERVER_HOST" "
cd $SERVER_PATH
chmod +x start.sh
echo '开始执行start.sh...'
./start.sh
"
if [ $? -eq 0 ]; then
print_message ""
print_message "🎉 部署成功完成!"
print_message "服务器: $SERVER_HOST"
print_message "目录: $SERVER_PATH"
print_message ""
print_message "常用管理命令:"
print_message "查看状态: ssh $SERVER_USER@$SERVER_HOST 'cd $SERVER_PATH && pm2 status'"
print_message "查看日志: ssh $SERVER_USER@$SERVER_HOST 'cd $SERVER_PATH && pm2 logs'"
print_message "重启服务: ssh $SERVER_USER@$SERVER_HOST 'cd $SERVER_PATH && pm2 restart ecosystem.config.js'"
else
print_error "部署失败,请检查服务器日志"
exit 1
fi