#!/bin/bash # 自动部署脚本 - 由 webhook 触发 # 用于 /data/ai/deploy.sh 的软链接 set -e DEPLOY_LOG="/tmp/deploy-auto.log" PROJECT_DIR="/data/ai/audio_codebuddy" BACKEND_PORT="3100" log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$DEPLOY_LOG" } log "========== 开始自动部署 ==========" # 切换到项目目录 cd "$PROJECT_DIR" # Git pull log "执行 git pull..." git pull origin master 2>&1 | tee -a "$DEPLOY_LOG" # 构建后端 log "构建后端..." cd "$PROJECT_DIR/server" npm run build 2>&1 | tee -a "$DEPLOY_LOG" # 安装依赖(如果 package.json 有变化) log "检查依赖..." npm install --production 2>&1 | tee -a "$DEPLOY_LOG" || true # 重启后端服务 log "重启后端服务..." # 使用 PM2 重启 if command -v pm2 &> /dev/null; then # 需要设置 SERVER_PORT=3100 pm2 delete server 2>/dev/null || true SERVER_PORT=3100 pm2 start dist/app.js --name server 2>&1 | tee -a "$DEPLOY_LOG" pm2 save 2>&1 | tee -a "$DEPLOY_LOG" else log "PM2 未安装,跳过进程管理" fi log "========== 部署完成 =========="