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:
+32
-11
@@ -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).
|
||||
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user