# Jenkins CICD with GitHub Integration

In this article we will be deploying a node.js application on EC2 instance and we will create a CICD pipeline using Jenkins.

Tools we will be using in the project:

1. AWS-EC2
    
2. GitHub
    
3. Docker
    
4. Jenkins
    

## **What is CICD pipeline?**

CI/CD (Continuous Integration/Continuous Deployment) pipeline is a set of automated processes that helps to integrate code changes, build, test, and deploy applications continuously. The primary goal of a CI/CD pipeline is to enable fast and reliable delivery of changes to production.

A typical CI/CD pipeline consists of several stages, including:

1. Code Integration: In this stage, developers integrate their code changes into a shared repository.
    
2. Build: In this stage, the CI system builds the code and runs any necessary tests.
    
3. Test: In this stage, the code is tested using various testing techniques, including unit tests, integration tests, and end-to-end tests.
    
4. Deployment: In this stage, the code is deployed to production or a staging environment.
    
5. Monitoring: In this stage, the system monitors the deployed application for performance and stability.
    

## **Step 1: Create EC2 instance and ssh into it**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677858082903/1a99750e-935c-45d0-b2fb-631656cda1a7.png align="center")

## **Step 2: Install Jenkins on the server**

Use the below link for the installation.  
[https://medium.com/@mhmdrameez/install-jenkins-on-aws-59ee029cd122](https://medium.com/@mhmdrameez/install-jenkins-on-aws-59ee029cd122)

Jenkins is Accessible

![](https://miro.medium.com/max/875/1*JQcBZXWGdeWePx8TUqZRUw.png align="left")

get this password from this

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677858439857/65829bfc-6afb-4e6c-b913-9bb71cec71d6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677858627104/5f642558-c14e-4790-894e-671dcd630b72.png align="center")

Set username and password

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677858653191/783b3ba4-889c-4114-994f-b9ded038f16b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677858679647/507a4126-94b3-4013-ab99-41d9f0e4a92e.png align="center")

Before starting with the Jenkins configurations we need to add the public key so that we can create a bridge between jenkins and Github for accessing the source code from the Github repo.

Go to your EC2 instance and type command

#ssh-keygen

It will create 2 keys. CD to .ssh and check the keys.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677858722469/eef04953-e7f1-472b-bede-67493e87019d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677858733103/28bc4e3f-d0b2-4853-9127-c99e609a353b.png align="center")

We need to add the Public key to Github so that we can create the bridge.

Go to Github -&gt; setting -&gt; SSH key -&gt;Add new key -&gt; do cat id\_rsa\_pub in the EC2instance and copy the key -&gt; give a name to the key -&gt; paste the copied key.

Create a item in jenkins -&gt; Add the URL of the repo

