Deploying by hand, copying files and typing commands, works the first ten times. Then one evening you forget a step, or skip the tests, and the site is down. CI/CD replaces the manual steps with a script that runs the same way every time. This lesson covers that script, and the three habits that keep a live app healthy afterwards: monitoring, backups and careful releases.
CI and CD
- Continuous integration (CI): every push and pull request automatically installs the project and runs the tests and checks. A failing test blocks the merge.
- Continuous delivery or deployment (CD): when the checks pass on the main branch, the new version is automatically published to the server.
- The benefit is not speed alone. It is confidence: nobody can ship something that fails the tests by accident.
A GitHub Actions workflow
GitHub Actions reads files in .github/workflows. The workflow below has two jobs. The test job runs on every push to main and on pull requests. It checks out the code, installs Node 22 with npm caching, runs npm ci, runs npm audit so that serious known vulnerabilities fail the build, and runs the tests. The deploy job runs only on main and only after test succeeded. It logs in to your server over SSH and runs the update commands.
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm audit --audit-level=high # fail on serious known vulnerabilities
- run: npm test
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- name: Deploy over SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd /var/www/app
git pull origin main
npm ci --omit=dev
pm2 reload ecosystem.config.cjs --env production # reload = no downtime
The values secrets.SERVER_HOST, SERVER_USER and SERVER_SSH_KEY come from the repository settings, under *Secrets*. They are never written in the file. Use a dedicated deploy key that can only access this server, not your personal key. The last command is pm2 reload, which restarts the app copies one by one, so the release has no downtime.
Safer releases
- Roll back quickly. Keep the previous version ready. With Git, redeploy the previous commit. With Docker, run the previous image tag.
- Database changes need care. Make them backwards compatible, such as adding a column first and removing the old one later, so old and new code work together during a release.
- Graceful shutdown. On
SIGTERM, stop accepting new requests, finish the current ones, close the database and exit. PM2 and Docker both send that signal. - Release small and often. A small change is easy to understand and to undo.
Monitoring: know before your users tell you
- Health route:
/healthanswers 200 when the app and its database work. An external uptime service, such as UptimeRobot, checks it every minute and sends you a message. - Logs: structured logs from lesson 33, kept somewhere searchable.
- Errors: a tool such as Sentry collects exceptions with the stack and context, and alerts you.
- Metrics: CPU, memory, response times and error rates. Tools such as Prometheus with Grafana, or the dashboards of your cloud, show trends. Event loop delay, from lesson 39, is a good Node-specific metric.
- Alerts that people can act on. Too many alerts teach everyone to ignore them.
Backups
Databases are lost through mistakes, bugs, attacks and broken disks. A backup is the only cure, and a backup that was never tested is a hope, not a backup. The script below dumps the MongoDB database every night, compresses it, and deletes dumps older than 14 days.
#!/usr/bin/env bash
# Daily database backup. Run from cron: 0 2 * * * /var/www/app/backup.sh
set -euo pipefail
STAMP=$(date +%F)
DEST=/var/backups/app
mkdir -p "$DEST"
mongodump --uri "$MONGODB_URI" --archive="$DEST/db-$STAMP.gz" --gzip
find "$DEST" -name 'db-*.gz' -mtime +14 -delete # keep 14 days
# Copy $DEST to another machine or a storage bucket too. A backup on the same disk is not enough.
# And TEST a restore now and then: mongorestore --uri "$TEST_URI" --archive=... --gzip
- Follow the 3-2-1 idea: three copies, on two kinds of storage, with one copy in another place.
- Copy backups off the server, for example to a storage bucket. A backup on the same disk dies with the disk.
- Encrypt backups that contain personal data, and restrict who can read them.
- Test the restore on a separate machine, on a schedule. Write down how long it takes.
- MongoDB Atlas and most managed databases offer automatic backups. Turn them on and read what they include.
Your task
- Add a CI workflow to a repository that only runs the tests, without the deploy job.
- Add a
/healthroute that pings the database and returns 503 on failure. - Run the backup script on a test database, then restore it into another database and compare the document counts.
How to check it worked
- A pull request with a failing test shows a red mark.
- The deploy job never runs on a failing build.
- You have restored a backup at least once.