Files
cozsweet-frontend-nextjs/git_hooks/post-receive
T

48 lines
1.9 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
# 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