Files
dukang/deploy/remote-release.sh
T
2026-07-03 12:57:01 +08:00

90 lines
2.4 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
# 在服务器上执行:拉取后的常规发版(不覆盖 .env、默认不 seed)
set -euo pipefail
APP_ROOT="${APP_ROOT:-/opt/dukang-haoke}"
DEPLOY_DIR="$APP_ROOT/deploy"
SKIP_BUILD=false
SKIP_DB=false
RUN_SEED=false
DB_PUSH_ACCEPT_DATA_LOSS=false
usage() {
cat <<'EOF'
用法: remote-release.sh [选项]
--skip-build 跳过 pnpm build(仅重启 PM2
--skip-db 跳过 Prisma generate / db push
--seed 执行 prisma:seed(默认不执行)
--accept-data-loss prisma db push 时允许数据丢失(慎用)
-h, --help 显示帮助
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--skip-build) SKIP_BUILD=true ;;
--skip-db) SKIP_DB=true ;;
--seed) RUN_SEED=true ;;
--accept-data-loss) DB_PUSH_ACCEPT_DATA_LOSS=true ;;
-h|--help) usage; exit 0 ;;
*) echo "未知参数: $1" >&2; usage; exit 1 ;;
esac
shift
done
cd "$APP_ROOT"
echo "==> 1. 启用 pnpm"
corepack enable 2>/dev/null || true
corepack prepare pnpm@11.2.2 --activate 2>/dev/null || true
echo "==> 2. 安装依赖"
export NODE_OPTIONS="${NODE_OPTIONS:---max-old-space-size=2048}"
pnpm install --frozen-lockfile 2>/dev/null || pnpm install
if [[ "$SKIP_BUILD" == false ]]; then
echo "==> 3. 构建"
pnpm build
else
echo "==> 3. 跳过构建"
fi
if [[ "$SKIP_DB" == false ]]; then
echo "==> 4. 数据库 schema 同步"
cd "$APP_ROOT/server/dukang-api"
pnpm prisma:generate
if [[ "$DB_PUSH_ACCEPT_DATA_LOSS" == true ]]; then
npx prisma db push --accept-data-loss
else
npx prisma db push
fi
if [[ "$RUN_SEED" == true ]]; then
echo "==> 执行 seed"
pnpm prisma:seed
fi
else
echo "==> 4. 跳过数据库"
fi
echo "==> 5. 重启 PM2"
if pm2 describe dukang-api &>/dev/null; then
pm2 restart dukang-api dukang-h5-user dukang-h5-shop dukang-h5-partner dukang-admin-web 2>/dev/null \
|| pm2 restart dukang-api dukang-h5-user dukang-h5-shop dukang-h5-partner
else
pm2 start "$DEPLOY_DIR/ecosystem.config.cjs"
fi
pm2 save
echo "==> 6. 健康检查"
sleep 2
pm2 list
ss -tlnp | grep -E '809[0-3]' || true
curl -sf -o /dev/null -w "h5-user: %{http_code}\n" http://127.0.0.1:8091/ || echo "h5-user: FAIL"
curl -sf -o /dev/null -w "api: %{http_code}\n" http://127.0.0.1:8090/api/v1/health 2>/dev/null \
|| curl -sf -o /dev/null -w "api: %{http_code}\n" http://127.0.0.1:8090/ \
|| echo "api: FAIL"
echo "==> 发版完成"