Learn How to Implement Zero-Downtime Deployments for Node.js Apps Using PM2 and GitHub Actions
Why Zero-Downtime Deployments Matter
- No service interruptions during updates.
- Smooth restarts with rolling reloads.
- Better SEO and uptime scores.
- Happier users (and developers).
Step 1: Install and Configure PM2
PM2 is a process manager for Node.js that supports zero-downtime reloads using the pm2 reload
command.
Install PM2 globally:
npm install pm2 -g
Start your Node.js app with PM2:
pm2 start app.js --name my-app
Enable zero-downtime reload:
pm2 reload my-app
PM2 will spin up the new version while keeping the old one alive until the new process is ready.
Step 2: Prepare Your Server for Deployments
- Node.js & npm are installed.
- PM2 is running your app.
- Git is installed for pulling updates.
Also, allow your CI/CD pipeline (GitHub Actions) to connect to your server via SSH.
Step 3: Create a GitHub Actions Workflow
GitHub Actions will automate the deployment process whenever you push to your main
branch.
Example Workflow:
name: Deploy Node.js Appon:push:branches:- mainjobs:deploy:runs-on: ubuntu-lateststeps:- name: Checkout codeuses: actions/checkout@v3- name: Install SSH keyuses: webfactory/ssh-agent@v0.5.4with:ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}- name: Deploy to Serverrun: |ssh -o StrictHostKeyChecking=no user@your-server-ip << 'EOF'cd /path/to/appgit pull origin mainnpm install --productionpm2 reload my-app --update-envEOF
Step 4: Secure Deployment with GitHub Secrets
In your repository settings:
-
Add
SSH_PRIVATE_KEY
(your server's private key for deployment). -
Ensure your server’s public key is in
~/.ssh/authorized_keys
.
This prevents exposing sensitive credentials in your repo.
Step 5: Test the Deployment
- Make a change in your code.
- Push to the
main
branch. - GitHub Actions will trigger, SSH into the server, pull changes, install dependencies, and reload your app without downtime.
Step 6: Advanced Tips
- Blue-Green Deployments: Keep two identical environments and switch traffic between them for safer rollouts.
- Environment Variables: Use
.env
andpm2 reload --update-env
to apply changes without restarts. - Monitoring: Use
pm2 monit
or PM2's dashboard to monitor performance during deployments.
Final Thoughts
By combining PM2 with GitHub Actions, you can create a fully automated zero-downtime deployment pipeline for your Node.js apps. This means:
- Faster releases.
- No more middle-of-the-night restart disasters.
- Peace of mind knowing your users are never interrupted.
If you want to make this even more robust, you can integrate database migrations, health checks, and rollback strategies.
Related Guides
- Learn to configure domains, set up Nginx, and install free SSL certificates to make your Ionic blog secure and SEO-friendly.
- Build portable and scalable apps effortlessly by packaging your entire Ionic and Node.js stack into containers.
- A step-by-step tutorial to set up CI/CD pipelines for SSH deployment and Firebase Hosting using GitHub Actions.
- Easily deploy and manage your apps on DigitalOcean using PM2 and Nginx for high availability and performance.
0 Comments