Saturday, 2 May 2020

Jenkins Integration with Git and GitHub.

In this article you will see how we can integrate Jenkins with Git and GitHub, which makes developer and operational team automatic.


To start with article first we understand the little background of Git, GitHub and Jenkins.

Jenkins : 

Jenkins is one of the most powerful tool in DevOps world. Jenkins can do continuous development and continuous deployment automatically this makes Jenkins very special. Jenkins power comes from plugins.

GitHub:

GitHub is a system which provide centralized data sharing for remote world. GitHub works on centralized version control system (CVCS). GitHub require Network connectivity. GitHub have graphical user interface.

Git:

Git is a system which provide local data sharing. Git works on decentralized version control system (DVCS). Git don't require network connectivity when it work locally. If you have network connectivity in your local system you can pull (download) or push (upload) your file from GitHub.Git have command line interface.

Let see some basic Git command :


- mkdir project
(It create a workspace in local system for developer)

- ls
(It shows file list of dictionary)

- git init
(It initialize local git repository)

- notepad web.html
(write program/code in notepad)

- git status
(It shows the file info.)

- git add web.html
(file go into staging area and now git will track the file)

- git commit web.html - "first"
(Copy this file in commit area and create a version of file)

- git log
(If any changes or editing done log will keep info. with version Id)

- git remote add origin "GitHub URL"
(giving GitHub URL to Git so that it will push the file at this location)

- git remote -v
(It check remote connectivity)

- git push --set-upstream origin master
(Finally file pushed on GitHub)

Setup :

So for practical I am using Linux OS and windows OS. Using virtualization I installed linux OS on top of windows OS. Linux OS will act as a webserver where I am going to upload a file/project for client and I will use my windows OS for client to access the webserver.

I installed Jenkins in Linux OS. Here I started my webserver service and Jenkins. I stopped my systems firewall for client, to access my webserver. It is very critical if we stop firewall but for practical you can do it. When your practical is done you can enable firewall.



I installed ngrok software in my Linux OS so that I can make my private IP to public IP. Why I am doing this? As we know that from Private IP you can access Public IP but from Public IP you can't accesss private IP. Jenkins is running on private IP and Github is running on public ip so to give access of our Jenkins to GitHub we have to make our systems IP public. So for this the tunnel we use is ngrok. That's why we give ngrok url in webhook. you can see red color highlighter is my systems public IP.



WorkFlow of our practical : 


Step 1: Setting up local hooks 

Here we are making developer work very easy. we are creating hooks/program. We are using post-commit hook means as soon as the developer do the commit, file will automatically push to GitHub.

Step 2: Setting up Plugins with GitHub

Now open Jenkins using public IP and create a new Item(project).


Open Jenkins project and download the GitHub plugins. In source code management select Git and paste the URL of GitHub repository. We are doing this because Jenkins will download the Git repository file in Jenkins workspace when we run build.




To make the continuous deployment we are going to use triggers. Here we are using GitHub hook trigger for GITScm poling. This trigger helps to integrate with GitHub. In GitHub we are going to use Webhook, the work of Webhook is that whenever a new file is pushed in GitHub repository GitHub will inform Jenkins and Jenkins will automatically download and save the file in his workspace. 


We are using execute shell in Jenkins because in it we will deploy the file on webserver. cp   *   /var/www/html(cp is nothing but a copy command, it will copy the files from Jenkins workspace and deploy at webserver's location)

  

Step 2 : Setting Up Webhooks in GitHub for Jenkins:


To setup Webhook with Jenkins go to setting of GitHub repository - select webhooks and add webhook.


When webhook page open paste jenkins URL in Payload URL and select the content type json finally click on add Webhook. Now GitHub is hook with Jenkins. 



Now the setup & workflow is done let start the practical : 


1. Checking current firstweb.txt file content on GitHub repository




2. Done some edition in firstweb.txt file.

3. Here I had done commit you can see file is pushed automatically on GitHub repository.



4. Here we can see that GitHub's Webhook worked successfully and Jenkins        downloaded the file in Jenkins workspace.



5.Here we can see the client can access the webserver. Page is same that I edited.


Big Thanks to my mentor Mr. Vimal Daga Sir.