DevOps  

Setting up CI/CD for Multi-Cloud Deployments using GitHub Actions and PM2

Introduction

In modern software development, speed, reliability, and scalability are critical. Whether you're deploying applications to AWS, Azure, or Google Cloud, managing deployments manually across multiple cloud platforms can quickly become complex and error-prone.

This is where CI/CD (Continuous Integration and Continuous Deployment) comes into play. By automating your build, test, and deployment processes, you can ensure faster releases, fewer bugs, and consistent performance across environments.

In this article, we will learn how to set up a complete CI/CD pipeline for multi-cloud deployments using GitHub Actions and PM2 in simple and practical terms.

What is CI/CD and Why It Matters

CI/CD is a development practice that automates the process of integrating code changes and deploying them to production.

Continuous Integration (CI)

This means automatically testing and building your code whenever changes are pushed to your repository.

Example:
Imagine you and your team are working on a Node.js application. Every time someone pushes code to GitHub, automated tests run to ensure nothing is broken.

Continuous Deployment (CD)

This means automatically deploying your application after successful testing.

Example:
If all tests pass, your app gets deployed to servers on AWS, Azure, or any other cloud platform without manual intervention.

Why CI/CD is important:

  • Reduces human errors in deployment

  • Speeds up release cycles

  • Ensures consistent deployments

  • Makes debugging easier

What is Multi-Cloud Deployment

Multi-cloud deployment means running your application on more than one cloud provider.

Example:

  • Backend hosted on AWS EC2

  • Database on Google Cloud

  • Backup server on Azure

Why companies use multi-cloud:

  • Avoid vendor lock-in

  • Improve reliability and uptime

  • Optimize cost

  • Better global performance

Real-life scenario:
If AWS goes down in a region, your app can still run on Azure or Google Cloud.

Tools We Will Use

Let’s understand the tools involved in this setup.

GitHub Actions

GitHub Actions is a CI/CD automation tool that allows you to create workflows directly inside your GitHub repository.

Key features:

  • Automates build, test, and deployment

  • Works with any programming language

  • Supports multi-cloud deployments

PM2

PM2 is a process manager for Node.js applications.

Key features:

  • Keeps your app running continuously

  • Automatically restarts app on crash

  • Supports zero-downtime reloads

Real-life analogy:
PM2 is like a supervisor that ensures your app is always running smoothly.

Architecture Overview

Before jumping into setup, let’s understand the flow.

  1. Developer pushes code to GitHub

  2. GitHub Actions triggers workflow

  3. Code is tested and built

  4. Application is deployed to multiple cloud servers

  5. PM2 manages the application processes

This ensures a fully automated pipeline from code commit to production.

Step 1: Prepare Your Servers (AWS, Azure, etc.)

First, you need servers on different cloud platforms.

Example setup:

  • AWS EC2 instance

  • Azure Virtual Machine

On each server:

  • Install Node.js

  • Install PM2 using command:

npm install -g pm2

  • Clone your application repository

Make sure SSH access is enabled because GitHub Actions will use it to deploy code.

Step 2: Configure SSH Access

To allow GitHub Actions to deploy your code, you need SSH keys.

Steps:

  1. Generate SSH key on your local machine

  2. Add public key to your cloud servers

  3. Add private key to GitHub Secrets

Example:

  • SSH_PRIVATE_KEY

  • SERVER_IP

  • USERNAME

This ensures secure communication between GitHub and your servers.

Step 3: Create GitHub Actions Workflow

Inside your repository, create a file:

.github/workflows/deploy.yml

Example workflow:

name: Multi-Cloud CI/CD Pipeline

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest

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

- name: Install Dependencies
  run: npm install

- name: Run Tests
  run: npm test

- name: Deploy to AWS
  run: |
    ssh user@aws-server "cd app && git pull && npm install && pm2 restart all"

- name: Deploy to Azure
  run: |
    ssh user@azure-server "cd app && git pull && npm install && pm2 restart all"

Explanation:

  • Code is checked out

  • Dependencies are installed

  • Tests are executed

  • Deployment happens on multiple servers

Step 4: Configure PM2 for Production

PM2 ensures your app runs reliably.

Start your app:

pm2 start app.js --name my-app

Enable auto-start on server reboot:

pm2 startup
pm2 save

Zero-downtime deployment:

pm2 reload my-app

Example scenario:
Instead of stopping your app during updates, PM2 reloads it smoothly without affecting users.

Step 5: Handling Environment Variables

Each cloud environment may have different configurations.

Example:

  • API keys

  • Database URLs

Use environment variables:

process.env.DB_URL

Store secrets securely in:

  • GitHub Secrets

  • Cloud provider settings

This keeps your application secure and flexible.

Step 6: Monitoring and Logs

Monitoring is essential for production apps.

PM2 provides built-in monitoring:

pm2 monit

View logs:

pm2 logs

Why it matters:

  • Detect issues early

  • Analyze performance

  • Debug errors quickly

Before vs After CI/CD

Before:

  • Manual deployments

  • High chance of errors

  • Slow release cycles

After:

  • Automated deployments

  • Faster releases

  • Reliable and consistent system

Advantages of This Setup

  • Fully automated deployment pipeline

  • Works across multiple cloud platforms

  • High availability and reliability

  • Easy rollback and updates

  • Scalable infrastructure

Disadvantages and Challenges

  • Initial setup can be complex

  • Requires knowledge of multiple cloud platforms

  • Debugging multi-cloud issues can be tricky

  • SSH management needs to be secure

Real-World Use Case

Imagine you are running an e-commerce website.

  • AWS handles main traffic

  • Azure acts as backup

  • GitHub Actions deploys updates automatically

  • PM2 ensures your app never goes down

If one cloud fails, your application continues running on another.

Conclusion

Setting up CI/CD for multi-cloud deployments using GitHub Actions and PM2 is a powerful way to build scalable and reliable applications.

While the setup may seem complex at first, the long-term benefits like automation, reduced downtime, and faster deployments make it absolutely worth it.

By following this approach, you can ensure your applications are always up-to-date, resilient, and ready to handle real-world challenges.

Start small, experiment, and gradually expand your CI/CD pipeline to support more advanced features like containerization and Kubernetes.