Skip to content

About

# Node.js CI/CD Deployment with AWS EC2 A Node.js application deployed on **AWS EC2 using a complete CI/CD pipeline with GitHub Actions**. Every push to the `main` branch automatically installs dependencies, builds and tests the application, verifies EC2 connectivity, and securely deploys the latest code via SSH. **PM2** is used on the EC2 server

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

CI/CD Deployment with GitHub Actions & AWS EC2

A production-style Next.JS,Node.js deployment setup using GitHub Actions CI/CD and Amazon EC2. Every push to the main branch automatically triggers the workflow to install dependencies, build the application, run tests, verify EC2 connectivity, and deploy the latest code to the EC2 server through SSH.

🚀 Deployment Architecture

Developer
   │
   │ git push
   ▼
GitHub Repository
   │
   ▼
GitHub Actions
   │
   ├── Checkout Code
   ├── Setup Node.js 22
   ├── npm ci
   ├── Build Application
   ├── Run Tests
   ├── Check EC2 Connectivity
   │
   ▼
SSH Deployment
   │
   ▼
AWS EC2 Instance
   │
   ├── git pull
   ├── npm install
   ├── npm run build
   └── PM2 restart
   │
   ▼
Running Application

⚙️ CI/CD Workflow

The workflow is triggered automatically whenever code is pushed to the main branch.

1. Checkout Repository

Uses actions/checkout to download the latest source code into the GitHub Actions runner.

2. Setup Node.js

The workflow uses Node.js 22.x with npm dependency caching through actions/setup-node.

3. Install Dependencies

npm ci

Installs dependencies from the lock file in a clean and reproducible way.

4. Build Application

npm run build --if-present

Builds the application when a build script is available.

5. Run Tests

npm test

Runs the project's automated tests before deployment.

6. Verify EC2 Connectivity

Before deployment, the workflow checks whether the EC2 instance is reachable on SSH port 22 using network connectivity checks.

7. Deploy Through SSH

The workflow uses appleboy/ssh-action to securely connect to the EC2 instance using GitHub Secrets.

On the EC2 server it executes:

git pull origin main
npm install
npm run build
pm2 restart all

This updates the application and restarts the running Node.js process using PM2.

🔐 GitHub Secrets

The following secrets are required:

Secret Purpose
EC2_HOST EC2 public IP/DNS
EC2_USERNAME EC2 SSH username
EC2_SSH_KEY Private SSH key

Sensitive credentials are stored in GitHub Secrets rather than being hard-coded in the workflow.

☁️ AWS EC2 Deployment

The application is deployed on an Amazon EC2 instance, which acts as the production server. Node.js and PM2 are installed on the instance, while Git is used to pull the latest version of the application during deployment.

🔄 Deployment Flow

git push origin main
        ↓
GitHub Actions Triggered
        ↓
Install Dependencies
        ↓
Build Application
        ↓
Run Tests
        ↓
Check EC2 SSH Connectivity
        ↓
SSH into EC2
        ↓
Pull Latest Code
        ↓
Install Dependencies
        ↓
Build
        ↓
PM2 Restart
        ↓
Application Updated

🛠️ Technologies Used

  • Node.js 22
  • Git & GitHub
  • GitHub Actions
  • AWS EC2
  • SSH
  • PM2
  • npm
  • Linux

🎯 Project Objective

The purpose of this project is to demonstrate a practical CI/CD pipeline for a Node.js application, where code changes pushed to GitHub are automatically validated and deployed to an AWS EC2 production environment without manually logging into the server for every deployment.

About

# Node.js CI/CD Deployment with AWS EC2 A Node.js application deployed on **AWS EC2 using a complete CI/CD pipeline with GitHub Actions**. Every push to the `main` branch automatically installs dependencies, builds and tests the application, verifies EC2 connectivity, and securely deploys the latest code via SSH. **PM2** is used on the EC2 server

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages