diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..c7a6005 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +# +# FuelAlert deploy script — run on the VPS from the project root: +# +# ./deploy.sh # deploy the latest main +# ./deploy.sh v0.1.3 # deploy a specific tag +# +# It puts the site in maintenance mode, updates the code, runs migrations and +# cache rebuilds, restarts the queue, then brings the site back up. composer +# install and npm build only run when their inputs actually changed, so most +# deploys skip them. If any step fails the script aborts and the site stays in +# maintenance mode on purpose — fix the issue, then re-run. +# +# See docs/ops/deployment.md for first-time setup and troubleshooting. +set -euo pipefail + +cd "$(dirname "$0")" + +REF="${1:-main}" +echo "==> Deploying ref: ${REF}" + +# Remember the current commit so we can see what changed after checkout. +BEFORE="$(git rev-parse HEAD)" + +echo "==> Maintenance mode on" +php artisan down --retry=15 + +git fetch --tags --prune origin +git checkout "${REF}" + +# Fast-forward to the remote only when on a branch (a tag leaves a detached HEAD). +if git symbolic-ref -q HEAD >/dev/null; then + git pull --ff-only +fi + +AFTER="$(git rev-parse HEAD)" +CHANGED="$(git diff --name-only "${BEFORE}" "${AFTER}" || true)" + +# Reinstall PHP deps only if the lockfile moved. +if grep -q '^composer\.lock$' <<<"${CHANGED}"; then + echo "==> composer.lock changed — installing PHP deps" + composer install --no-dev --optimize-autoloader +else + echo "==> composer.lock unchanged — skipping composer install" +fi + +# Rebuild the Vue SPA only if frontend sources or the JS lockfile moved. +if grep -qE '^(package(-lock)?\.json|vite\.config\.|resources/(js|css)/)' <<<"${CHANGED}"; then + echo "==> Frontend changed — rebuilding SPA" + npm ci + npm run build +else + echo "==> No frontend changes — skipping npm build" +fi + +echo "==> Running migrations" +php artisan migrate --force + +echo "==> Rebuilding caches" +php artisan config:cache +php artisan route:cache +php artisan view:cache +php artisan event:cache + +echo "==> Restarting queue workers" +php artisan queue:restart + +echo "==> Maintenance mode off" +php artisan up + +echo "==> Deploy complete" +php artisan about