nex_buddy/scripts/deploy.sh

136 lines
3.9 KiB
Bash
Raw Normal View History

2026-11-13 06:38:19 +00:00
#!/usr/bin/env bash
# 服务器部署:scripts/deploy.sh [选项]
# 默认流程:创建/更新 venv → 安装依赖 → alembic upgrade → 构建 Web → 重启服务 → 健康检查。
#
# 选项:
# --no-web 不安装/构建前端
# --no-migrate 不执行数据库迁移
# --skip-install 跳过 Python 依赖安装(依赖没变时可加快部署)
# --no-restart 不重启 systemd 服务
# --init 首次部署:额外导入城市/星座码表与通用月度运势
set -euo pipefail
cd "$(dirname "$0")/.."
DO_WEB=1
DO_MIGRATE=1
DO_INSTALL=1
DO_RESTART=1
DO_INIT=0
usage() {
cat <<'EOF'
服务器部署:scripts/deploy.sh [选项]
默认流程:创建/更新 venv → 安装依赖 → alembic upgrade → 构建 Web → 重启服务 → 健康检查。
选项:
--no-web 不安装/构建前端
--no-migrate 不执行数据库迁移
--skip-install 跳过 Python 依赖安装(依赖没变时可加快部署)
--no-restart 不重启 systemd 服务
--init 首次部署:额外导入城市/星座码表与通用月度运势
EOF
}
for arg in "$@"; do
case "$arg" in
--no-web) DO_WEB=0 ;;
--no-migrate) DO_MIGRATE=0 ;;
--skip-install) DO_INSTALL=0 ;;
--no-restart) DO_RESTART=0 ;;
--init) DO_INIT=1 ;;
-h|--help) usage; exit 0 ;;
*) echo "未知参数:$arg(用 --help 查看用法)" >&2; exit 2 ;;
esac
done
if [ ! -f .env ]; then
2026-11-13 14:19:35 +00:00
echo "!! 缺少 .env:先执行 cp .env.example .env,并填写 DATABASE_URL / REDIS_URL" >&2
2026-11-13 06:38:19 +00:00
exit 1
fi
PYTHON="${PYTHON:-python3}"
if ! command -v "$PYTHON" >/dev/null 2>&1; then
echo "!! 找不到 $PYTHON,请先安装 Python 3.12+" >&2
exit 1
fi
if [ ! -x .venv/bin/python ]; then
echo ">> 创建虚拟环境"
"$PYTHON" -m venv .venv
fi
if [ "$DO_INSTALL" = 1 ]; then
echo ">> 安装 Python 依赖"
.venv/bin/python -m pip install --upgrade pip
.venv/bin/python -m pip install -r requirements.txt
fi
if [ "$DO_MIGRATE" = 1 ]; then
echo ">> 数据库迁移"
(cd server && ../.venv/bin/python -m alembic upgrade head)
fi
if [ "$DO_INIT" = 1 ]; then
echo ">> 初始化码表与运势"
(
cd server
../.venv/bin/python scripts/seed_catalog.py
../.venv/bin/python scripts/seed_fortunes.py
)
fi
if [ "$DO_WEB" = 1 ]; then
if ! command -v npm >/dev/null 2>&1; then
echo "!! 找不到 npm,构建前端需要 Node.js 18+" >&2
exit 1
fi
echo ">> 安装并构建 Web"
(
cd web
if [ -f package-lock.json ]; then
npm ci
else
npm install
fi
npm run build
)
fi
PORT="$(grep -E '^BACKEND_PORT=' .env 2>/dev/null | tail -1 | cut -d= -f2 | tr -d '[:space:]' || true)"
2026-11-13 08:51:05 +00:00
PORT="${PORT:-8000}"
2026-11-13 06:38:19 +00:00
SERVICE_NAME="${SERVICE_NAME:-nex-buddy}"
STARTED=0
if [ "$DO_RESTART" = 1 ]; then
if [ -f "/etc/systemd/system/${SERVICE_NAME}.service" ] && command -v systemctl >/dev/null 2>&1; then
echo ">> 重启服务 ${SERVICE_NAME}"
if [ "$(id -u)" -eq 0 ]; then
systemctl restart "$SERVICE_NAME"
else
sudo systemctl restart "$SERVICE_NAME"
fi
STARTED=1
elif [ -f "/etc/systemd/system/${SERVICE_NAME}.service" ]; then
echo "!! 已安装 ${SERVICE_NAME}.service,但当前环境没有 systemctl,跳过重启" >&2
else
echo ">> 未安装 systemd 单元 ${SERVICE_NAME}.service,跳过重启"
echo " 首次部署:sudo cp scripts/nex-buddy.service /etc/systemd/system/${SERVICE_NAME}.service"
echo " 然后按实际路径/用户修改 WorkingDirectory、User,再 systemctl enable --now ${SERVICE_NAME}"
fi
fi
if [ "$STARTED" = 1 ] && command -v curl >/dev/null 2>&1; then
echo ">> 健康检查 http://127.0.0.1:${PORT}/healthz"
for _ in $(seq 1 20); do
if out="$(curl -fsS "http://127.0.0.1:${PORT}/healthz" 2>/dev/null)"; then
echo "$out"
exit 0
fi
sleep 1
done
echo "!! 健康检查失败:确认服务已启动、端口 ${PORT} 正确" >&2
exit 1
fi
echo ">> 部署完成"