diff --git a/.githooks/post-receive b/.githooks/post-receive index 300bc726..7d7f9893 100755 --- a/.githooks/post-receive +++ b/.githooks/post-receive @@ -18,7 +18,6 @@ REPO_TOPLEVEL="" LOG_FILE="" CURRENT_BRANCH="" START_PORT="" -PM2_APP_NAME="" BUILD_EXIT=0 STOP_WAIT_SECONDS=3 STOP_VERIFY_RETRIES=3 @@ -88,21 +87,16 @@ select_port_by_branch() { case "$CURRENT_BRANCH" in main) START_PORT=9185 - PM2_APP_NAME="cozsweet-main" ;; test|dev) START_PORT=9135 - PM2_APP_NAME="cozsweet-$CURRENT_BRANCH" ;; *) START_PORT=9135 - PM2_APP_NAME="cozsweet-$CURRENT_BRANCH" ;; esac export START_PORT - export PM2_APP_NAME echo "=== start port: $START_PORT ===" >> "$LOG_FILE" - echo "=== pm2 app: $PM2_APP_NAME ===" >> "$LOG_FILE" } # ============================================================ @@ -194,41 +188,11 @@ stop_existing_next() { return 0 } -stop_existing_pm2() { - echo "=== stop existing pm2 app $PM2_APP_NAME @ $(date +%Y-%m-%dT%H:%M:%S%z) ===" >> "$LOG_FILE" - - if ! command -v pm2 >/dev/null 2>&1; then - echo "=== pm2 command not found; install pm2 before deployment ===" >> "$LOG_FILE" - return 1 - fi - - if pm2 describe "$PM2_APP_NAME" >/dev/null 2>&1; then - pm2 delete "$PM2_APP_NAME" >> "$LOG_FILE" 2>&1 || true - echo "=== pm2 app deleted: $PM2_APP_NAME ===" >> "$LOG_FILE" - else - echo "=== no pm2 app found: $PM2_APP_NAME ===" >> "$LOG_FILE" - fi - - echo "" >> "$LOG_FILE" - return 0 -} - launch_next() { - echo "=== pm2 LAUNCH app=$PM2_APP_NAME port=$START_PORT @ $(date +%Y-%m-%dT%H:%M:%S%z) ===" >> "$LOG_FILE" - - export PORT="$START_PORT" - pm2 start pnpm --name "$PM2_APP_NAME" -- run start >> "$LOG_FILE" 2>&1 - PM2_EXIT=$? - echo "=== pm2 START EXIT_CODE=$PM2_EXIT @ $(date +%Y-%m-%dT%H:%M:%S%z) ===" >> "$LOG_FILE" - - if [ "$PM2_EXIT" -ne 0 ]; then - echo "=== pm2 START FAILED ===" >> "$LOG_FILE" - return "$PM2_EXIT" - fi - - pm2 save >> "$LOG_FILE" 2>&1 || true - echo "=== realtime logs: pm2 logs $PM2_APP_NAME ===" >> "$LOG_FILE" - return 0 + echo "=== start LAUNCH port=$START_PORT @ $(date +%Y-%m-%dT%H:%M:%S%z) ===" >> "$LOG_FILE" + PORT="$START_PORT" nohup pnpm run start >> "$LOG_FILE" 2>&1 & + START_PID=$! + echo "=== start PID=$START_PID @ $(date +%Y-%m-%dT%H:%M:%S%z) ===" >> "$LOG_FILE" } # ============================================================ @@ -256,17 +220,10 @@ main() { exit $BUILD_EXIT # build 失败 → 不启 start(避免用旧 .next 跑新代码) fi - if ! stop_existing_pm2; then - exit 1 # pm2 不可用 → 不启动新服务 - fi - - # if ! stop_existing_next; then - # exit 1 # 端口未释放 → 不启动新服务,避免 EADDRINUSE - # fi - - if ! launch_next; then - exit 1 # pm2 启动失败 + if ! stop_existing_next; then + exit 1 # 端口未释放 → 不启动新服务,避免 EADDRINUSE fi + launch_next # 再启新 } main "$@"