https://github.com/priyansusahoo/deploy-v2
Repository includes all files for the application, dockerfile. Integrated with jenkins using web-hooks.
https://github.com/priyansusahoo/deploy-v2
aws-ec2-intances cicd demo-deploy dockerfile jenkins-pipeline nodejs web-hook
Last synced: 8 months ago
JSON representation
Repository includes all files for the application, dockerfile. Integrated with jenkins using web-hooks.
- Host: GitHub
- URL: https://github.com/priyansusahoo/deploy-v2
- Owner: Priyansusahoo
- Created: 2022-12-21T15:30:04.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-06T13:01:08.000Z (almost 2 years ago)
- Last Synced: 2025-01-08T06:46:06.512Z (9 months ago)
- Topics: aws-ec2-intances, cicd, demo-deploy, dockerfile, jenkins-pipeline, nodejs, web-hook
- Language: JavaScript
- Homepage: http://34.214.204.239:8000/todo
- Size: 96.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Deployment of end-to-end application in node.js using Jenkins CICD, Docker, and AWS EC2 with GitHub Integration.
### Task/Achievements:
- Developed a Jenkins Continuous Integration and Continuous Deployment (CICD) Pipeline for a web application, streamlining the deployment process and improving efficiency.
- Implemented automation using webhooks in GitHub, enabling automatic triggers for the CICD pipeline upon code changes, reducing manual intervention, and accelerating the deployment process.
- Gained hands-on experience with Docker, Jenkins, and AWS-EC2, enhancing proficiency in DevOps practices and cloud technologies.# Step-By-Step Instructions to Setup the Project:
---
| | |
| --- | ----------- |
| **Objective** | - Deploy the end-to-end application in node.js using Jenkins CICD with GitHub Integration
- Trigger Jenkins pipeline automatically once the code is pushed on GitHub |
| **Approach** | - Using Amazon's Elastic Compute Cloud (EC2) for running applications on the Amazon Web Services (AWS) infrastructure
- Containerize application by creating Dockerfile
- Integrate GitHub with Jenkins using Webhook |
| **Impact** | - Jenkins pipeline triggers automatically once the code is pushed on GitHub- Accomplish faster quality releases by automating CI/CD pipelines |**Primary Technology:** Github, Docker, Jenkins, aws EC2 service

---## 1. Creating a Node App
Before we write any CI/CD pipeline we need an application to test and deploy. We are going to build a simple **to-do application** in node.js. Then, create a new repository under your GitHub account.
## 2. Creating AWS EC2 instance
### 2.1 Creating AWS EC2 instance
After logging into your AWS account, search for EC2
Select **Instances(running)**
Click **Launch instances**
Enter Name and select **Ubuntu**
Select **t2.micro** as Instance type and create new key pair to connect to the server
Enter **key pair name** and select **RSA** as Key pair type and **.pem** as Private key file format. Then, click **Create key pair**
Finally, click **Launch instance**

### 2.2 Connecting AWS EC2 instance
Click **Connect** on the top of the screen
Click **Connect**
## 3. Installing Jenkins on AWS
### 3.1 Installing Java
Install Java using following commands:
**sudo apt update**
**sudo apt install openjdk-22-jre**
**java -version**


### 3.2 Installing Jenkins
Install Jenkins using following commands:
**curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \ /usr/share/keyrings/jenkins-keyring.asc > /dev/null **
**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**
**sudo apt-get update**
**sudo apt-get install jenkins**

### 3.3 Start Jenkins
Start jenkins using following commands:
**sudo systemctl enable jenkins**
**sudo systemctl start jenkins**
**sudo systemctl status jenkins**
### 3.4 Open port 8080 from AWS Console
Go to Instances. Click on Security tab

Click on the link below Security groups

Click on **Edit inbound rules**

Click on **Add rule** and add port 8080 and select **My IP** and then click **Save rules**

### 3.5 Unlock Jenkins
Open port 8080 on new tab
On console, type the command **sudo cat /var/lib/jenkins/secrets/initialAdminPassword** and copy the password
Paste the password in Administrator password to unlock jenkins
Install suggested plugins

