From 6605c770c0ed1d3baf60e3afaaf95d5824826414 Mon Sep 17 00:00:00 2001 From: chenhang Date: Thu, 18 Jun 2026 16:21:27 +0800 Subject: [PATCH 1/3] refactor(post-commit): simplify pre-release hook logic by removing unnecessary checks --- .githooks/post-commit | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/.githooks/post-commit b/.githooks/post-commit index 8d2eea5b..dd4044d7 100755 --- a/.githooks/post-commit +++ b/.githooks/post-commit @@ -5,19 +5,6 @@ set -eu SCRIPT_PATH="scripts/release/pre_release_web.sh" -if [ "${COZSWEET_SKIP_POST_COMMIT_PRE_RELEASE:-}" = "1" ]; then - echo "=== post-commit: skip pre-release hook (nested git operation) ===" - exit 0 -fi - -REPO_TOPLEVEL="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" -cd "$REPO_TOPLEVEL" - -if [ ! -f "$SCRIPT_PATH" ]; then - echo "=== post-commit: script not found: $SCRIPT_PATH ===" >&2 - exit 1 -fi - echo "=== post-commit: running $SCRIPT_PATH ===" export COZSWEET_SKIP_POST_COMMIT_PRE_RELEASE=1 bash "$SCRIPT_PATH" From 0bca7d4fa78ab12b0a3f17b75ebbc66d5566779a Mon Sep 17 00:00:00 2001 From: chenhang Date: Thu, 18 Jun 2026 16:33:29 +0800 Subject: [PATCH 2/3] chore(deploy): run next with pm2 --- .githooks/post-receive | 57 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/.githooks/post-receive b/.githooks/post-receive index f7b6e678..4a62a63b 100755 --- a/.githooks/post-receive +++ b/.githooks/post-receive @@ -11,6 +11,7 @@ REPO_TOPLEVEL="" LOG_FILE="" CURRENT_BRANCH="" START_PORT="" +PM2_APP_NAME="" BUILD_EXIT=0 STOP_WAIT_SECONDS=3 STOP_VERIFY_RETRIES=3 @@ -80,16 +81,21 @@ 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" } # ============================================================ @@ -181,11 +187,41 @@ 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 "=== 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" + 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 } # ============================================================ @@ -213,10 +249,17 @@ main() { exit $BUILD_EXIT # build 失败 → 不启 start(避免用旧 .next 跑新代码) fi - if ! stop_existing_next; then - exit 1 # 端口未释放 → 不启动新服务,避免 EADDRINUSE + 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 启动失败 fi - launch_next # 再启新 } main "$@" From 96d0a54105b11e75eecfa15ca815569faac32d51 Mon Sep 17 00:00:00 2001 From: chenhang Date: Thu, 18 Jun 2026 16:35:29 +0800 Subject: [PATCH 3/3] chore(git): simplify post commit release hook --- .githooks/post-commit | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.githooks/post-commit b/.githooks/post-commit index dd4044d7..a56502f2 100755 --- a/.githooks/post-commit +++ b/.githooks/post-commit @@ -3,8 +3,4 @@ set -eu -SCRIPT_PATH="scripts/release/pre_release_web.sh" - -echo "=== post-commit: running $SCRIPT_PATH ===" -export COZSWEET_SKIP_POST_COMMIT_PRE_RELEASE=1 -bash "$SCRIPT_PATH" +bash scripts/release/pre_release_web.sh