An open API service indexing awesome lists of open source software.

https://github.com/abdulrahmanalpha/ci-cd-webapp-ec2

CI/CD pipeline for web application using GitHub Actions, Docker, and AWS EC2.
https://github.com/abdulrahmanalpha/ci-cd-webapp-ec2

automation aws ci-cd cloud devops docker ec2 github-actions pipeline

Last synced: 9 months ago
JSON representation

CI/CD pipeline for web application using GitHub Actions, Docker, and AWS EC2.

Awesome Lists containing this project

README

          

# ๐Ÿš€ CI/CD Pipeline for Web Application

**Stack:** GitHub Actions ยท Docker ยท AWS EC2

---

![Terraform](https://img.shields.io/badge/IaC-Terraform-blueviolet?logo=terraform)
![AWS](https://img.shields.io/badge/Cloud-AWS-orange?logo=amazonaws)
![CI/CD](https://img.shields.io/badge/CI%2FCD-Jenkins-blue?logo=jenkins)
![Docker](https://img.shields.io/badge/Containers-Docker-blue?logo=docker)

---

## ๐Ÿ“Œ Project Overview

This project demonstrates how to build a **lightweight CI/CD pipeline** for deploying a web application on **AWS EC2** using **GitHub Actions** and **Docker**.

The goal was to **automate build, test, and deployment** workflows, ensuring faster delivery cycles and reproducible builds.

### ๐ŸŽฏ Outcomes

* Reduced deployment cycle time from **6 hours โ†’ 30 minutes**.
* Increased **developer velocity** with automated linting & testing.
* Standardized builds with **Docker images**.
* Automated deployment to **AWS EC2** via GitHub Actions.

---

## ๐Ÿ—๏ธ Architecture

![Architecture Diagram](docs/Architecture%20Diagram.png)

**Key components:**

1. **GitHub Actions CI/CD:**

* Workflow triggers on `git push`.
* Stages: Lint โ†’ Test โ†’ Build โ†’ Push โ†’ Deploy.
* Uses GitHub Secrets for storing AWS credentials & SSH keys.

2. **Docker:**

* Containerizes the application for reproducible builds.
* Images stored in AWS ECR (or Docker Hub).

3. **AWS EC2:**

* Acts as the deployment target.
* Runs Docker containers via a **deployment script** (`remote-deploy.sh`).
* Pulls new image, runs container, performs health check, and swaps/rolls back.

---

## ๐Ÿ“‚ Repository Structure

```
cicd-pipeline-ec2/
โ”œโ”€โ”€ .github/workflows/ # GitHub Actions workflows (CI/CD)
โ”œโ”€โ”€ docker/ # Dockerfiles for containerization
โ”œโ”€โ”€ scripts/ # Deployment helper scripts (e.g., remote-deploy.sh)
โ”œโ”€โ”€ app/ # Application source code
โ”œโ”€โ”€ docs/ # Architecture docs, diagrams
โ””โ”€โ”€ README.md # Project documentation
```

---

## โš™๏ธ Step-by-Step Implementation

### 1๏ธโƒฃ Setup GitHub Actions CI/CD

* Created `.github/workflows/cicd.yml` with stages:

1. Lint (ESLint).
2. Run unit tests (Jest).
3. Build Docker image.
4. Push image to AWS ECR (or Docker Hub).
5. Deploy to AWS EC2 over SSH.

๐Ÿ‘‰ Triggered automatically on `push` to `main`.

---

### 2๏ธโƒฃ Containerize Application with Docker

* Wrote **Dockerfile** to package app (Node.js/Express).
* Local test:

```bash
docker build -t webapp:local docker/
docker run -p 3000:3000 webapp:local
```

---

### 3๏ธโƒฃ Provision AWS EC2

* Launched an EC2 instance with:

* Docker installed.
* Security Group allowing SSH (22) & App traffic (3000).
* Configured SSH keys and stored in GitHub Secrets.

---

### 4๏ธโƒฃ Deploy to EC2

* Added `scripts/remote-deploy.sh`:

* Connects via SSH.
* Pulls latest Docker image.
* Stops old container & starts new one.
* Performs health check.
* Rolls back if failure detected.

๐Ÿ‘‰ Run automatically from GitHub Actions after build & push.

---

### 5๏ธโƒฃ Access the Application

* Find EC2 public IP:

```bash
aws ec2 describe-instances \
--query "Reservations[].Instances[].PublicIpAddress" \
--output text
```

* Open `http://:3000` in browser.

---

### 6๏ธโƒฃ Cleanup

* Stop Docker containers on EC2:

```bash
docker ps -q | xargs docker stop
```

* Terminate EC2 instance to avoid charges.

---

## ๐Ÿ”’ Security Notes

* **AWS credentials** stored securely in **GitHub Secrets**.
* **SSH key** used for deployment (never stored in repo).
* Docker images tagged with commit SHA for traceability.
* EC2 Security Group restricted to trusted IP ranges.

---

## ๐Ÿ“ˆ Next Improvements

* Add **Blue-Green Deployments** for zero-downtime releases.
* Use **Terraform** for EC2 provisioning.
* Replace manual SSH deploy with **AWS CodeDeploy** or **Ansible**.
* Add monitoring/alerts with **CloudWatch + Grafana**.

---

## โœ… Skills Demonstrated

* CI/CD with **GitHub Actions**.
* **Dockerization** for reproducible builds.
* Automated **EC2 deployments**.
* Secure use of **GitHub Secrets**.
* Real-world **DevOps automation workflow**.

---

## ๐Ÿง‘โ€๐Ÿ’ป Author

**Abdulrahman A. Muhamad**
DevOps | Cloud | SRE Enthusiast

๐Ÿ”— [LinkedIn](https://www.linkedin.com/in/abdulrahmanalpha) | [GitHub](https://github.com/AbdulrahmanAlpha) | [Portfolio](https://abdulrahman-alpha.web.app)