Zero Downtime Deployments: Blue-Green Strategy with PM2 and GitHub Actions
Blue-Green Deployment is a modern deployment strategy that minimizes downtime and risk by maintaining two production environments—Blue and Green. In this guide, we’ll combine this approach with a real-world CI/CD pipeline using GitHub Actions, PM2, and NGINX.
What is Blue-Green Deployment?
In this strategy, you maintain two environments:
- Blue - The current live environment
- Green - The new environment, where you deploy and test the latest version
Once the Green environment is confirmed stable, traffic is routed from Blue to Green via a reverse proxy like NGINX. If anything goes wrong, switching back is instant and safe.
Why Use Blue-Green Deployment?
Problem | Blue-Green Solution |
---|---|
Downtime during deployment | Deploy to Green in isolation, then switch traffic |
Rollback complexity | Switch traffic back to Blue instantly |
Live deployment anxiety | Confidence via full testing before going live |
Tools We’ll Use
- PM2 – Process manager for Node.js apps
- GitHub Actions – CI/CD automation platform
- NGINX – Reverse proxy to control traffic switching
- Node.js – Application runtime
Folder Structure
Your VPS will have two directories for the app:
/var/www/myapp-blue # Live version (on port 3000)
/var/www/myapp-green # New version (on port 3001)
CI/CD Pipeline with GitHub Actions
We’ll use GitHub Actions to deploy to the Green environment on every push to main
. After deployment, it will automatically switch NGINX to point to Green.
📄 .github/workflows/deploy.yml
name: Deploy to VPS (Blue-Green)
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.VPS_SSH_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.VPS_HOST }} >> ~/.ssh/known_hosts
- name: Deploy to Green Environment
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} << 'EOF'
echo "🔁 Connected to VPS"
# Load Node.js environment (if using nvm)
export NVM_DIR="\$HOME/.nvm"
[ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh"
# Navigate to green deployment
cd /var/www/myapp-green
git pull origin main
npm install
npx tsc
# Restart Green instance with PM2
pm2 start dist/index.js --name myapp-green || pm2 restart myapp-green
pm2 save
echo "🟢 Green deployed. Switching traffic..."
# Update NGINX config to route to port 3001
sudo sed -i 's/3000/3001/' /etc/nginx/sites-available/myapp
sudo nginx -s reload
echo "✅ Deployment complete!"
EOF
🔐 Required GitHub Secrets
VPS_HOST
– Your server’s IP or domainVPS_USER
– SSH username (e.g., ubuntu)VPS_SSH_KEY
– Private key (paste as plain text)
Rollback in One Command
If something fails in Green, you can switch NGINX back to Blue:
sudo sed -i 's/3001/3000/' /etc/nginx/sites-available/myapp
sudo nginx -s reload
pm2 restart myapp-blue
Benefits Recap
- ✅ Zero-downtime deployments
- ✅ Easy rollback with one-line NGINX switch
- ✅ Automated pipeline using GitHub Actions
- ✅ Works well even for solo devs and small teams
Conclusion
Blue-Green Deployment is no longer limited to large enterprise setups. With the help of PM2, NGINX, and GitHub Actions, you can deploy robustly, confidently, and without downtime—on your own VPS. This approach provides both flexibility and safety, giving you full control over the release process.
0 Comments