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.
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
The workflow is triggered automatically whenever code is pushed to the main branch.
Uses actions/checkout to download the latest source code into the GitHub Actions runner.
The workflow uses Node.js 22.x with npm dependency caching through actions/setup-node.
npm ciInstalls dependencies from the lock file in a clean and reproducible way.
npm run build --if-presentBuilds the application when a build script is available.
npm testRuns the project's automated tests before deployment.
Before deployment, the workflow checks whether the EC2 instance is reachable on SSH port 22 using network connectivity checks.
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 allThis updates the application and restarts the running Node.js process using PM2.
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.
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.
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
- Node.js 22
- Git & GitHub
- GitHub Actions
- AWS EC2
- SSH
- PM2
- npm
- Linux
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.