fix(hook): update post-receive script to handle build and start processes with logging
This commit is contained in:
+37
-4
@@ -4,11 +4,44 @@
|
|||||||
# 触发场景:
|
# 触发场景:
|
||||||
# - 非 bare repo + receive.denyCurrentBranch=updateInstead:本地推送自动部署
|
# - 非 bare repo + receive.denyCurrentBranch=updateInstead:本地推送自动部署
|
||||||
#
|
#
|
||||||
# 用途:执行 package.json 中的 `build:start` 脚本
|
# 用途:执行 package.json 中的 `build` + `start` 脚本
|
||||||
# - 内部 = `npm run build && npm run start`
|
# - 实际调用 `pnpm run build`(输出丢,仅记退出码)
|
||||||
# - 实际调用 `pnpm run build:start`(项目使用 pnpm)
|
# - 再后台启 `pnpm run start`(nohup,输出丢,hook 立即返回)
|
||||||
|
|
||||||
# 准备生产环境变量(从 env-example/.env.production.example 复制 → .env.production)
|
# 准备生产环境变量(从 env-example/.env.production.example 复制 → .env.production)
|
||||||
cp -f env-example/.env.production.example .env.production
|
cp -f env-example/.env.production.example .env.production
|
||||||
|
|
||||||
pnpm run build:start
|
# 日志文件路径(**覆盖**模式:每次推送都生成全新日志)
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user