Setting Up Your CI/CD Pipeline With Jenkins and GitHub
December 23, 2021

Setting Up Your CI/CD Pipeline With Jenkins and GitHub

Open Source Automation

CI/CD (Continuous Integration/Continuous Delivery) is a holistic DevOps process that focuses on creating a compatible blend between the development cycle and the operations process. This is done by automating workflows and rolling out automatic updates to improve ROI. The CI/CD pipeline implementation is the backbone of the entire DevOps paradigm and facilitates the process of introducing the product to the marketplace faster than ever before. 

In this article, we will be providing a comprehensive guide for creating and deploying your CI/CD pipeline with Jenkins and GitHub

Table of Contents:

What is Continuous Integration/Continuous Delivery?

Continuous Integration, which was first proposed as a term by Gary Booch, integrates the source code with the repository. This facilitates the developers to carry out the development process in a quick and sophisticated manner.

CI is not entirely an essential prerequisite required for creating a stable software product. However, it definitely serves an important role when developing software products or components that require frequent changes. Furthermore, it also ensures that all the components of an application are integrated properly. 

In the SDLC, CI mainly covers the Source and Build phases. A CI pipeline typically involves these steps:      

  • Detect changes in the code 
  • Analyze the quality of the source code
  • Build
  • Execute all unit tests
  • Execute all integration tests
  • Generate deployable artifacts
  • Report status

If any of the above steps fail, the integration stops immediately, and the team is notified about the result. 

Continuous Delivery, on the other hand, is a set of software development practices that ensures the deployment of code to production while performing efficient testing in the process. Precisely speaking, CD starts where CI ends. Continuous Delivery is responsible for pushing the code to the testing environment where different tests such as system testing, unit testing, and integration testing are performed.

A typical CI/CD pipeline works in 4 phases that are listed below:       

  • Phase 1: Commit - This is the actual phase where developers commit changes to the code.
  • Phase 2: Build – In this phase, the source code is integrated into the build from the repository. 
  • Phase 3: Test Automation - This step is an integral part of any CI/CD pipeline. The source code previously integrated into the build is subjected to a systematic cycle of testing.  
  • Phase 4: Deploy - The tested version is finally sent for deployment in this phase.

While Continuous Integration covers the commit and build stages, Continuous Delivery, on the other hand, ensures the process automation as well as testing till the deployment phase.

How Do You Create Your CI/CD Pipeline With Jenkins?

Jenkins is one of the most widely used open-source CI/CD DevOps tools. It enables developers to implement CI/CD pipelines within the development environment in a comprehensive manner. Jenkins is written in Java and supports various version control tools including Git and Maven.

Its popularity is based on the fact that Jenkins is an open source repository of rich integrations and plugins that are well documented and extensible, based on an extended community of developers who have developed hundreds of plugins to accomplish almost any task. 

Jenkins runs on the server and requires constant maintenance and updates. The availability of Jenkins as a cross-platform tool for Windows, Linux, and various operating systems makes it stand out among other DevOps tools. Moreover, it can easily be configured using CLI or GUI.

Why Use CI/CD with Jenkins?

The list of benefits of using CI/CD with Jenkins is a detailed one. The three main ones are:

1. Costs

While implementing CI/CD with Jenkins, developers and the DevOps team don’t need to worry about the additional procurement costs involved in setting up code pipelines, as Jenkins is free and open source. This simply means that they no longer have to get spending approval from management. As far as cloud and infrastructure costs are concerned, these costs can be optimized as well by efficiently utilizing the resources available and implementing the Infrastructure as Code (IAC) approach using Jenkins. 

2. Plugins

Another remarkable feature that highlights Jenkins’ value is the extended variety of Jenkins pluginsavailable in Jenkins. With this diverse range, users of different cloud providers can feasibly utilize CI processes via Jenkins in a significantly lesser time. Moreover, Jenkins also provides a default list of common tools and services that can be implemented either on-premises or on the cloud. Top Jenkins plugins include Dashboard View, Test Analysis Plugins, and Build Pipeline Plugins.

BlazeMeter also provides a plugin for running continuous testing as part of your CI/CD.

3. Open Source

Jenkins, with its long history of CI/CD practices, was introduced in the year 2011. Its availability on the open-source platform provides it an edge over other tools used for the same purpose. Numerous developers, community contributors, and users actively participate in the open-source platform to maintain Jenkin’s functionality as an open-source product.

Configuring Automated CI/CD with Jenkins & GitHub — Step by Step

Configuring automated CI/CD with Jenkins and GitHub is a simple and straightforward process and can help automate the entire workflow. Integrating Jenkins with GitHub enables the developers to pull the source code from any Git repository in a hassle-free manner.

Furthermore, GitHub also supports bi-directional integration, which will automatically initiate a trigger to Jenkins every time there is a change in the GitHub repository.

The step-by-step procedure for configuring Jenkins with GitHub is elaborated below. 

Installation

To configure automated CI/CD with Jenkins, the first and the most obvious step is to install Jenkins. Jenkins setup, with complete instructions on how to install it, is availablehere.

To implement it on the server or a remote computer, simply replace localhost with the IP Address of the target machine. Jenkins provides long-term support and weekly releases for several Operating Systems. For the scope of this blog, we will be setting up Jenkins on a local Ubuntu machine. To use the Ubuntu repository, execute the command in the terminal:

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

 

After that, add the Jenkins repository by executing this command:

sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ >

/etc/apt/sources.list.d/jenkins.list'

 

Make sure to update the packages using the sudo command as shown here:

