Optimize deploy script
- Zero-downtime PM2 reload instead of stop+start - Keep .next.prev backup until after health check - Add standalone static file verification - Remove duplicate sleep - Clean up naming
This commit is contained in:
1 parent
d9e95c823a
commit
9226558aa0
1 file changed
+15
-7
@@ -316,13 +316,22 @@ jobs:
|
|||||||
echo "ERROR: stage build produced no .next/" >&2
|
echo "ERROR: stage build produced no .next/" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
# Verify standalone static files exist (required by Next.js standalone mode).
|
||||||
|
STANDALONE_STATIC="${STAGE}/.next/standalone/.next/static"
|
||||||
|
if [ -d "${STANDALONE_STATIC}" ] && [ -z "$(ls -A "${STANDALONE_STATIC}" 2>/dev/null)" ]; then
|
||||||
|
echo "WARNING: standalone .next/static is empty — copying fresh..."
|
||||||
|
cp -r "${STAGE}/.next/static/." "${STANDALONE_STATIC}/"
|
||||||
|
elif [ ! -d "${STANDALONE_STATIC}" ]; then
|
||||||
|
echo "WARNING: standalone static dir missing — creating..."
|
||||||
|
mkdir -p "${STANDALONE_STATIC}"
|
||||||
|
cp -r "${STAGE}/.next/static/." "${STANDALONE_STATIC}/"
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Cutover: stop service (free DB connections), migrate, swap .next..."
|
echo "Cutover: stop service (free DB connections), migrate, swap .next..."
|
||||||
CUTOVER_STARTED=1
|
CUTOVER_STARTED=1
|
||||||
pm2 stop next || true
|
pm2 stop next || true
|
||||||
sleep 2
|
|
||||||
# Give MariaDB a moment to reclaim the live pool.
|
# Give MariaDB a moment to reclaim the live pool.
|
||||||
sleep 2
|
sleep 3
|
||||||
|
|
||||||
# Migrate only after live is stopped — avoids ER_CON_COUNT_ERROR while
|
# Migrate only after live is stopped — avoids ER_CON_COUNT_ERROR while
|
||||||
# the old process still holds DATABASE_POOL_SIZE connections.
|
# the old process still holds DATABASE_POOL_SIZE connections.
|
||||||
@@ -356,8 +365,7 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
echo "Verified live src/ matches HEAD"
|
echo "Verified live src/ matches HEAD"
|
||||||
|
|
||||||
# Atomic-ish artifact swap: keep previous .next until the new one is in place.
|
# Save current .next as backup before swapping (kept until health check passes).
|
||||||
rm -rf .next.prev
|
|
||||||
if [ -d .next ]; then
|
if [ -d .next ]; then
|
||||||
mv .next .next.prev
|
mv .next .next.prev
|
||||||
fi
|
fi
|
||||||
@@ -372,8 +380,8 @@ jobs:
|
|||||||
|
|
||||||
sudo chown -R www-data:www-data "${LIVE}"
|
sudo chown -R www-data:www-data "${LIVE}"
|
||||||
|
|
||||||
echo "Starting PM2..."
|
echo "Starting PM2 (zero-downtime reload)..."
|
||||||
pm2 start next --update-env || pm2 restart next --update-env
|
pm2 reload next --update-env || pm2 start next --update-env
|
||||||
|
|
||||||
sleep 3
|
sleep 3
|
||||||
if ! pm2 show next 2>/dev/null | grep -q 'online'; then
|
if ! pm2 show next 2>/dev/null | grep -q 'online'; then
|
||||||
@@ -402,7 +410,7 @@ jobs:
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Cleaning stage worktree and previous .next..."
|
echo "Cleaning stage worktree and previous .next backup..."
|
||||||
rm -rf "${LIVE}/.next.prev"
|
rm -rf "${LIVE}/.next.prev"
|
||||||
git -C "${LIVE}" worktree remove --force "${STAGE}" 2>/dev/null || rm -rf "${STAGE}" || true
|
git -C "${LIVE}" worktree remove --force "${STAGE}" 2>/dev/null || rm -rf "${STAGE}" || true
|
||||||
STAGE=""
|
STAGE=""
|
||||||
|
|||||||
Reference in new issue
Block a user