Maximizing Efficiency: Jenkins Integration with GitHub

Maximizing Efficiency: Jenkins Integration with GitHub

In this article, we will explore how to integrate Jenkins with GitHub to automate CI/CD of our code. This setup ensures that each push to our repository automatically starts a new build on the Jenkins server. This is primarily done using a webhook. But first, let's clarify, what is Jenkins?

Jenkins is an open-source automation server commonly used for Continuous Integration (CI) and Continuous Delivery (CD) in software development. It assists in automating different stages of the software development lifecycle, such as building, testing, and deploying applications.

Key features of Jenkins include:

  1. Continuous Integration: Jenkins supports the continuous integration of code changes into a shared repository. It automatically starts build processes when new code is committed, aiding in identifying integration errors early in the development cycle.

  2. Extensibility: Jenkins provides a wide range of plugins that expand its capabilities to accommodate different tools, technologies, and processes.

We are going to use an EC2 instance running Ubuntu to host our Jenkins server. If you need instructions on launching an instance and SSHing into it, you can find them here (insert link here later). Also, I recommend using a t2.small or t2.medium instance type to ensure that Jenkins has sufficient resources to operate.

SSH into your EC2 instance and update package repository and upgrade packages using the commands below:

sudo apt update 
sudo apt upgrade

Installing Java

Jenkins requires Java to operate, so our first step is to install Java on the instance. We will utilize the adoptium package. To add the adoptium package, execute the following commands.

wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | tee /etc/apt/keyrings/adoptium.asc
echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | tee /etc/apt/sources.list.d/adoptium.list

Then install Java 17

sudo apt install temurin-17-jdk

Installing Jenkins

Add the Jenkins key repository and install Jenkins

sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

Start Jenkins

Enable Jenkins with the command below:

sudo systemctl enable jenkins

Start Jenkins with the command below:

sudo systemctl start jenkins

Check the status if Jenkins with the command below:

sudo systemctl status jenkins

The output should be as follows indicating the Jenkins service is running:

For any other operating system, please refer to the official documentation here.

To access your Jenkins server, obtain the public address of your EC2 instance and enter the following URL in your browser to reach the server. Remember to substitute Public_IP_Address with your actual IP address. Jenkins typically operates on port 8080.

 http://your_server_ip_or_domain:8080

Thereafter, follow the on-screen instructions to complete the installation.

For best practices, if you plan to use Jenkins for multiple builds, you need to create Jenkins agents that will allow for use of different environments and help distribute the workloads across multiple agents to run jobs in parallel. Tou can follow the tutorial here to create an agent or multiple if you plan to use this as your dedicated Jenkins server for future builds.

First, we are going to generate a GitHub Personal Access Token and add it to Jenkins as a credential. Here we name it jenkins-access-token.

On the Jenkins dashboard go to "Manage Jenkins" > "Credentials" and under the global scope add your GitHub token as Username with Password .

Click create to save the credentials.

Creating a pipeline project

We are going to use a Django project for this demo, but the steps should work for any project that uses pipeline syntax. Log into the Jenkins dashboard and, on the top left, select New Item to create a new job. Give the project a name (in this case, I'll name it "burymeahatchet") and then choose the pipeline option. Click on OK to create the job.

We are then prompted to configure the project. The most important step is to set the Git repository from which Jenkins will get the source code for building the project. This repository should contain a Jenkinsfile in the root directory.

Under build triggers, select "GitHub hook trigger for GITScm polling"..

Then, under the pipeline section, go to the SCM section. Select git as the SCM, provide the GitHub URL to your repository, and choose the credentials we created earlier if your project is private. You can leave this blank if the repository is public.

Scrolling down, you can add more configurations if needed, but I will leave mine as default.

Creating a webhook

Next, we will create a webhook to automatically build our code without human intervention. Every time a new change is pushed to GitHub, it will trigger a new build on Jenkins.

In the GitHub repository, under settings, click on Webhooks. Click on Add webhook and enter the Jenkins URL in the following format::

http://<jenkins_server>/github-webhook/

In our case the URL will look something like:

http://your_server_ip_or_domain:8080/github-webhook/

After configuring the webhook, click "Add webhook" to save.

This will automatically trigger a build on Jenkins. Returning to the Jenkins dashboard, you should see a new build. You can test the webhook by pushing a new change to GitHub and watching for new builds that you did not manually trigger.

The GitHub repository can be found here.

Happy hacking❤️!