Introduction
CI/CD (Continuous Integration and Continuous Deployment) automates the process of deploying applications to production. Instead of manually uploading files to a VPS, changes pushed to GitHub can automatically trigger deployment on the server.
This guide explains how to configure GitHub Actions to automatically deploy a Node.js SaaS application to a production VPS.
Prerequisites
- Ubuntu 22.04 VPS
- Node.js application already running
- PM2 installed on server
- NGINX configured
- GitHub repository
- SSH access to VPS
Step 1: Generate SSH Key on VPS
ssh-keygen -t rsa -b 4096
Copy the public key:
cat ~/.ssh/id_rsa.pub
Add this key to your GitHub repository under:
Settings → Deploy Keys → Add Key
Step 2: Add Server SSH Private Key to GitHub Secrets
On your local machine:
cat ~/.ssh/id_rsa
Copy the private key and add it inside:
GitHub Repository → Settings → Secrets → Actions → New Repository Secret
Name: VPS_SSH_KEY
Step 3: Create GitHub Actions Workflow
Inside your project create:
.github/workflows/deploy.yml
Add the following configuration:
name: Deploy to VPS
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_IP }}
username: root
key: ${{ secrets.VPS_SSH_KEY }}
script: |
cd /var/www/your-app
git pull origin main
npm install
pm2 restart node-app
Step 4: Add Additional Secrets
Add these repository secrets:
SERVER_IP → Your VPS IP address
VPS_SSH_KEY → Private SSH key
How It Works
- Developer pushes code to GitHub
- GitHub Actions workflow triggers automatically
- Server pulls latest code
- Dependencies install
- PM2 restarts application
- Zero manual deployment required
Zero Downtime Deployment Improvement
To reduce downtime, use:
pm2 reload node-app
Instead of restart. This enables seamless reload.
Security Best Practices
- Do not expose private SSH keys publicly
- Use non-root user for deployment
- Restrict SSH access by IP
- Enable firewall
Benefits of CI/CD for SaaS
- Faster deployment
- Reduced human error
- Consistent production environment
- Improved development workflow
- Scalable deployment automation
Conclusion
Implementing CI/CD on a VPS significantly improves deployment efficiency and reliability. By integrating GitHub Actions with your production server, you can automate SaaS application updates and focus on product development instead of manual server management.