/Deploy-V2

Repository includes all files for the application, dockerfile. Integrated with jenkins using web-hooks.

Primary LanguageJavaScript

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

Design Thinking Whiteboard in Yellow Blue Basic Style



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. 1

2. Creating AWS EC2 instance

2.1 Creating AWS EC2 instance

After logging into your AWS account, search for EC2 2

Select Instances(running) 3

Click Launch instances 4

Enter Name and select Ubuntu 5

Select t2.micro as Instance type and create new key pair to connect to the server 6

Enter key pair name and select RSA as Key pair type and .pem as Private key file format. Then, click Create key pair 7

Finally, click Launch instance 8 9

2.2 Connecting AWS EC2 instance

Click Connect on the top of the screen 10

Click Connect 11

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
12 13 14

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
15 16

3.3 Start Jenkins

Start jenkins using following commands:
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
17

3.4 Open port 8080 from AWS Console

Go to Instances. Click on Security tab 18 Click on the link below Security groups 19 Click on Edit inbound rules 20 Click on Add rule and add port 8080 and select My IP and then click Save rules 21 22

3.5 Unlock Jenkins

Open port 8080 on new tab 23

On console, type the command sudo cat /var/lib/jenkins/secrets/initialAdminPassword and copy the password 24

Paste the password in Administrator password to unlock jenkins 25

Install suggested plugins 26 27

Create First Admin User 28

Enter Jenkins URL and click Save and Finish 29

Finally, Jenkins is ready. Click on the button Start using Jenkins 30

3.5 Connect GitHub and Jenkins

Create new job by clicking New item 32

Enter an item name. Select Freestyle project. Add description. Select GitHub project and add project url 33

Select Git as Code Management and add Repository URL. Click on Add to add key 34

Generate SSH key on console using following commands:
ssh-keygen
cd .ssh
cat id_rsa_pub
Copy the public key 35 36 37

Go to GitHub. Click on Settings 38

Click on SSH and GPS keys on the left pane and click on Add SSH key 39

Paste the SSH key and click Add SSH key 40 Go to Jenkins, and select SSH Username with private key in kind 41 On console, enter the command cat id_rsa and copy the private key 42 Paste the private key in jenkins wizard 43

Select ubuntu(This is for github and jenkins integration) in credentials 44

Enter */master in Branch Specifier and click Save 45

3.6 Get code in jenkins

In jenkins, click on Build Now on the left pane 46

Now, click on #1 and select Console Output to view the console 48 49

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 50

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 54 55

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
51 52 53 56

4. Automating using Docker

4.1 Install Docker

Remove Dockerfile and install Docker using following command:
sudo rm Dockerfile
sudo apt install docker.io
57

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"]
58

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 59 After restarting, enter the following command:
sudo build . -t todo-node-app
63

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
64 65

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
68 69

On jenkins, go to dashboard and select the project. For me, it is todo-node-app 71

Click on configure on the left pane 72

Click on Build Steps on the left pane and then select Execute shell in the Build Steps section 73

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 74

Click on Build Now on the left pane 75 76

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
77 78

On jenkins, go to dashboard and select the project. For me, it is todo-node-app 79

Click on Manage Jenkins on the left pane and then click on Manage Plugins 80

Search for GitHub Integration and select GitHub Integration. Finally, click on Download without restart 82 83

6.2 Configure inbound rules of port 8080

On aws, go to instances and click on Security pane 88

Edit inbound traffic of port 8080 to Anywhere IPv4 and click Save rules 89 90

6.3 Configure webhook in GitHub

Go to GitHub and click Settings 84

Ensure that SSH and GPG keys are present on your GitHub account 85

Now, click on repository settings 86

Click on Webhooks on left pane and click on Add webhook to add a webhook 87

Add Payload URL and select content type as application/json 91

Finally, click Add webhook 92 93

6.3 Configure Jenkins

Go to Jenkins Job and click on configure on left pane 94

Click on Build Triggers on left pane and enable **GitHub hook trigger for GITScm polling. Finally, click Save 95

Testing

Go to GitHub project repository and edit the project. Click Commit changes 96

Clearly, Jenkins pipeline are triggered as the code is pushed on GitHub 97 98

99