# Project Title: Jenkins CI/CD(Freestyle pipeline) with GitHub Integration (Deploying Node.js To-Do Application)

This documentation provides a comprehensive guide on setting up a **CI/CD pipeline** using **Jenkins** integrated with **GitHub** for continuous integration and deployment of a **Node.js To-Do application** on an **AWS EC2** instance. Jenkins will automatically pull code from GitHub, build it, and deploy it using **Docker**.

---

## **Steps to Complete the Project:**

### **1\. Login to AWS and Create an EC2 Instance**

* **Login to your AWS account** through the [AWS Console](https://aws.amazon.com/console/).
    
* **Launch an EC2 instance**:
    
    * Navigate to the **EC2 Dashboard** and click on **Launch Instance**.
        
    * Choose **Ubuntu 24.04 LTS** as the operating system.
        
    * Select the **t2.micro** instance type (free-tier eligible).
        
    * Configure your **security groups** to allow **SSH (port 22)** access and also prepare ports **8080** (for Jenkins) and **8000** (for the web app).
        
    * Launch the instance and download the **.pem key** file for SSH access.
        

### **2\. Connect to the EC2 Instance**

Connect to your newly created EC2 instance using **SSH**.

```bash
ssh -i <your-key-file.pem> ubuntu@<instance-public-ip>
```

### **3\. Update the Instance**

Run the following command to update the instance with the latest package information:

```bash
sudo apt update
```

### **4\. Install Java**

Since Jenkins is built in **Java**, it requires **Java** to run. Install **OpenJDK 17**:

```bash
sudo apt install openjdk-17-jre
```

Verify the installation:

```bash
java -version
```

### **5\. Install Jenkins**

Now, let's install **Jenkins**, which is essential for building the CI/CD pipeline.

a) Install dependencies required for Jenkins:

```bash
sudo apt-get install -y ca-certificates curl gnupg
```

b) Add the Jenkins GPG key:

```bash
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
```

c) Add Jenkins repository:

```bash
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
```

d) Update the package list:

```bash
sudo apt-get update
```

e) Install Jenkins:

```bash
sudo apt-get install jenkins
```

f) Enable Jenkins to start on boot:

```bash
sudo systemctl enable jenkins
```

g) Start Jenkins:

```bash
sudo systemctl start jenkins
```

h) Check Jenkins status to confirm it's running:

```bash
sudo systemctl status jenkins
```

### **6\. Open Jenkins in a Browser**

Jenkins runs on **port 8080**. To access it:

* **Modify the security group** of your EC2 instance to allow inbound traffic on **port 8080**.
    
* In your browser, go to `http://<instance-public-ip>:8080`.
    

### **7\. Unlock Jenkins**

When accessing Jenkins for the first time, you'll be asked for an admin password.

* Retrieve the initial admin password by running:
    

```bash
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
```

* Copy the password and paste it into the Jenkins login screen.
    

### **8\. Install Suggested Jenkins Plugins**

After entering the admin password, Jenkins will prompt you to install plugins. Select **Install Suggested Plugins** to get essential tools.

### **9\. Create an Admin User**

After plugins installation, Jenkins will prompt you to create your admin user account. Set a username and password for future logins.

### **10\. Create a New Jenkins Project**

Now, we'll create a **freestyle project** for our CI/CD pipeline.

* Click on **New Item**.
    
* Enter a name for your project (e.g., **NodeJS To-Do App**).
    
* Select **Freestyle Project** and click **OK**.
    

### **11\. Configure GitHub Integration**

In the new Jenkins project:

* Under **Source Code Management**, select **Git**.
    
* Paste the **URL of your GitHub repository** where your Node.js To-Do application code is hosted.
    

### **12\. Generate SSH Keys for GitHub Authentication**

Jenkins will authenticate with GitHub using SSH keys.

* Run the following command on your EC2 instance to generate SSH keys:
    

```bash
ssh-keygen
```

* **Save the private key** for Jenkins and use the **public key** for GitHub.
    

### **13\. Add SSH Key to GitHub**

To enable Jenkins to connect to your GitHub repository:

* Go to **GitHub Settings** &gt; **SSH and GPG Keys**.
    
* Click on **New SSH Key**, provide a title, and paste the **public key** generated earlier.
    
* Save it.
    

### **14\. Add SSH Credentials in Jenkins**