repo link:[https://github.com/mhmdrameez/node-todo](https://github.com/mhmdrameez/node-todo)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677858820493/8864a3ef-1ca8-4139-8ac2-af171d373114.png align="center")

Now we will add the credentials so that jenkins can access the code from the Github.

In Source Core Management

Go to Add credentials

![](https://miro.medium.com/max/875/1*gnD0CDC4jZMSursy8zPCJw.png align="left")

![](https://miro.medium.com/max/875/1*b0N77LD3Ye1iotYvL6n0_g.png align="left")

![](https://miro.medium.com/max/875/1*dN8c20NBXsCV84bbBSSGug.png align="left")

Click on Add -&gt; save it

Now we are all ready to Build the job now. Click on Build now.

It will get build.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677858998323/2f7ff303-39b7-410a-baca-e047e1536899.png align="center")

Go to instance and check if the repo is cloned there.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677859034747/2972aa33-c6d9-4645-81ee-5fd6eee49d12.png align="center")

Now check the readme and install the necessary dependencies.

![](https://miro.medium.com/max/764/1*aF07d0L-_kb7xPJk5rcOkQ.png align="left")

After executing all these commands. check if you can access the URL . It is not accessible as we have not given access to the port 8000.

![](https://miro.medium.com/max/875/1*asJZhApLpvIhOpeIJMcj8Q.png align="left")

Go to instance -&gt; Security -&gt; Edit inbound rules -&gt; Add rule

![](https://miro.medium.com/max/875/1*aB7WpXOYYfRaQLOQnaaj6Q.png align="left")

Save it.

Now take public ip of the instance and port 8000 and you can access the application.

adding the public ip with port number 8000

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677859152214/23bbe9e7-b20c-4350-a503-3700bc66d5ca.png align="center")

Now we will dockerize the application so that it can be accessed anywhere by anyone.

Go to instance -&gt; install docker

```plaintext
$ sudo install docker.io
```

Create your Dockerfile

```plaintext
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
EXPOSE 8000
CMD ["node","app.js"]
```

Now build the image using this Dockerfile.

```plaintext
#docker build . -t todoapp
```

![](https://miro.medium.com/max/875/1*sGRJRxFhaXtuEn9boDh7RA.png align="left")

Image (todo:latest) has been created successfully.

Now we will create container from this image.

```plaintext
#docker run -d --name todoappnode -p 8000:8000 todo:latest
```

Container will be created after this.

![](https://miro.medium.com/max/875/1*FUovO8-F3f8M9V8nGG-NJw.png align="left")

Try accessing the same using the public IP and port 8000.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677859500610/1ca891ce-ae86-41e3-8079-2c042c7a9750.png align="center")

It is accessible.

Now we will automate this process by adding the commands in the shell.

![](https://miro.medium.com/max/875/1*qprtpVuYWgLvzIZ4qMZH2w.png align="left")

Build is successsful. We can access the same on browser.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677859538952/b517acb5-ecec-4df9-958a-c470a8490239.png align="center")

Now all the processes that we were doing manually will be performed automatically as we did the automation.

Now we will configure web-hook so that every time there is any updation, deletion on the repository the job should be automatically triggered and it should perform the upcoming processes.

Kill the existing container first.

Jenkins -&gt; Manage Jenkins -&gt; Manage Plugins -&gt; Install github integration plugin

Go to repository settings -&gt; webhook -&gt; Add webhook -&gt; Payload URL add jenkins URL here -&gt; ([http://18.181.84.9:8000/](http://18.181.84.9:8000/)[github-webhook/](http://13.231.214.11:8080/github-webhook/))

Content type -&gt; application.json

Add webhook.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677859636828/c2ef1d70-3dd8-4ddf-996e-31d9c8e5a452.png align="center")

It has been added successfully.

Go to jenkins -&gt; configure -&gt; tick this

![](https://miro.medium.com/max/875/1*IlBC3arlt-bUvlF7iAnUig.png align="left")

Now just update something in the repo and your job will be triggered automatically.

![](https://miro.medium.com/max/835/1*-aEJTfUVr8VthUCq3D6RZw.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677859538952/b517acb5-ecec-4df9-958a-c470a8490239.png align="left")

So the build is successful.

Resources used :  
Github :[https://github.com/mhmdrameez/node-todo](https://github.com/mhmdrameez/node-todo)

Notes : [https://docs.google.com/document/d/1qos4eUfY4vZojjnZLSGw8D3A46Yy2r42uiZPyPxL17A/edit#](https://docs.google.com/document/d/1qos4eUfY4vZojjnZLSGw8D3A46Yy2r42uiZPyPxL17A/edit#)

**Please, feel free to drop any questions in the comments below. I would be happy to answer them.**

**If this post was helpful, please do follow and click the like** ❤️ **button below to show your support 😄**

𝐂𝐨𝐧𝐧𝐞𝐜𝐭 𝐦𝐞 𝐨𝐧 𝐋𝐢𝐧𝐤𝐞𝐝𝐢𝐧 𝐚𝐧𝐝 𝐇𝐚𝐬𝐡𝐧𝐨𝐝𝐞 𝐟𝐨𝐫 𝐦𝐨𝐫𝐞 𝐋𝐢𝐧𝐮𝐱 𝐚𝐧𝐝 𝐃𝐞𝐯𝐎𝐩𝐬 𝐛𝐥𝐨𝐠𝐬.
