Files
dukang/deploy/remote-release.sh
T
jacy 7062aeb8f8
CI / verify (pull_request) Has been cancelled
fix(deploy): prevent mini-user H5 blank page under /user basename
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-15 18:57:33 +08:00

123 lines
4.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
# 在服务器上执行:拉取后的常规发版(不覆盖 .env.production、默认不 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. 安装依赖"
# mini-user H5Taro)构建峰值内存较高
export NODE_OPTIONS="${NODE_OPTIONS:---max-old-space-size=8192}"
# 生产部署 pathnginx 入口为 /user/
export TARO_H5_PUBLIC_PATH="${TARO_H5_PUBLIC_PATH:-/user/}"
export TARO_H5_ROUTER_BASENAME="${TARO_H5_ROUTER_BASENAME:-/user}"
pnpm approve-builds --all 2>/dev/null || true
pnpm install --frozen-lockfile 2>/dev/null || pnpm install
if [[ "$SKIP_DB" == false ]]; then
echo "==> 3. 数据库 schema 同步(构建前,读取 .env.production"
cd "$APP_ROOT/server/dukang-api"
if [[ ! -f .env.production ]]; then
echo "错误: 未找到 server/dukang-api/.env.production" >&2
exit 1
fi
node scripts/with-api-env.cjs pnpm prisma:generate
if [[ "$DB_PUSH_ACCEPT_DATA_LOSS" == true ]]; then
node scripts/with-api-env.cjs npx prisma db push --accept-data-loss
else
node scripts/with-api-env.cjs npx prisma db push
fi
if [[ "$RUN_SEED" == true ]]; then
echo "==> 执行 seed"
node scripts/with-api-env.cjs pnpm prisma:seed
fi
cd "$APP_ROOT"
else
echo "==> 3. 跳过数据库"
fi
if [[ "$SKIP_BUILD" == false ]]; then
echo "==> 4. 构建(C 端使用 mini-user H5,跳过 h5-user"
pnpm --filter @dukang/shared-types \
--filter @dukang/domain \
--filter @dukang/api \
--filter @dukang/mini-user \
--filter @dukang/h5-shop \
--filter @dukang/h5-partner \
--filter @dukang/admin-web \
build
# serve 读取 dist/serve.json(相对静态根目录)
if [[ -f "$APP_ROOT/apps/mini-user/serve.json" ]]; then
cp -f "$APP_ROOT/apps/mini-user/serve.json" "$APP_ROOT/apps/mini-user/dist/serve.json"
fi
else
echo "==> 4. 跳过构建"
if [[ -f "$APP_ROOT/apps/mini-user/serve.json" && -d "$APP_ROOT/apps/mini-user/dist" ]]; then
cp -f "$APP_ROOT/apps/mini-user/serve.json" "$APP_ROOT/apps/mini-user/dist/serve.json"
fi
fi
echo "==> 5. 重启 PM2"
if pm2 describe dukang-api &>/dev/null; then
# delete+start 以确保 cwd 从 h5-user 切到 mini-user
pm2 delete dukang-h5-user 2>/dev/null || true
pm2 start "$DEPLOY_DIR/ecosystem.config.cjs" --only dukang-h5-user
pm2 restart dukang-api dukang-h5-shop dukang-h5-partner dukang-admin-web 2>/dev/null \
|| pm2 restart dukang-api 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-4]' || true
curl -sf -o /dev/null -w "mini-user(h5): %{http_code}\n" http://127.0.0.1:8091/ || echo "mini-user(h5): 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 "==> 7. 记录系统版本"
cd "$APP_ROOT/server/dukang-api"
APP_ROOT="$APP_ROOT" DEPLOY_TRIGGER="${DEPLOY_TRIGGER:-manual}" \
node scripts/with-api-env.cjs node scripts/record-system-version.cjs \
|| echo "WARN: system_version 写入失败"
echo "==> 发版完成"