Files
cozsweet-frontend-nextjs/.githooks/post-receive
T
admin c90d2a5139 chore(env): add AUTH_SECRET for next-auth across environments
- Add per-environment AUTH_SECRET (dev/test/prod) for next-auth v4 JWT signing
- Temporarily redirect next start stdout/stderr to LOG_FILE in post-receive hook for server error debugging
2026-06-12 18:42:43 +08:00

64 lines
2.6 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 立即返回)
# 准备生产环境变量(从 env-example/.env.production.example 复制 → .env.production
cp -f env-example/.env.production.example .env.production
# 日志文件路径(**覆盖**模式:每次推送都生成全新日志)
LOG_FILE="$(git rev-parse --show-toplevel)/logs/post-receive.log"
mkdir -p "$(dirname "$LOG_FILE")"
# 元信息头(**首**次写 → 覆盖)
{
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 进程
# **临**时**改**next 输**出**也**接**到 LOG_FILE**为**排**查** Server error
# —— 排**查**完**后**改回 `> /dev/null 2>&1`
echo "=== start LAUNCH @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" >> "$LOG_FILE"
nohup pnpm run start >> "$LOG_FILE" 2>&1 &
START_PID=$!
echo "=== start PID=$START_PID @ $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" >> "$LOG_FILE"
exit 0