#!/usr/bin/env bash # Updates this install to a tagged release: back up the database, check out the tag, # install, migrate, build, and reload PM2. If anything fails before the reload, the # previous version keeps running and the checkout goes back to it. # # Usage (on the server, from the repo folder): bash scripts/deploy.sh v1.34.0 # CI runs this over SSH when a v* tag is pushed (see .gitea/workflows/ci.yml). set -euo pipefail TAG="${1:?Usage: scripts/deploy.sh }" cd "$(dirname "$0")/.." PREVIOUS="$(git describe --tags --exact-match 2>/dev/null || git rev-parse --short HEAD)" echo "==> Deploying $TAG (currently $PREVIOUS)" echo "==> Backing up the database" (cd backend && node -e "require('dotenv').config(); require('./src/utils/backupUtils').runBackup().then(r => console.log(' ', r.filename)).catch(e => { console.error(' Backup failed:', e.message); process.exit(1); })") rollback() { echo "!!> Deploy failed — going back to $PREVIOUS (the running version was not touched)" git checkout -q "$PREVIOUS" (cd backend && npm ci --silent) || true (cd frontend && npm ci --silent && npm run build --silent) || true } trap rollback ERR git fetch --tags --quiet origin git checkout -q "$TAG" echo "==> Backend: install and migrate" (cd backend && npm ci && npm run prisma:deploy) echo "==> Frontend: install and build" (cd frontend && npm ci && npm run build) trap - ERR echo "==> Reloading PM2" pm2 reload ecosystem.config.js --update-env echo "==> Deployed $TAG"