sudo apt-get update

 

Finally, install Jenkins:

sudo apt-get install jenkins

 

Make sure to install Java Runtime Environment (JRE) explicitly after installing Jenkins. However, if you get a “package error”, you must check for the Java version by executing the command given below:

java -version.

 

If the package does not exist, then you will have to install it. For this, you will first search for the available packages:

sudo apt search openjdk

 

Next, pick the available version and install it.

sudo apt install openjdk-11-jdk

 

After the successful installation, check the Java version.

java -version

 

You will get a result like this:

Java

The second step is to run Jenkins. However, you can also run Jenkins on several platforms including Docker, Kubernetes, and Windows. Notice here that you will need to install Docker or Kubernetes if you are planning to run Jenkins on these platforms. In this tutorial, we will run Jenkins on localhost on Linux.

Starting the Server

After the successful installation of Jenkins, you will start it on localhost. For this step, you should start Jenkins first using the systemctl command on the command line. 

sudo systemctl start jenkins

sudo systemctl enable jenkins

 

To check the status of Jenkins service, use the status command as shown below:

sudo systemctl status jenkins

 

The output will be rendered as Active” if everything has been configured successfully.

Post-Installation Setup

When you configure Jenkins for the first time, it is crucial to unlock it using an automatic password. For this setup, typehttp://localhost:8080 in the browser, and wait for the Getting Started page to appear. The automatically-generated password is stored in /var/jenkins_home/secrets/initialAdminPassword file. The file can be also accessed via the command-line interface using the cat command with the filename. 

Copy/paste the password in the Administrator Password field and click Continue. The dialogue box will close and that’s it! You have successfully set up the user. After that, you will get the Customize Jenkins page. From this page, you can install any number of plugins you require. You can install suggested plugins as they are selected by default and you can also choose any additional plugins. Make sure to install the Git Plugin.

Configure GitHub

Now that you have successfully configured the user, it is time to configure the GitHub repository using Webhooks. Additionally, you can also perform this step after adding the Jenkins project. 

Head over to the existing GitHub repository, and click the Settings tab.

Settings tab when setting up CI/CD with GitHub


After that, click on the Webhooks option from the navigation panel on the left side of the screen.

Webhooks option on navigation panel.


Now, click on the Add webhook button on the right side of the screen.

Adding a Webhook in GitHub

You will see a new form appear in the webhooks section. In the Payload URL field, type in the Jenkins endpoint. In our case, it is localhost: 8080. Since the localhost is behind a Firewall, make sure to make it visible to your public GitHub repository by using a webhook proxy service. In our case, we have used SocketXP. If you have a specific URL, make sure that it is publicly available. At the end of the URL, type /github-webhook/.

 

Adding Webhook in CI/CD pipeline with Jenkins & GitHub.

Choose application/json as Content-Type from the drop-down menu, select Just the push event, and click the Add webhook button.

Selecting "Just the push" event

For now, this repository will only call Jenkins on the push event. For other events, you will have to select the Let me select individual events option instead of Just the push event.

That’s all! You are done with the configurations on the GitHub side.

Jenkins Project and Adding GitHub

After successfully logging in, the Jenkins dashboard will appear on the screen. Here, all the CI/CD pipelines and their summary are visible to a user. On the Jenkins dashboard, click on “New Item”. After that, enter the name of your project, choose “Freestyle Project” from the list, and hit the enter key.

Choosing a new project on the Jenkins CI/CD dashboard.

The next step is to configure the project on the Jenkins dashboard. In the General tab, choose your GitHub Project, and enter the GitHub Repository URL. Now, head over to the Source Code Management tab to add the credentials.

Choosing GitHub project in Jenkins CI/CD dashboard.



For this step, press the Add button and type Username and Password. Press Add again to close the dialogue box. Be sure to select the main branch in the Branch Specifier.

Now, head over to the Build Triggers section to set the triggers for Jenkins. The purpose of triggers is to necessarily indicate to Jenkins when to build the project. Select Poll SCM section, which queues VCS on the predefined schedule. The predefined schedule is ***** which represents every minute. That means, our Jenkins job will check for the change in our repository every single minute. 

Build Jenkins Project

Now go back to the repository that you configured, and make a sample file, or edit any existing file and commit the changes. 

Commit changes in Jenkins project

After that, head over to Jenkins and click on the “Build Now” button from the navigation bar.
 

"Build Now" button in Jenkins.


The build number will display a green tick if the build is successful, and the app will be published on the localhost:8080. You can also use the output console to debug the application.

Jenkins Console Output

As you can see, Jenkins pulled the changes from the GitHub repository and reflected them in its console.
 

Jenkins pulling changes from the GitHub repository.

Finally, you have successfully integrated Jenkins with the GitHub repository. With each push, commit, or update, GitHub will trigger the Jenkins job, which in turn will execute the necessary steps to deploy the changes.

Bottom Line

DevOps is an ever-evolving ecosystem in the development industry, and the CI/CD tools in this domain are highly critical. CI/CD practices effectively handle the misalignment between developers and the operational team. There are several tools built for this purpose in the marketplace today, but Jenkins holds an established position in the industry as well as a promising future ahead of it because of its open-source benefits and a wide range of plugins. Furthermore, it integrates well with GitHub allowing productivity and flexibility in the DevOps cycle.

We hope that this detailed guide will assist you in setting up your first Jenkins CI/CD build with GitHub repository successfully. Stay tuned for more tutorials and articles on the latest trends in the software and IT industry!

Start Testing Now