This project demonstrates a CI/CD pipeline for building, testing, and deploying a React application using Jenkins, Docker, and AWS EC2. The pipeline automates the process of building Docker images, pushing them to Docker Hub, and deploying the application to an EC2 instance.
This project automates the following tasks:
- Build: Builds a Docker image for a React application.
- Push: Pushes the Docker image to Docker Hub.
- Images are tagged as
devfor thedevbranch andprodfor themasterbranch.
- Images are tagged as
- Deploy: Deploys the application to an AWS EC2 instance.
- The
masterbranch triggers deployment to the production environment.
- The
Before setting up the project, ensure you have the following:
- A GitHub repository with the project code.
- Two branches:
devandmaster.
- Jenkins installed and configured.
- Required plugins:
- GitHub Integration
- Docker Pipeline
- Credentials Plugin
- A Docker Hub account.
- Two repositories:
dev(public) andprod(private).
- An EC2 instance running Ubuntu.
- Docker installed on the instance.
- Security group configured to allow HTTP traffic on port 80 and SSH access.
- An SSH key pair for accessing the EC2 instance.
Clone the repository:
git clone https://github.com/manjunath-start/devops-build-task.git
cd devops-build-taskInstall dependencies:
npm installBuild the React application:
npm run buildBuild the Docker image:
docker build -t manjunathdc/devops-app:dev .Run the Docker container locally:
docker run -d -p 80:80 --name devops-app manjunathdc/devops-app:devAccess the application:
http://localhost:80
Follow the official Jenkins installation guide.
Go to Manage Jenkins > Manage Plugins and install:
- GitHub Integration
- Docker Pipeline
- Credentials Plugin
- Go to your GitHub repository.
- Navigate to Settings > Webhooks.
- Add a new webhook:
- Payload URL:
http://<your-jenkins-server-ip>:8080/github-webhook/ - Content type:
application/json. - Trigger: Select Just the push event.
- Payload URL:
Go to Manage Jenkins > Manage Credentials and add:
- Docker Hub credentials (username and password).
- SSH key for the EC2 instance.
- Go to New Item in Jenkins.
- Enter a name for your pipeline (e.g.,
DevOps-Build-Deploy-Multibranch). - Select Multibranch Pipeline and configure:
- Branch Sources: Add your GitHub repository.
- Scan Multibranch Pipeline Triggers: Set up automatic branch scanning.
- Use an Ubuntu AMI.
- Configure the security group to allow HTTP traffic on port 80 and SSH access.
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable dockersudo usermod -aG docker ubuntusudo systemctl restart docker-
Push to
devBranch- Jenkins builds the Docker image and pushes it to the
devrepository on Docker Hub.
- Jenkins builds the Docker image and pushes it to the
-
Merge
devintomaster- Jenkins builds the Docker image and pushes it to the
prodrepository on Docker Hub. - Jenkins deploys the application to the EC2 instance.
- Jenkins builds the Docker image and pushes it to the