Jenkins CICD with GitHub Integration

Muhammed RameezMuhammed Rameez
4 min read

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

Step 2: Install Jenkins on the server

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

Jenkins is Accessible

get this password from this

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

Set username and password

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.

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

Go to Github -> setting -> SSH key ->Add new key -> do cat id_rsa_pub in the EC2instance and copy the key -> give a name to the key -> paste the copied key.

Create a item in jenkins -> Add the URL of the repo

repo link:https://github.com/mhmdrameez/node-todo

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

In Source Core Management

Go to Add credentials

Click on Add -> save it

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

It will get build.

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

Now check the readme and install the necessary dependencies.

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.

Go to instance -> Security -> Edit inbound rules -> Add rule

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

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

Go to instance -> install docker

$ sudo install docker.io

Create your Dockerfile

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.

#docker build . -t todoapp

Image (todo:latest) has been created successfully.

Now we will create container from this image.

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

Container will be created after this.

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

It is accessible.

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

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

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 -> Manage Jenkins -> Manage Plugins -> Install github integration plugin

Go to repository settings -> webhook -> Add webhook -> Payload URL add jenkins URL here -> (http://18.181.84.9:8000/github-webhook/)

Content type -> application.json

Add webhook.

It has been added successfully.

Go to jenkins -> configure -> tick this

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

So the build is successful.

Resources used :
Github :https://github.com/mhmdrameez/node-todo

Notes : 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 ๐Ÿ˜„

๐‚๐จ๐ง๐ง๐ž๐œ๐ญ ๐ฆ๐ž ๐จ๐ง ๐‹๐ข๐ง๐ค๐ž๐๐ข๐ง ๐š๐ง๐ ๐‡๐š๐ฌ๐ก๐ง๐จ๐๐ž ๐Ÿ๐จ๐ซ ๐ฆ๐จ๐ซ๐ž ๐‹๐ข๐ง๐ฎ๐ฑ ๐š๐ง๐ ๐ƒ๐ž๐ฏ๐Ž๐ฉ๐ฌ ๐›๐ฅ๐จ๐ ๐ฌ.

0
Subscribe to my newsletter

Read articles from Muhammed Rameez directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Muhammed Rameez
Muhammed Rameez