Create First Admin User
Enter Jenkins URL and click **Save and Finish**
Finally, Jenkins is ready. Click on the button **Start using Jenkins**
### 3.5 Connect GitHub and Jenkins
Create new job by clicking **New item**
Enter an item name. Select **Freestyle project**. Add description. Select **GitHub project** and add project url
Select **Git** as Code Management and add **Repository URL**. Click on **Add** to add key
Generate SSH key on console using following commands:
**ssh-keygen**
**cd .ssh**
**cat id_rsa_pub**
Copy the public key


Go to GitHub. Click on **Settings**
Click on **SSH and GPS keys** on the left pane and click on **Add SSH key**
Paste the SSH key and click **Add SSH key**

Go to Jenkins, and select **SSH Username with private key** in kind

On console, enter the command **cat id_rsa** and copy the private key

Paste the private key in jenkins wizard
Select **ubuntu(This is for github and jenkins integration)** in credentials
Enter ***/master** in Branch Specifier and click **Save**
### 3.6 Get code in jenkins
In jenkins, click on **Build Now** on the left pane
Now, click on **#1** and select **Console Output** to view the console

To check whether we got the code on EC2 instance, go to console, and enter the following commands:
**sudo cd /var/lib/jenkins/workspace/todo-node-app**
**ls**
Clearly, the code is present in this directory
### 3.7 Change permission of inbound traffic of port 8000
Click **Add rule** and enter port number 8000 and select **Anywhere IPv4** it can be accessed by anyone. Click **Save rules**

### 3.8 Run node.js application
On console, run the following commands:
**sudo apt install nodejs**
**sudo apt install npm**
**npm install**
**node app.js**



## 4. Automating using Docker
## 4.1 Install Docker
Remove Dockerfile and install Docker using following command:
**sudo rm Dockerfile**
**sudo apt install docker.io**
## 4.2 Create Dockerfile
Edit Dockerfile using the command **sudo vim Dockerfile** and add following commands within it:
**FROM node:12.2.0-alpine**
**WORKDIR app**
**COPY . .**
**RUN npm install**
**EXPOSE 8000**
**CMD ["node", "app.js"]**
## 4.3 Build Docker
On console, enter the following commands:
**sudo usernod -a -G docker $USER**
**sudo reboot**
To give permission to docker and reboot the system

After restarting, enter the following command:
**sudo build . -t todo-node-app**
## 4.4 Run Docker
After building docker, enter the following command:
**docker run -d --name node-todo-app -p 8000:8000 todo-node-app**
To check status of container **docker ps**

## 5. Automating using Jenkins (CICD pipelines)
### 5.1 Automating commands using Jenkins (Execute shell)
On console, enter the following commands to terminate a container:
**docker ps**
**docker kill **

On jenkins, go to dashboard and select the project. For me, it is **todo-node-app**
Click on **configure** on the left pane
Click on **Build Steps** on the left pane and then select **Execute shell** in the Build Steps section
Enter the following commands in the Execute shell to be executed:
**docker build . -t node-app-todo**
**docker run -d --name node-app-container -p 8000:8000 node-app-todo**
Click **Save**
Click on **Build Now** on the left pane

## 6. Trigger Jenkins pipelines automatically once the code is pushed on GitHub (Webhooks)
### 6.1 Install plugin
On console, enter the following commands to terminate a container:
**docker ps**
**docker kill **

On jenkins, go to dashboard and select the project. For me, it is **todo-node-app**
Click on **Manage Jenkins** on the left pane and then click on **Manage Plugins**
Search for **GitHub Integration** and select **GitHub Integration**. Finally, click on **Download without restart**

### 6.2 Configure inbound rules of port 8080
On aws, go to instances and click on **Security** pane
Edit inbound traffic of port 8080 to **Anywhere IPv4** and click **Save rules**

### 6.3 Configure webhook in GitHub
Go to GitHub and click **Settings**
Ensure that SSH and GPG keys are present on your GitHub account
Now, click on **repository settings**
Click on **Webhooks** on left pane and click on **Add webhook** to add a webhook
Add Payload URL and select content type as **application/json**
Finally, click **Add webhook**

### 6.3 Configure Jenkins
Go to Jenkins Job and click on **configure** on left pane
Click on **Build Triggers** on left pane and enable **GitHub hook trigger for GITScm polling. Finally, click **Save**
## Testing
Go to GitHub project repository and edit the project. Click **Commit changes**
Clearly, Jenkins pipeline are triggered as the code is pushed on GitHub


