Files
hometown/run.sh
2026-03-08 17:23:00 +08:00

75 lines
3.0 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.
#!/usr/bin/env bash
# 部署流程:拉代码 → 安装依赖 → 构建前端到 dist/ →(可选)同步到 Nginx root → 后台启动 API5599
# 若 Nginx 未反代 /api请先设置 API_BASE_URL 再执行,例如:
# export API_BASE_URL="http://你的域名或IP:5599"
# ./run.sh
# 使用 pm2 保活推荐export USE_PM2=1 && ./run.sh
# 若项目在子目录、Nginx root 指向固定目录,构建后同步到该目录,例如:
# export DEPLOY_DIST=/www/wwwroot/view.airtep.com/dist
# ./run.sh
set -e
cd "$(dirname "$0")"
API_PORT="${PORT:-5599}"
export PORT="$API_PORT"
git fetch origin && git reset --hard origin/master
npm install --no-audit --no-fund
# 若设置了 API_BASE_URL则写入 api.config.json供 build 复制到 dist/
if [ -n "$API_BASE_URL" ]; then
echo "[run.sh] 使用 API_BASE_URL=$API_BASE_URL"
node -e "require('fs').writeFileSync('api.config.json', JSON.stringify({ apiBase: process.env.API_BASE_URL }, null, 2))"
fi
node build.js
# 若设置了 DEPLOY_DIST与 Nginx root 一致),将 dist/ 同步到该目录以便访问
if [ -n "$DEPLOY_DIST" ]; then
mkdir -p "$DEPLOY_DIST"
if command -v rsync >/dev/null 2>&1; then
rsync -a --delete dist/ "$DEPLOY_DIST/"
else
(cd dist && find . -mindepth 1 -maxdepth 1 -exec cp -r {} "$DEPLOY_DIST/" \;)
fi
echo "[run.sh] 已同步 dist/ 到 $DEPLOY_DIST"
fi
# 释放端口:避免 EADDRINUSE上次进程未退出或多次执行 run.sh
_kill_port() {
if command -v lsof >/dev/null 2>&1; then
pids=$(lsof -t -i ":$API_PORT" 2>/dev/null) || true
if [ -n "$pids" ]; then
echo "[run.sh] 停止占用端口 ${API_PORT} 的进程: $pids"
echo "$pids" | xargs kill 2>/dev/null || true
sleep 1
fi
elif command -v fuser >/dev/null 2>&1; then
if fuser -n tcp "$API_PORT" >/dev/null 2>&1; then
echo "[run.sh] 停止占用端口 ${API_PORT} 的进程..."
fuser -k "$API_PORT/tcp" 2>/dev/null || true
sleep 1
fi
fi
}
_kill_port
# 后台启动 API优先 pm2否则 nohup
if [ -n "$USE_PM2" ] && command -v pm2 >/dev/null 2>&1; then
echo "[run.sh] 使用 pm2 启动 API端口 ${API_PORT}..."
(cd server && npm install --no-audit --no-fund --silent && pm2 delete view-airtep-api 2>/dev/null; true)
(cd server && pm2 start index.js --name view-airtep-api)
pm2 save 2>/dev/null || true
echo "[run.sh] API 已由 pm2 托管。查看: pm2 list / pm2 logs view-airtep-api"
elif command -v nohup >/dev/null 2>&1; then
echo "[run.sh] 使用 nohup 后台启动 API端口 ${API_PORT}..."
mkdir -p logs
nohup bash start-api.sh >> logs/api.log 2>&1 &
echo "[run.sh] API 已在后台运行。查看日志: tail -f logs/api.log"
else
echo "[run.sh] 前台启动 APICtrl+C 会停止服务)..."
exec bash start-api.sh
fi
echo "[run.sh] 部署完成。前端静态在 dist/API 端口 ${API_PORT}"
echo "[run.sh] 检查服务: curl -s http://127.0.0.1:${API_PORT}/config 或 ./check-service.sh"