chore(deploy): run next with pm2
This commit is contained in:
+50
-7
@@ -11,6 +11,7 @@ REPO_TOPLEVEL=""
|
|||||||
LOG_FILE=""
|
LOG_FILE=""
|
||||||
CURRENT_BRANCH=""
|
CURRENT_BRANCH=""
|
||||||
START_PORT=""
|
START_PORT=""
|
||||||
|
PM2_APP_NAME=""
|
||||||
BUILD_EXIT=0
|
BUILD_EXIT=0
|
||||||
STOP_WAIT_SECONDS=3
|
STOP_WAIT_SECONDS=3
|
||||||
STOP_VERIFY_RETRIES=3
|
STOP_VERIFY_RETRIES=3
|
||||||
@@ -80,16 +81,21 @@ select_port_by_branch() {
|
|||||||
case "$CURRENT_BRANCH" in
|
case "$CURRENT_BRANCH" in
|
||||||
main)
|
main)
|
||||||
START_PORT=9185
|
START_PORT=9185
|
||||||
|
PM2_APP_NAME="cozsweet-main"
|
||||||
;;
|
;;
|
||||||
test|dev)
|
test|dev)
|
||||||
START_PORT=9135
|
START_PORT=9135
|
||||||
|
PM2_APP_NAME="cozsweet-$CURRENT_BRANCH"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
START_PORT=9135
|
START_PORT=9135
|
||||||
|
PM2_APP_NAME="cozsweet-$CURRENT_BRANCH"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
export START_PORT
|
export START_PORT
|
||||||
|
export PM2_APP_NAME
|
||||||
echo "=== start port: $START_PORT ===" >> "$LOG_FILE"
|
echo "=== start port: $START_PORT ===" >> "$LOG_FILE"
|
||||||
|
echo "=== pm2 app: $PM2_APP_NAME ===" >> "$LOG_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@@ -181,11 +187,41 @@ stop_existing_next() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stop_existing_pm2() {
|
||||||
|
echo "=== stop existing pm2 app $PM2_APP_NAME @ $(date +%Y-%m-%dT%H:%M:%S%z) ===" >> "$LOG_FILE"
|
||||||
|
|
||||||
|
if ! command -v pm2 >/dev/null 2>&1; then
|
||||||
|
echo "=== pm2 command not found; install pm2 before deployment ===" >> "$LOG_FILE"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if pm2 describe "$PM2_APP_NAME" >/dev/null 2>&1; then
|
||||||
|
pm2 delete "$PM2_APP_NAME" >> "$LOG_FILE" 2>&1 || true
|
||||||
|
echo "=== pm2 app deleted: $PM2_APP_NAME ===" >> "$LOG_FILE"
|
||||||
|
else
|
||||||
|
echo "=== no pm2 app found: $PM2_APP_NAME ===" >> "$LOG_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "" >> "$LOG_FILE"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
launch_next() {
|
launch_next() {
|
||||||
echo "=== start LAUNCH port=$START_PORT @ $(date +%Y-%m-%dT%H:%M:%S%z) ===" >> "$LOG_FILE"
|
echo "=== pm2 LAUNCH app=$PM2_APP_NAME port=$START_PORT @ $(date +%Y-%m-%dT%H:%M:%S%z) ===" >> "$LOG_FILE"
|
||||||
PORT="$START_PORT" nohup pnpm run start >> "$LOG_FILE" 2>&1 &
|
|
||||||
START_PID=$!
|
export PORT="$START_PORT"
|
||||||
echo "=== start PID=$START_PID @ $(date +%Y-%m-%dT%H:%M:%S%z) ===" >> "$LOG_FILE"
|
pm2 start pnpm --name "$PM2_APP_NAME" -- run start >> "$LOG_FILE" 2>&1
|
||||||
|
PM2_EXIT=$?
|
||||||
|
echo "=== pm2 START EXIT_CODE=$PM2_EXIT @ $(date +%Y-%m-%dT%H:%M:%S%z) ===" >> "$LOG_FILE"
|
||||||
|
|
||||||
|
if [ "$PM2_EXIT" -ne 0 ]; then
|
||||||
|
echo "=== pm2 START FAILED ===" >> "$LOG_FILE"
|
||||||
|
return "$PM2_EXIT"
|
||||||
|
fi
|
||||||
|
|
||||||
|
pm2 save >> "$LOG_FILE" 2>&1 || true
|
||||||
|
echo "=== realtime logs: pm2 logs $PM2_APP_NAME ===" >> "$LOG_FILE"
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@@ -213,10 +249,17 @@ main() {
|
|||||||
exit $BUILD_EXIT # build 失败 → 不启 start(避免用旧 .next 跑新代码)
|
exit $BUILD_EXIT # build 失败 → 不启 start(避免用旧 .next 跑新代码)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! stop_existing_next; then
|
if ! stop_existing_pm2; then
|
||||||
exit 1 # 端口未释放 → 不启动新服务,避免 EADDRINUSE
|
exit 1 # pm2 不可用 → 不启动新服务
|
||||||
|
fi
|
||||||
|
|
||||||
|
# if ! stop_existing_next; then
|
||||||
|
# exit 1 # 端口未释放 → 不启动新服务,避免 EADDRINUSE
|
||||||
|
# fi
|
||||||
|
|
||||||
|
if ! launch_next; then
|
||||||
|
exit 1 # pm2 启动失败
|
||||||
fi
|
fi
|
||||||
launch_next # 再启新
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
|||||||
Reference in New Issue
Block a user