Files
openclaw-market/scripts/deploy.sh
richarjiang fa4c458eda init
2026-03-13 11:00:01 +08:00

119 lines
2.9 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/openclaw-market"
PM2_APP_NAME="openclaw-market"
PORT=3003
# 颜色输出
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 "开始本地构建..."
pnpm 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 '.claude' \
--exclude '.env.local' \
--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 ${SERVER_PATH}
# 安装 pnpm如果未安装
if ! command -v pnpm &> /dev/null; then
echo "安装 pnpm..."
npm install -g pnpm
fi
# 安装依赖
echo "安装依赖..."
npm config set registry https://registry.npmmirror.com
pnpm install --frozen-lockfile --prod
# 检查 PM2 是否安装
if ! command -v pm2 &> /dev/null; then
echo "安装 PM2..."
npm install -g pm2
fi
# 停止旧服务(如果存在)
if pm2 describe ${PM2_APP_NAME} &> /dev/null; then
echo "停止旧服务..."
pm2 stop ${PM2_APP_NAME}
pm2 delete ${PM2_APP_NAME}
fi
# 启动新服务
echo "启动服务(端口 ${PORT}..."
PORT=${PORT} pm2 start ./node_modules/next/dist/bin/next --name ${PM2_APP_NAME} -- start -p ${PORT}
# 保存 PM2 配置
pm2 save --force
# 等待服务启动并检查状态
sleep 3
echo "服务状态:"
pm2 status ${PM2_APP_NAME}
# 检查服务是否在线
if pm2 describe ${PM2_APP_NAME} | grep -q "online"; then
echo "✓ 服务启动成功!"
else
echo "✗ 服务启动失败,查看日志:"
pm2 logs ${PM2_APP_NAME} --lines 20 --nostream
exit 1
fi
ENDSSH
if [ $? -eq 0 ]; then
echo_info "部署成功!"
echo_info "应用地址: http://${SERVER_HOST}:${PORT}"
else
echo_error "部署失败"
exit 1
fi