Header Ads

Zero-Downtime Deployment Flowchart – Step-by-Step DevOps Guide

 Learn How to Implement Zero-Downtime Deployments for Node.js Apps Using PM2 and GitHub Actions

Downtime is detrimental to user experience. If your Node.js application goes offline for even a few seconds during deployment, you risk losing users, transactions, and trust.
In this guide, we’ll walk you through setting up zero-downtime deployments for a Node.js app using PM2 and GitHub Actions, ensuring your updates roll out without interruption.



Why Zero-Downtime Deployments Matter

Imagine you push a new feature, and while the server restarts, a customer is making a payment. That request fails — now you have an angry customer.
Zero-downtime deployments ensure:

  • 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

You need a server where PM2 runs your app.
SSH into your server and make sure:

  • 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.

In your repository, create the file:
.github/workflows/deploy.yml


Example Workflow:

name: Deploy Node.js App

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Install SSH key
        uses: webfactory/ssh-agent@v0.5.4
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

      - name: Deploy to Server
        run: |
          ssh -o StrictHostKeyChecking=no user@your-server-ip << 'EOF'
            cd /path/to/app
            git pull origin main
            npm install --production
            pm2 reload my-app --update-env
          EOF


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 and pm2 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.
Step-by-step guide to deploy your Ionic frontend and Node.js backend on DigitalOcean using PM2 and Nginx
  • Easily deploy and manage your apps on DigitalOcean using PM2 and Nginx for high availability and performance.

Post a Comment

0 Comments