Files
mini-game-ai/scripts/deploy.sh

113 lines
2.7 KiB
Bash
Executable File
Raw Permalink 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_USER="root"
SERVER_HOST="129.204.155.94"
SERVER_PATH="/usr/local/web/mini-game-ai"
PM2_APP_NAME="mini-game-ai"
# 颜色输出
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
echo_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
echo_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
echo_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# 检查 SSH 连接
echo_info "检查服务器连接..."
if ! ssh -o ConnectTimeout=5 ${SERVER_USER}@${SERVER_HOST} "echo '连接成功'" > /dev/null 2>&1; then
echo_error "无法连接到服务器 ${SERVER_HOST}"
echo_warn "请确保:"
echo_warn " 1. 服务器地址正确"
echo_warn " 2. 已配置 SSH 密钥认证或密码"
exit 1
fi
# 本地构建
echo_info "开始本地构建..."
npm run build
if [ $? -ne 0 ]; then
echo_error "构建失败,部署终止"
exit 1
fi
echo_info "构建完成"
# 创建服务器目录
echo_info "准备服务器目录..."
ssh ${SERVER_USER}@${SERVER_HOST} "mkdir -p ${SERVER_PATH}"
# 同步文件到服务器
echo_info "上传文件到服务器 ${SERVER_HOST}:${SERVER_PATH}"
rsync -avz --delete \
--exclude 'node_modules' \
--exclude '.git' \
--exclude 'public/uploads' \
--exclude '__pycache__' \
--exclude '*.pyc' \
--exclude '.DS_Store' \
--exclude '*.log' \
./ ${SERVER_USER}@${SERVER_HOST}:${SERVER_PATH}/
if [ $? -ne 0 ]; then
echo_error "文件上传失败"
exit 1
fi
echo_info "文件上传完成"
# 在服务器上安装依赖并重启服务
echo_info "服务器部署操作..."
ssh ${SERVER_USER}@${SERVER_HOST} "bash -l" << 'ENDSSH'
export PATH="$HOME/.nvm/versions/node/v22.17.1/bin:$PATH"
cd /usr/local/web/mini-game-ai
# 安装依赖
echo "安装依赖..."
npm config set registry https://registry.npmmirror.com
npm install --production --no-audit --no-fund
# 检查 PM2 是否安装
if ! command -v pm2 &> /dev/null; then
echo "安装 PM2..."
npm install -g pm2
fi
# 停止旧服务(如果存在)
if pm2 describe mini-game-ai &> /dev/null; then
echo "停止旧服务..."
pm2 stop mini-game-ai
pm2 delete mini-game-ai
fi
# 启动新服务
echo "启动服务(端口 3003..."
PORT=3003 pm2 start npm --name mini-game-ai -- start
# 保存 PM2 配置
pm2 save --force
# 等待服务启动并检查状态
sleep 3
echo "服务状态:"
pm2 status mini-game-ai
# 检查服务是否在线
if pm2 describe mini-game-ai | grep -q "online"; then
echo "✓ 服务启动成功!"
else
echo "✗ 服务启动失败,查看日志:"
pm2 logs mini-game-ai --lines 20 --nostream
exit 1
fi
ENDSSH
if [ $? -eq 0 ]; then
echo_info "部署成功!"
echo_info "应用地址: http://${SERVER_HOST}:3003"
else
echo_error "部署失败"
exit 1
fi