20. CI/CD & Production Servers
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.
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:
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.
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.
Knowledge Check
Ready to test your understanding of 20. CI/CD & Production Servers?