Back in Jenkins, under **Source Code Management**:

* Click on **Add Credentials**.
    
* Choose **SSH Username with Private Key**.
    
* Paste the **private key** you generated earlier for Jenkins.
    

### **15\. Configure Build Trigger for GitHub Webhook**

To ensure Jenkins automatically pulls new code when changes are made on GitHub:

* Go to **Build Triggers** and check **GitHub hook trigger for GITScm polling**.
    

### **16\. Install Docker on EC2 Instance**

Since we will deploy the Node.js app inside a Docker container, Docker needs to be installed on your EC2 instance.

* Install Docker:
    

```bash
sudo apt install docker.io
```

* Add Jenkins to the **docker** group:
    

```bash
sudo usermod -a -G docker jenkins
```

* Restart Jenkins to apply changes:
    

```bash
sudo systemctl restart jenkins
```

### **17\. Configure Build Steps**

In the Jenkins project configuration, add a **Build Step** by selecting **Execute Shell**.

Paste the following Docker commands to remove any existing container, build a new image, and deploy the app:

```bash
# Remove the existing container if it exists (ignore errors)
docker rm -f <desired_name_1> || true

# Build the Docker image
docker build . -t <desired_name_2>

# Run the container
docker run --rm -d --name <desired_name_1> -p 8000:8000 <desired_name_2>
```

This script ensures that your application is continuously integrated and deployed from GitHub without errors.

### **18\. Install the GitHub Integration Plugin**

To fully integrate Jenkins with GitHub:

* Navigate to **Manage Jenkins** &gt; **Manage Plugins**.
    
* Search for **GitHub Integration Plugin** under the **Available** tab and install it.
    
* Restart Jenkins.
    

### **19\. Configure GitHub Webhook**

GitHub needs to notify Jenkins whenever new code is pushed.

* Go to your GitHub repository settings and click on **Webhooks**.
    
* Add a new webhook with the following details:
    
    * **Payload URL**: `http://<instance-public-ip>:8080/github-webhook/`
        
    * **Content Type**: `application/json`
        
* Save the webhook and ensure it shows a green tick, indicating success.
    

### **20\. Open Application Port 8000**

To allow access to your deployed Node.js application:

* Modify the security group of your EC2 instance to allow inbound traffic on **port 8000**.
    

### **21\. Access the Application**

You can now access the deployed Node.js To-Do application by navigating to:

```bash
http://<instance-public-ip>:8000
```

### **22\. Continuous Integration and Deployment**

Every time you push code changes to your GitHub repository, Jenkins will:

1. Automatically pull the latest changes.
    
2. Build a new Docker image.
    
3. Remove any old containers.
    
4. Deploy the updated application.
    

This ensures continuous integration and continuous deployment of your application.

---

## **Conclusion**

This Jenkins CI/CD pipeline automates the entire process of **building**, **testing**, and **deploying** your Node.js application on **AWS EC2** using **Docker**. With the integration of **GitHub webhooks**, any push to your GitHub repository will trigger Jenkins to update your live application seamlessly.

---

**Repository Used in This Project:**

* [GitHub Repository - Node.js To-Do App](https://github.com/Amitabh-DevOps/Jenkins-CI-CD-Project-Todo-node-app)
    

By following these steps, you've successfully completed the project!

---

# <mark>Output of My Project :</mark>

1 ) Making changes in Github repo :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726674811581/15e6d292-33e2-4602-84cc-0115beacb0a4.png align="center")

2 ) Checking the Contineous Integration of code on Jenkins :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726674912547/f4aec1ff-c5b4-4b78-a8b1-4700e245875f.png align="center")

3 ) Changes is Successfully applied to Project using Github push :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726674925733/94f1a85e-87c1-4d62-9926-f823dbbd8f88.png align="center")

4 ) Checking the Output :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726675088237/935ae651-ed7b-410b-984f-bca9775ea0d9.png align="center")

5 ) Testing the Deployed Project by checking its Features :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726675150548/b87b38bf-8482-4e20-893f-d96fe1f8f274.png align="center")

6 ) Checking Contineous Integration is Working or NOT :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726675217969/de37adf1-ce45-4a69-ab01-79826e41c1b6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726675235022/afeee188-81df-42a6-aacb-83f9240ddc28.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726675249078/ec7c57da-e254-4fef-b713-45dca1c3bb2e.png align="center")
