DevOps Project - 2

Jenkins CI/CD with GitHub integration

DevOps Project - 2

In this blog, we are going to create a CI/CD pipeline. Let us take a look at what things are going to be involved in this project.

  1. First, we will be having some code. Here is the link. Fork it. It is a nodejs app. Being a DevOps engineer you don't need to focus on the code.

  2. Then we need Docker.

  3. We also need an AWS EC2 instance where we will be running our code.

  4. Jenkins, using which we will be creating our CI and CD pipeline.

That was the theory part. Now let us move to the practical part.

Create an AWS EC2 instance

Select Ubuntu as the operating system. Select the instance type as t2.micro because we don't need to do any heavy task on our instance and it's also free tier eligible.

After successfully launching the instance, connect to the instance.

Now since we need to create a CI and CD pipeline, we need to install Jenkins on this EC2 instance. Here is the guide using which you can easily install Jenkins on your EC2 instance and run it, click here.

Since Jenkins runs on port 8080, we need to enable port 8080 on our EC2 instance to be accessed globally. For that go to the 'Edit inbound rules' under Security groups, and add the port 8080, save the changes.

After the successful installation of Jenkins, add the password and install the suggested plugins. Then create the first admin user. After all this, you will be at the home page of Jenkins.

Create a freestyle pipeline

Click on 'new item', name it 'todo-node-app' and choose the option 'freestyle project'. Click on OK.

Then give any description as you wish.

Now since it's a project that is on GitHub, tick the 'GitHub project' option.

Now give the URL of the project which you will get from your forked repo.

Since we are taking the source code from GitHub, tick the option 'git' under 'Source code management', and give the same URL as we gave in the previous step.

Then you need to add the credentials. For that go to the EC2 instance and give the command,

ssh-keygen

This command will generate a public and a private key pair. Now use this command to go inside the ssh folder,

cd .ssh

ssh is a secret folder that has keys stored in it. It has two files- id_rsa(private key) and id_rsa.pub(public key).

Now if we want to connect GitHub to our server, then we need to give it the public key. Here is how to do that.

  1. Open your GitHub account and go to the settings.

  2. Then go to the option 'SSH and GPG Keys'. Click on 'New SSH key'.

  3. Give it the title 'Jenkins-project'.

  4. Now to fill the 'key' section, go to your EC2 instance terminal. Run the command 'cat id_rsa.pub', copy the public key and paste it under the 'key' section.

  5. Click on 'Add SSH key'.

Now GitHub has the public access to connect to our EC2 instance.

Now come back to our Jenkins where we left off. Let us now add the credentials. Click on 'Add'. Select 'Jenkins' as shown in the screenshot here.

You will see this type of screen.

Select the options as mentioned below:

  1. Under 'kind' select 'SSH Username with private key'.

  2. Under 'ID' type 'github-jenkins'.

  3. Under 'Description' type 'This is for Jenkins and GitHub integration'.

  4. Under 'Username' type 'ubuntu'.

  5. Under 'Private key' click on 'Enter directly'. Now go to your EC2 instance and run the command 'cat id_rsa', copy the private key and paste it here.

  6. Finally, click on 'Add'.

And then select the Credential we just created.

Leave the rest of the options as it is and click on 'Save'.

Till now we have created a bridge between Jenkins and GitHub.

Now click on 'Build Now'.

If you follow every step properly, then you will see that our Build is successful.

Now we know how to bring code from GitHub to Jenkins, but how to run that code? If we see the README file of the project, then it asks us to run these commands:

sudo apt install nodejs
sudo apt install npm
sudo npm install
node app.js

Now you will see an output saying 'app running on port 8000' or something like that. We need to open port 8000 first, and for that follow the same steps we did for port 8080 earlier. Go to security groups inside your instance, click on edit inbound rules add port 8000 and in source select 'anywhere'. Save it.

Now open port 8000 followed by the IP address of your EC2 instance, your instance should be running.

Creating the Dockerfile

Now we need to create our app in such a way that it runs anywhere. So we need to create a Dockerfile.

First, we should install Docker on our EC2 instance. Run the command:

sudo apt install docker.io

Now here is the Dockerfile:

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

Now we need to give the required permissions. Run the command:

sudo usermod -a -G docker $USER

Now we need to reboot the system. Run the command:

sudo reboot

Now we need to build the container from our Dockerfile. run the command:

docker build . -t todo-node-app

Our image will start to build and will take a few seconds.

Let us now run the container. Run the command:

docker run -d --name node-todo-app -p 8000:8000 todo-node-app

Now if again access our application on port 8000 followed by the IP address of our EC2 instance, we will be able to access our application again.

Till this point, you have integrated the GitHub's code using Docker. Continuous Integration is done.

Automating the whole stuff

Now what we need to do is automate the whole stuff that we did manually.

First stop and remove the container.

What we want to automate is that Jenkins brings the code from GitHub, creates an image from the Dockerfile, and creates a Docker container from that image and things should work fine.

First, go to the Configure option.

Then go to 'Build Steps' then 'Execute shell' and write the Docker commands that we entered manually, as shown in the screenshot below. Press save.

Now we need to give some permissions. Run the command:

sudo usermod -a -G jenkins
sudo systemctl restart jenkins

Now click on 'Build now'.

Your build will be successful and your application will run fine.

Continuous Deployment

So the last thing remaining in the project is to completely automate it. What I mean is that we have to click on the 'Build now' button, but what if we don't even need to do that also. Imagine whenever there is a change in the GitHub repo, your application should automatically build without even clicking on 'Build now'. This is known as Continuous Deployment.

The concept we are going to talk about is webhook. It means that GitHub and Jenkins are connected in such a way that whenever there is a change in the GitHub repo, there is a trigger in Jenkins.

First of all, stop and remove the running container. Now you need to install a plugin named 'GitHub Integration'. This plugin is for integration with Jenkins and GitHub.

Now we need to go to GitHub and configure webhook. Go to your forked repository and go to the settings of this repository as shown in the screenshot below.

On the left side you will see the option 'webhooks', click on that. Then inside that click on 'Add webhook'.

Now it will ask you for the payload URL, in which you only have to give the URL of your Jenkins and after that add 'github-webhook/'. For example, 18.203.29.10:8080/github-webhook

Select a content type as 'application/json'

Click on 'Add webhook'.

You will see a dot before our webhook. Refresh once and you will see a tick before it.

Now, webhook is integrated with Jenkins.

Now you need to go to your job on your Jenkins dashboard. Then configure. Then go to the 'Build Triggers' option. Tick the option 'GitHub hook trigger for GITScm polling'. Hit save.

Now whenever there is a change in the GitHub repo, the Jenkins build will start automatically.

Outro

Congratulations, finally we did it. We created a Jenkins CI/CD with the GitHub Integration project.

Stay connected with me for more blogs in this DevOps Project blog series.