Tuesday 15 May 2018

Continuous Integration with Jenkins and GitHub to automate the deployment


Continuous Integration (CI) is rapidly becoming an integral part of software development process as it makes our monotonous and repetitive tasks a little and execute it with ease. CI is a project development practice where developers integrate code into a repository frequently. Each Integration is then verified by an automated build that allows the team to detect problems in an early stage. 

Let me walk through continuous integration with Jenkins and GitHub. Here we will install Jenkins, create Jenkins tasks and then configure it to GitHub. When creating Jenkins tasks I'm going to use PHPUnit tests in build steps to detect problems.


Why Jenkins is so popular in Continuous Integration?

Jenkins is an open-source tool that tests and compiles the code. If everything is fine then it deploys the code for production else you will get a notification that builds has failed and you need to fix the error.

Advantages of Jenkins

  • Easily configurable
  • Platform independent
  • Rich plugin support
  • Easy to create new Jenkins plugin if one is not available
Now let's follow the below steps to configure integration with Jenkins and GitHub.

Step 1 : Add the key and source list to apt for Jenkins.

$ wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
When the key is added, the system will return OK.

Step 2 : Append the Debian package repository address to the server's sources.list
$ echo deb https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list
Step 3 : Update the packages

$ sudo apt-get update
Step 4 : Now install Jenkins
$ sudo apt-get install jenkins
Step 5 : Start Jenkins using systemctl.
$ sudo systemctl start jenkins
Step 6: Since above command does not provide an output, we should run status command to check the status of the Jenkins server
$ sudo systemctl status jenkins
If everything goes well the output of the above command shows the service as active.
jenkins.service - LSB: Start Jenkins at boot time
  Loaded: loaded (/etc/init.d/jenkins; bad; vendor preset: enabled)
  Active:active (exited) since Thu 2017-04-20 16:51:13 UTC; 2min 7s ago
    Docs: man:systemd-sysv-generator(8)

Step 7:  By default, Jenkins use 8080 port. Change the port to custom and allow it outside to access it from the browser.
$ vi /etc/default/jenkins
Replace the HTTP_PORT with custom.
HTTP_PORT=7011
Restart the Jenkins service
$ sudo service jenkins restart

Step 8: Setting up Jenkins
To setup Jenkins we need to visit Jenkins using the custom port as shown below.
http://ip_address_or_domain_name:7011

We should see "Unlock Jenkins" screen which displays the location of the initial password.

Go to below path and copy the 32 bit character string and paste it and continue.
sudo cat /var/lib/jenkins/secrets/initialAdminPassword /

Next screen shows the options of install suggested plugins or select specific plugins. Select the instal suggested plugin option in our case.




Once the installation finish,we will be prompted to create our first administrative user as shown below.


Once we save and finish we are done with the setup of Jenkins and now we can move to GitHub integration.




Note : The default Jenkins server is NOT encrypted, so the data submitted with this form is not protected. When you're ready to use this installation, follow the guide How to Configure Jenkins with SSL using an Nginx Reverse Proxy This will protect user credentials and information about builds that are transmitted via the Web interface

Step 9: Creating a Jenkins job for GitHub repository
  • To Create a new task for Jenkins, click on “New Item” then enter an item name that is suitable for your project and select Freestyle project. Now click 'Ok'.


  • Select the GitHub project checkbox and set the Project URL to point to your GitHub Repository.


  • Under Source Code Management tab, select Git and then set the Repository URL to point to your GitHub Repository.
  • Now Under Build Triggers tab, select the “Build when a change is pushed to GitHub” checkbox.


  • At the end, execute Shell script as shown below. When the configuration is done, click on save button.

Note: Refspec in Source Code Management has to be added exactly as shown below where you only change the branch accordingly.


+refs/heads/master:refs/remotes/origin/master

Step 10: Configure GitHub for Jenkins

  • Install the Jenkins (GitHub plugin) on a git repository.
  • Now set the Jenkins hook URL as the URL for your machine as shown below.
http://ipaddress_or_domain:7011/github-webhook/

Finally,every time you publish changes to GitHub, it will trigger new Jenkins job. Now you know an entire process of continuous integration with Jenkins and GitHub. 

Is there anything you'd like to clarify about Jenkins and GitHub? Let me know in the comments below!

No comments:

Post a Comment

Integrating SonarQube with Jenkins for PHP

SonarQube  is a quality management tool popularly used among 85000 companies around the world. It has four editions. Community Edition ( ...