20. CI/CD & Production Servers
Writing code locally is the easy part. Getting that code running reliably on a server that's accessible to the entire world — that's deployment. Modern deployment isn't a manual process of copying files to a server. It's an automated pipeline where every code change is automatically tested, built, and deployed without human intervention. This is CI/CD: Continuous Integration / Continuous Deployment.
Why Local Development Servers Can't Be Used in Production
The node server.js or python app.py commands you use locally are fine for development, but catastrophically unsuitable for production:
- No auto-restart: If your code throws an unhandled error and crashes, the server dies. Users get connection refused errors until someone manually restarts it.
- Single core: Node.js runs on a single CPU core. A modern server has 8, 16, or 32 cores, all sitting idle.
- No logging: Console.log output disappears. When something breaks at 3am, you have zero visibility.
- Security exposure: Development servers often skip HTTPS, CORS, and other security configurations.
PM2 — Production Process Manager
PM2 is the standard process manager for Node.js in production. It solves all the problems above: it keeps your process alive, restarts on crash, utilizes all CPU cores, manages logs, and provides a monitoring dashboard.
npm install -g pm2 # Start your app with PM2 pm2 start server.js --name "voidx-api" # Start in cluster mode — one process per CPU core (maximum throughput) pm2 start server.js --name "voidx-api" --instances max # Common PM2 commands pm2 status # See all running processes pm2 logs voidx-api # Stream live logs pm2 restart voidx-api # Restart gracefully pm2 stop voidx-api # Stop the process pm2 delete voidx-api # Remove from PM2's list # Make PM2 auto-start on server reboot pm2 startup pm2 save
GitHub Actions — Automated CI/CD
Every time you push code to GitHub, GitHub Actions can automatically run your tests, build your Docker image, and deploy to your server — all without manual steps. This is CI/CD. Here is a complete workflow file:
# .github/workflows/deploy.yml
name: Test and Deploy
on:
push:
branches: [main] # Trigger on every push to main branch
pull_request:
branches: [main] # Also run tests on pull requests
jobs:
# ─── Job 1: Run tests ───
test:
runs-on: ubuntu-latest
services:
# Spin up a real PostgreSQL for integration tests
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: testpassword
POSTGRES_DB: testdb
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
env:
DATABASE_URL: postgresql://postgres:testpassword@localhost/testdb
JWT_SECRET: test-secret-key
# ─── Job 2: Deploy (only runs if tests pass) ───
deploy:
needs: test # Only run if the 'test' job succeeded
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' # Only deploy from main branch
steps:
- name: Deploy to Railway
uses: railway-app/railway-deploy-action@v1
with:
railway-token: ${{ secrets.RAILWAY_TOKEN }}
service: voidx-api
Cloud Deployment Platforms
Deployment platforms handle the infrastructure, letting you focus on your code:
The most developer-friendly option. Connect GitHub repo, it detects your app and deploys automatically. Free tier available. Supports PostgreSQL, Redis built-in.
Similar to Railway. Free tier with persistent disk. Excellent for full-stack apps, background workers, and cron jobs.
More control, still developer-friendly. App Platform for easy deploys, Droplets for full VPS control. Competitive pricing.
Maximum power and flexibility. Used by enterprises at scale. Steeper learning curve. EC2, Lambda, App Engine, Azure App Service.
Environment Variables in Production
Never commit sensitive values to your repository. Every cloud platform provides a way to set environment variables through their dashboard or CLI — these are injected into your container at runtime, completely separate from your code.
# Railway CLI railway variables set JWT_SECRET="production-super-long-random-key-here" railway variables set DATABASE_URL="postgresql://..." railway variables set STRIPE_SECRET_KEY="sk_live_..." # For Docker on a VPS, use a .env file that's NEVER in Git # and reference it when starting the container: docker run --env-file /etc/voidx/.env -p 3000:3000 my-backend-api
Nginx as a Reverse Proxy
If you deploy on a raw Linux server (like a DigitalOcean Droplet), you typically run your Node.js app on port 3000 and put Nginx in front of it on port 80/443. Nginx handles HTTPS termination, static file serving, load balancing, and request buffering — all much more efficiently than Node.js can on its own.
# /etc/nginx/sites-available/voidx-api
server {
listen 80;
server_name api.voidx.io;
# Redirect all HTTP to HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name api.voidx.io;
# SSL certificates from Let's Encrypt (free)
ssl_certificate /etc/letsencrypt/live/api.voidx.io/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.voidx.io/privkey.pem;
# Proxy all requests to your Node.js app on port 3000
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
}
}
Knowledge Check
Ready to test your understanding of 20. CI/CD & Production Servers?