Jenkins is a very popular open source tool to facilitate continuous integration and continuous delivery. It automates several stages of software development related to testing, building, and deployment. The integration of code management systems like GitHub with Jenkins, serves as a building block in modern software development processes.

We will create a freestyle Jenkins job and integrate it with a GitHub repository. Later, we will automate the job to instantly kick off after making a commit to the code.

What You’ll Learn

What You’ll Need

What You’ll Need to Set Up

Introduction

GitHub, Inc. is an Internet hosting service for software development and version control using Git. It is primarily used for storing, tracking, and collaborating on software projects. It lets you and others collaborate on projects from any part of the world.

Setting up the GitHub repository

Go to https://github.com/<YourUserId> to access your GitHub home page.

Step_1_GitHub_Homescreen_001.png

To create a repository, move to the Repositories section.

Step_1_GitHub_Homescreen_002.png

Click on the New button. It will take you to a new page where you will fill out your repository details.

Step_1_GitHub_Homescreen_003.png

Provide a Repository name and a suitable Description. Select the Public option to make the repository visible to everyone.

Step_1_GitHub_Homescreen_004.png

Keep the rest of the settings as default and click on the Create repository button, located at the bottom of the screen.

Step_1_GitHub_Homescreen_005.png

Click on creating a new file option to start creating your own python file.

Step_1_GitHub_Homescreen_006.png

We will be creating a trivial Python code file. Provide the name of the file with a .py extension and type the following lines of code:

print('This repo will be integrated to Jenkins job in order to automate the building phase')

Step_1_GitHub_Homescreen_007.png

Scroll down and click on Commit new file button.

Step_1_GitHub_Homescreen_008.png

Great Job! You have added your Python code file to the newly created repository.

Step_1_GitHub_Homescreen_009.png

Learn More

Introduction

We are going to create a Jenkins Freestyle job and integrate it into our GitHub repository. A Freestyle job is a stand-alone collection of steps that are not part of a pipeline. Freestyle jobs help us execute the task without running an entire pipeline.

Creating a Jenkins Freestyle Job

Access the hosted Jenkins server. The default Jenkins Port is 8080.

Step_2_Jenkins_001.png

Click on Create a job button to start creating a job.

Step_2_Jenkins_002.png

Provide the name of your job, Ex. github_integration. Choose Freestyle project and click on OK button, located at the bottom of the screen.

Step_2_Jenkins_003.png

Provide a Description of the job. For self-learning purposes, click on the ? symbol corresponding to each setting option to understand the setting.

Step_2_Jenkins_004.png

Scroll down to Source Code Management section and select Git. Provide the URL of the repository created previously. In order to make Jenkins successfully authenticate with your GitHub repository, you need to provide your GitHub credentials. Select the Add option. 

Step_2_Jenkins_005.png

For the Kind option, select Username with password. Enter your Username and Password.

Step_2_Jenkins_006.png

Click on the Add button to successfully add the credentials in Jenkins. Click on the Credentials dropdown and select the credentials you just added.

Step_2_Jenkins_007.png

Scroll down to the Branches to build settings and rename the branch to */main in the Branch Specifier option. Keep the rest of the settings as defaults. Click on the Save button, located at the bottom of the page.

Step_2_Jenkins_008.png

We have successfully created and configured a Jenkins job.

Step_2_Jenkins_009.png

Click on Build Now to initiate a build.

Step_2_Jenkins_010.png

Observe a green tick to the left of the build number, showing the last build was successfully completed. Click on the build number.

Step_2_Jenkins_011.png

Observe the GitHub repository pulled during the execution of the build. It is the same repository that we created previously and provided in the configuration settings of the job. Click on Console Output located to the left of the screen.

Step_2_Jenkins_012.png

Understand the flow of instructions in the Console Output.

Step_2_Jenkins_013.png

Well done! You have successfully created a Freestyle Job in Jenkins.

Introduction

We were manually starting the build by clicking on Build Now. We will now be automating the builds such that the process starts right after every commit is made to the repository. We will be creating a webhook on our GitHub repository, which will trigger the build process.

Automating builds

Webhooks are most commonly used to simplify communication between two applications, but they can also be used to automate Infrastructure-as-code (IaC) workflows and enable GitOps practices. To set up a webhook from GitHub, we need to provide Jenkins’ URL. But Jenkins is currently hosted on the local machine. That’s exactly where NGROK helps us. It will expose the Jenkins server to the Internet. Note that this is not recommended in a production environment.

To create a ngrok tunnel, enter the command ngrok http 8080 in the terminal. Note that the Jenkins server is running on 8080. From the command prompt, copy the forwarding URL.

Step_3_Webhook_001.png

To create a webhook, click Settings on GitHub. Click Webhooks, followed by Add webhook.

Step_3_Webhook_002.png

Step_3_Webhook_003.png

Step_3_Webhook_004.png

Paste the forwarding URL in payload URL and append /github-webhook/ to the URL. Disable SSL verification and click on Add webhook button.

Step_3_Webhook_004.png

Step_3_Webhook_006.png

We will now be integrating the webhook to our job. In Jenkins, open the job and click Configure.

Step_3_Webhook_007.png

Scroll down to Build Triggers option and check Github hook trigger for GITScm polling. Click on Save.

Step_3_Webhook_008.png

We will now make changes in python code file and trigger a build automatically. In GitHub, click on the .py file. Click on pencil icon to edit the file.

Step_3_Webhook_009.png

Add the following print statement:

print('We are now automating the Jenkins build')

Step_3_Webhook_010.png

Give a commit message and click on Commit Changes button.

Step_3_Webhook_011.png

Step_3_Webhook_012.png

Jump to your Jenkins screen and observe that a build is automatically triggered.

Step_3_Webhook_013.png

Open the build and click on Console Output. Observe the first line of the logs and compare it with the previous build that was run in previous task.

Step_3_Webhook_014.png

Great job! You integrated a webhook into Jenkins Job and automated builds.

Congratulations! You’ve successfully completed this tutorial!

You learned:

Learn More