Files
cozsweet-frontend-nextjs/.githooks/post-receive
T
admin f72d1ce94f ci(deploy): branch-aware env copy and restore start output silencing
- Select env template per branch: main → .env.production, test/dev → .env.local
- Skip env copy on unknown branches with warning log
- Revert pnpm run start output to /dev/null (debugging complete)
- Reorder LOG_FILE definition before env-copy block so case branches can log
2026-06-12 18:53:30 +08:00

81 lines
3.2 KiB
Bash
Executable File
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.
#!/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 立即返回)
# 日志文件路径(**覆盖**模式:每次推送都生成全新日志)—— **先**定**义**给后续 echo 用
LOG_FILE="$(git rev-parse --show-toplevel)/logs/post-receive.log"
mkdir -p "$(dirname "$LOG_FILE")"
# **根**据**分**支选**择** env-example → .envtest 服**务**器**只**需 .env.local**不**碰 .env.production
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "=== branch: $CURRENT_BRANCH ===" >> "$LOG_FILE"
case "$CURRENT_BRANCH" in
main)
cp -f env-example/.env.production.example .env.production
echo "=== env: copied .env.production (main → prod) ===" >> "$LOG_FILE"
;;
test)
cp -f env-example/.env.local.example .env.local
echo "=== env: copied .env.local (test → test) ===" >> "$LOG_FILE"
;;
dev)
cp -f env-example/.env.development.example .env.local
echo "=== env: copied .env.local (dev → dev) ===" >> "$LOG_FILE"
;;
*)
echo "=== env: UNKNOWN branch '$CURRENT_BRANCH', skip env copy ===" >> "$LOG_FILE"
;;
esac
# 元信息头(**首**次写 → 覆盖)
{
echo "=== post-receive @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ==="
echo "branch: $(git rev-parse --abbrev-ref HEAD)"
echo "commit: $(git rev-parse --short HEAD)"
echo "cwd: $(pwd)"
echo ""
} > "$LOG_FILE"
# pnpm run build → 输出**丢**>/dev/null 2>&1),仅捕获退出码写日志
echo "=== build START @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" >> "$LOG_FILE"
pnpm run build > /dev/null 2>&1
BUILD_EXIT=$?
echo "=== build EXIT_CODE=$BUILD_EXIT @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
# build 失败 → **不**启 start(避免用旧 .next 跑新代码),立即退出
if [ $BUILD_EXIT -ne 0 ]; then
echo "=== ABORTED @ $(date -u +%Y-%m-%dT%H:%M:%SZ) (build failed, start skipped) ===" >> "$LOG_FILE"
exit $BUILD_EXIT
fi
# build 成功 → **先**关闭旧 next 进程(**避**免端口 EADDRINUSE 冲突,新代码**能**生效)
echo "=== stop existing next @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" >> "$LOG_FILE"
# 1) **优**雅**关**闭(SIGTERM)—— 旧进程**自**己 drain 现有请求
pkill -f "next start" 2>/dev/null
pkill -f "next-server" 2>/dev/null
# 2) 等 2s 让**优**雅**关**闭**完**成
sleep 2
# 3) 强杀(SIGKILL)—— **还**在**的**话
pkill -9 -f "next start" 2>/dev/null
pkill -9 -f "next-server" 2>/dev/null
# 4) **最**后等 1s 确**保**端口**完**全释放
sleep 1
echo "=== stop DONE @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
# pnpm run start → **后台**跑(nohup ... &),long-running 进程
# 输出**丢**> /dev/null 2>&1)—— 真实 next 日志要看 next 自身 stdout/stderr
echo "=== start LAUNCH @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" >> "$LOG_FILE"
nohup pnpm run start > /dev/null 2>&1 &
START_PID=$!
echo "=== start PID=$START_PID @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" >> "$LOG_FILE"
exit 0