From 9226558aa08755a6d7cd657cd7956a91db2f13cb Mon Sep 17 00:00:00 2001 From: openhands Date: Thu, 23 Jul 2026 19:13:06 +0200 Subject: [PATCH] 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 --- .gitea/workflows/deploy.yaml | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/.gitea/workflows/deploy.yaml b/.gitea/workflows/deploy.yaml index c2e10a501a..b31cc13a1d 100644 --- a/.gitea/workflows/deploy.yaml +++ b/.gitea/workflows/deploy.yaml @@ -316,13 +316,22 @@ jobs: echo "ERROR: stage build produced no .next/" >&2 exit 1 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..." CUTOVER_STARTED=1 pm2 stop next || true - sleep 2 # 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 # the old process still holds DATABASE_POOL_SIZE connections. @@ -356,8 +365,7 @@ jobs: fi echo "Verified live src/ matches HEAD" - # Atomic-ish artifact swap: keep previous .next until the new one is in place. - rm -rf .next.prev + # Save current .next as backup before swapping (kept until health check passes). if [ -d .next ]; then mv .next .next.prev fi @@ -372,8 +380,8 @@ jobs: sudo chown -R www-data:www-data "${LIVE}" - echo "Starting PM2..." - pm2 start next --update-env || pm2 restart next --update-env + echo "Starting PM2 (zero-downtime reload)..." + pm2 reload next --update-env || pm2 start next --update-env sleep 3 if ! pm2 show next 2>/dev/null | grep -q 'online'; then @@ -402,7 +410,7 @@ jobs: exit 1 fi - echo "Cleaning stage worktree and previous .next..." + echo "Cleaning stage worktree and previous .next backup..." rm -rf "${LIVE}/.next.prev" git -C "${LIVE}" worktree remove --force "${STAGE}" 2>/dev/null || rm -rf "${STAGE}" || true STAGE=""