A complete pipeline: typecheck, lint and build on every push, then deploy to your VPS over SSH with backups and one-command rollback — no platform lock-in.

Francis K Njenga
Senior Developer
Managed platforms are wonderful until the bill scales with your traffic. A $6 VPS running Next.js behind Nginx can serve a very serious amount of requests — what it usually lacks is the automation platforms give you for free: every push tested, every merge deployed, every release rollback-able. This article builds that automation with GitHub Actions, SSH and PM2. It is the same shape as the pipeline we run for client sites, including this one.
In the repo settings add: VPS_HOST, VPS_USER, VPS_SSH_KEY (a dedicated deploy key — never your personal one), and VPS_PATH (e.g. /var/www/myapp). On the server: Node LTS, PM2 (npm i -g pm2), Nginx proxying to the app port, and the app's .env living only on the server. Add the public half of the deploy key to ~/.ssh/authorized_keys.
name: CI / CD
on:
push:
branches: [main]
pull_request:
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npx tsc --noEmit
- run: npm run lint
- run: npm run build
env:
NEXT_TELEMETRY_DISABLED: 1
deploy:
needs: ci
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci && npm run build
- name: Prepare release bundle
run: |
mkdir -p release
cp -r .next public package.json package-lock.json next.config.mjs release/
- name: Upload to VPS
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USER }}
key: ${{ secrets.VPS_SSH_KEY }}
source: "release/*"
target: "/tmp/incoming"
- name: Swap + reload
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USER }}
key: ${{ secrets.VPS_SSH_KEY }}
script: |
set -e
APP=${{ secrets.VPS_PATH }}
STAMP=$(date +%Y%m%d-%H%M%S)
mkdir -p /var/backups/myapp
tar czf /var/backups/myapp/$STAMP.tar.gz -C $APP .next public
rsync -a --delete /tmp/incoming/release/ $APP/
cd $APP && npm ci --omit=dev
pm2 reload myapp --update-env
rm -rf /tmp/incoming
ls -t /var/backups/myapp | tail -n +6 | xargs -I{} rm /var/backups/myapp/{} || trueneeds: ci makes deployment structurally impossible on a failing build.
pm2 reload restarts workers one at a time (cluster mode) or gracefully (fork mode with kill_timeout), so in-flight requests finish. Register the app once with an ecosystem file so restarts survive reboots:
module.exports = {
apps: [
{
name: "myapp",
cwd: "/var/www/myapp",
script: "node_modules/next/dist/bin/next",
args: "start -p 3000",
instances: 1, // raise on bigger boxes for cluster reloads
exec_mode: "fork",
kill_timeout: 5000, // let in-flight requests finish
max_memory_restart: "512M",
env: { NODE_ENV: "production" },
},
],
};
// First time on the server:
// pm2 start ecosystem.config.cjs && pm2 save && pm2 startupmax_memory_restart is a safety net for leaks — tune to your box.
Because every deploy archives the previous .next and public, recovery is mechanical — no git archaeology during an incident:
#!/usr/bin/env bash
set -euo pipefail
APP=/var/www/myapp
LATEST=$(ls -t /var/backups/myapp/*.tar.gz | head -1)
echo "Rolling back to: $LATEST"
tar xzf "$LATEST" -C "$APP"
cd "$APP"
pm2 reload myapp --update-env
pm2 status myappPractice it once in staging — a rollback you have never run is a hope, not a plan.
command= restrictions in authorized_keys.pg_dump), and make them backward-compatible for one release.curl -fsS https://yourdomain.com/api/health as the last step; fail the run (and alert) if it does not answer.concurrency: { group: deploy, cancel-in-progress: false } so two pushes never deploy simultaneously.Why not just build on the server? You can — it is simpler — but a 2 GB VPS compiling Next.js while serving traffic risks OOM-killing your app (or the neighbor app). Building in CI and shipping artifacts keeps the server boring, which is exactly what you want it to be.
This is the deployment discipline we bring to every project: tested builds, atomic swaps, backups, and a rollback that works at 2 a.m. Want it set up for your product? Let's talk DevOps.

Senior Developer
Francis Njenga is an experienced Lead Developer specializing in React, Next.js, and modern JavaScript frameworks, with a strong background in web development.