From 664ff67ee913de7f3495de50c5773048db145f1d Mon Sep 17 00:00:00 2001 From: chenhang Date: Mon, 15 Jun 2026 18:44:35 +0800 Subject: [PATCH] chore(githooks): simplify post-receive hook and overwrite server log Simplify the repo toplevel detection logic and remove verbose comments from the post-receive hook. Switch next-server.log from append mode to overwrite mode so each deploy starts with a fresh log file. --- .githooks/post-receive | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/.githooks/post-receive b/.githooks/post-receive index 6069c739..06bc22a4 100755 --- a/.githooks/post-receive +++ b/.githooks/post-receive @@ -1,41 +1,20 @@ #!/bin/sh # Post-receive hook: push 后自动 build + start # -# 触发场景: -# - 非 bare repo + receive.denyCurrentBranch=updateInstead:本地推送自动部署 -# -# 用途:执行 package.json 中的 `build` + `start` 脚本 -# - 实际调用 `pnpm run build`(输出丢,仅记退出码) -# - 再后台启 `pnpm run start`(nohup,输出丢,hook 立即返回) -# -# 设计:逻辑块**封**装为独立函数,main 调**度**(shell 风格) -# - **全**局变量:LOG_FILE(在 init_log_file **里**定**义**)、CURRENT_BRANCH -# - **函**数**间**通过 `$LOG_FILE` / `$BUILD_EXIT` 通信 -# - **不**用 set -e —— 各步**自**己处理错**误**(env 不**存**在 / build 失败 / 旧**进**程**不**存**在)→ 让 deploy 错**误****不**挡 git push - -# git-receive-pack 调 hook 时 CWD **可**能是 `.git/` 内**部**, -# `git rev-parse --show-toplevel` 会**报**错 `must be run in a work tree`。 -# 解**法**:**用** `git rev-parse --absolute-git-dir` **取**得**绝**对 .git **路**径, -# 然后 dirname **得**到 repo **顶**层。**这**两个调**用**在 .git/ 内**部**也都**能**成**功**。 -GIT_DIR_ABS="$(git rev-parse --absolute-git-dir 2>/dev/null || true)" -if [ -n "$GIT_DIR_ABS" ] && [ "$GIT_DIR_ABS" != ".git" ]; then - REPO_TOPLEVEL="$(cd "$GIT_DIR_ABS/.." && pwd)" -else - REPO_TOPLEVEL="$PWD" -fi -cd "$REPO_TOPLEVEL" || { echo "FATAL: cannot cd to $REPO_TOPLEVEL" >&2; exit 1; } - -# 同 .bashrc **里**的 NVM **加**载逻辑 +# pnpm 环境变量 export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion +REPO_TOPLEVEL="$(git rev-parse --show-toplevel 2>/dev/null || echo "$PWD")" +cd "$REPO_TOPLEVEL" || { echo "FATAL: cannot cd to $REPO_TOPLEVEL" >&2; exit 1; } + # ============================================================ # 1. init_log_file —— 定**义** LOG_FILE + 创**建** logs/ 目**录** # ============================================================ init_log_file() { - LOG_FILE="$(git rev-parse --show-toplevel)/logs/post-receive.log" + LOG_FILE="$REPO_TOPLEVEL/logs/post-receive.log" mkdir -p "$(dirname "$LOG_FILE")" } @@ -116,10 +95,10 @@ stop_existing_next() { # 6. launch_next —— 后**台**启 `pnpm run start`,捕 PID # ============================================================ launch_next() { - # next stdout/stderr 接**到** logs/next-server.log(**追**加**模式**,**保**留**历**史** log **用**于排**查**) + # next stdout/stderr 接**到** logs/next-server.log(**覆**盖**模式**,**每**次 deploy **重**新**开**始) NEXT_LOG_FILE="$REPO_TOPLEVEL/logs/next-server.log" echo "=== start LAUNCH @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" >> "$LOG_FILE" - nohup pnpm run start >> "$NEXT_LOG_FILE" 2>&1 & + nohup pnpm run start > "$NEXT_LOG_FILE" 2>&1 & START_PID=$! echo "=== start PID=$START_PID @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" >> "$LOG_FILE" }