Deploy script with rollback (1.1.2)

scripts/deploy.sh backs up the database (pg_dump), checks out the release,
runs npm ci, prisma migrate deploy and generate, builds the frontend,
restarts the PM2 apps and health-checks them. Any failure puts back the
previous code and build, and restores the dump if new migrations had run.
deployment.md §12 now uses it. tsconfig.tsbuildinfo leaves git so builds
don't leave the tree dirty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-27 23:55:46 +02:00
co-authored by Claude Opus 5.5
parent e4a19f90dc
commit 5a90c713d8
10 changed files with 168 additions and 18 deletions
+1
View File
@@ -0,0 +1 @@
*.sh text eol=lf
+1
View File
@@ -8,3 +8,4 @@ frontend/.next/
frontend/next-env.d.ts
*.log
coverage/
*.tsbuildinfo
+13
View File
@@ -5,6 +5,19 @@ versions follow [Semantic Versioning](https://semver.org/).
## [Unreleased]
## [1.1.2] - 2026-10-27
### Added
- **`scripts/deploy.sh`** to update the Hub on the server: backs up the database, checks out the
release, installs, migrates, builds, restarts PM2 and checks both apps answer. If any step
fails it goes back to the previous version, restoring the database if new migrations had run.
See `deployment.md` §12.
### Changed
- `frontend/tsconfig.tsbuildinfo` is no longer kept in git (builds change it).
## [1.1.1] - 2026-10-27
### Fixed
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "events-hub-backend",
"version": "1.1.1",
"version": "1.1.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "events-hub-backend",
"version": "1.1.1",
"version": "1.1.2",
"hasInstallScript": true,
"dependencies": {
"@prisma/adapter-pg": "^7.10.1",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "events-hub-backend",
"version": "1.1.1",
"version": "1.1.2",
"private": true,
"description": "Cross Events Hub — API for managing Cross Events installs, licences and heartbeats",
"main": "src/index.js",
+32 -11
View File
@@ -331,19 +331,40 @@ To restore a dump: `pg_restore --clean --no-owner -d "<DATABASE_URL>" events_hub
```bash
sudo -iu <siteuser>
cd /var/www/<siteuser>/data/www/events-hub.crosscode.co.za/app
git pull
cd backend
npm ci
npx prisma migrate deploy && npx prisma generate
pm2 restart events-hub-backend
cd ../frontend
npm ci
npm run build
pm2 restart events-hub-frontend
bash scripts/deploy.sh # the latest main
bash scripts/deploy.sh v1.2.0 # or a tag / commit
```
The script:
1. Refuses to start if tracked files have local changes (it would overwrite them).
2. Backs up the database with `pg_dump` to `~/backups/events-hub/` (the newest 10 are kept).
3. Checks out the release (`main` as a detached `origin/main`).
4. Backend: `npm ci`, `prisma migrate deploy`, `prisma generate`.
5. Frontend: `npm ci`, `npm run build`.
6. `pm2 restart events-hub-backend events-hub-frontend`, then waits up to 30 seconds each for
`/api/health` on the backend and the frontend's home page to answer.
If any step fails, it goes back to the version that was running: the old code, dependencies
and build, a PM2 restart if it had already restarted, and **a restore of the database backup if
the release brought new migrations**. A restore drops anything written since the backup (usually
a few heartbeats in those minutes). If the restore itself fails, the script prints the
`pg_restore` command to run by hand.
Different backup folder or PM2 names? Set `HUB_BACKUP_DIR`, `HUB_PM2_BACKEND`,
`HUB_PM2_FRONTEND` when running it. It needs `pg_dump`/`pg_restore` and `curl` on the server.
**The first time only**, the script isn't on the server yet, and the clone is on the `main`
branch. Get it with:
```bash
git checkout -- frontend/tsconfig.tsbuildinfo # a build file no longer kept in git
git pull
bash scripts/deploy.sh
```
From then on the checkout is detached, so use the script rather than `git pull`.
Installs keep working while the Hub is briefly down (licences are valid offline for 30 days).
---
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "events-hub-frontend",
"version": "1.1.1",
"version": "1.1.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "events-hub-frontend",
"version": "1.1.1",
"version": "1.1.2",
"dependencies": {
"lucide-react": "^0.536.0",
"next": "^15.5.24",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "events-hub-frontend",
"version": "1.1.1",
"version": "1.1.2",
"private": true,
"scripts": {
"dev": "next dev -p 3200",
File diff suppressed because one or more lines are too long
+115
View File
@@ -0,0 +1,115 @@
#!/usr/bin/env bash
# Updates the Hub: back up the database, check out the release, install, migrate, build,
# restart PM2 and check it answers. If any step fails, it goes back to the previous version:
# the code, its dependencies and build, and the database too if new migrations had run.
#
# Usage (on the server, as the site user, from the repo folder):
# bash scripts/deploy.sh # the latest main
# bash scripts/deploy.sh v1.2.0 # a tag (or any commit)
#
# Optional settings (environment variables):
# HUB_BACKUP_DIR where database dumps go (default ~/backups/events-hub; the newest 10 are kept)
# HUB_PM2_BACKEND PM2 name of the backend (default events-hub-backend)
# HUB_PM2_FRONTEND PM2 name of the frontend (default events-hub-frontend)
set -Eeuo pipefail
# The checkout below replaces this file, and bash reads a script as it runs — so run from a
# copy that can't change underneath us.
if [ -z "${HUB_DEPLOY_COPY:-}" ]; then
COPY="$(mktemp)"
cp "$0" "$COPY"
HUB_DEPLOY_COPY=1 HUB_DEPLOY_DIR="$(cd "$(dirname "$0")/.." && pwd)" exec bash "$COPY" "$@"
fi
trap 'rm -f "$0"' EXIT
cd "$HUB_DEPLOY_DIR"
TARGET="${1:-main}"
BACKUP_DIR="${HUB_BACKUP_DIR:-$HOME/backups/events-hub}"
PM2_BACKEND="${HUB_PM2_BACKEND:-events-hub-backend}"
PM2_FRONTEND="${HUB_PM2_FRONTEND:-events-hub-frontend}"
FRONTEND_PORT=3200 # matches "start" in frontend/package.json
describe() { git describe --tags --exact-match 2>/dev/null || git rev-parse --short HEAD; }
PREVIOUS="$(git rev-parse HEAD)"
PREVIOUS_NAME="$(describe)"
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
echo "!!> There are local changes to tracked files; deploy would overwrite them:"
git status --short --untracked-files=no
exit 1
fi
# The database URL from backend/.env, without Prisma-only options (pg_dump rejects ?schema=).
DB_URL="$(cd backend && node -e "require('dotenv').config(); const u = new URL(process.env.DATABASE_URL); u.search = ''; console.log(u.toString())")"
BACKEND_PORT="$(cd backend && node -e "require('dotenv').config(); console.log(process.env.PORT || 5200)")"
echo "==> Deploying $TARGET (currently $PREVIOUS_NAME)"
echo "==> Backing up the database"
mkdir -p "$BACKUP_DIR"
BACKUP="$BACKUP_DIR/events_hub-$(date +%Y%m%d-%H%M%S)-$PREVIOUS_NAME.dump"
pg_dump --dbname="$DB_URL" -Fc -f "$BACKUP"
echo " $BACKUP"
ls -1t "$BACKUP_DIR"/events_hub-*.dump | tail -n +11 | xargs -r rm -f
MIGRATED=0
RESTARTED=0
rollback() {
# set -E passes this trap into ( … ) subshells too; only the script itself rolls back.
if [ "$BASH_SUBSHELL" -gt 0 ]; then return; fi
trap - ERR
set +e
echo "!!> Deploy failed — going back to $PREVIOUS_NAME"
git checkout -q -- . 2>/dev/null
git checkout -q "$PREVIOUS"
if [ "$MIGRATED" = 1 ]; then
echo "!!> Restoring the database from $BACKUP"
pg_restore --clean --if-exists --no-owner --single-transaction --dbname="$DB_URL" "$BACKUP" \
|| echo "!!> Restore failed. Restore by hand: pg_restore --clean --if-exists --no-owner -d \"<DATABASE_URL>\" $BACKUP"
fi
(cd backend && npm ci --silent)
(cd frontend && npm ci --silent && npm run build --silent)
if [ "$RESTARTED" = 1 ]; then pm2 restart "$PM2_BACKEND" "$PM2_FRONTEND" --update-env; fi
echo "!!> Back on $PREVIOUS_NAME. Look at the output above for what failed."
exit 1
}
trap rollback ERR
# A tag or commit as is; main as the latest origin/main (detached, so a local main is never
# in the way).
if [ "$TARGET" = "main" ]; then
git fetch --quiet origin main
git checkout -q --detach origin/main
else
git fetch --tags --quiet origin
git checkout -q "$TARGET"
fi
DEPLOYED="$(describe)"
echo "==> Backend: install, migrate and generate"
(cd backend && npm ci)
if [ -n "$(git diff --name-only "$PREVIOUS" HEAD -- backend/prisma/migrations)" ]; then MIGRATED=1; fi
(cd backend && npm run prisma:deploy)
echo "==> Frontend: install and build"
(cd frontend && npm ci && npm run build)
echo "==> Restarting PM2"
RESTARTED=1
pm2 restart "$PM2_BACKEND" "$PM2_FRONTEND" --update-env
echo "==> Checking it answers"
check() {
for _ in $(seq 1 30); do
if curl -fsS -o /dev/null "$1"; then return 0; fi
sleep 1
done
echo "!!> No answer from $1 after 30 seconds"
return 1
}
check "http://127.0.0.1:$BACKEND_PORT/api/health"
check "http://127.0.0.1:$FRONTEND_PORT/"
trap - ERR
pm2 save --force >/dev/null
echo "==> Deployed $TARGET ($DEPLOYED). Backup: $BACKUP"