Integrating GitHub with Jenkins using Webhooks

Modern software development heavily relies on continuous integration [CI]. It involves building and testing software automatically whenever changes are made to the codebase. Jenkins is a popular open-source CI tool that allows you to automate your code’s building, testing, and deployment.

In this tutorial, we’ll walk through integrating a GitHub repository with a Jenkins project using webhooks. By doing this, we’ll be able to automatically trigger a Jenkins build whenever changes are pushed to the repository, we will learn how to create and run a job in Jenkins using the Jenkins Freestyle project.

Prerequisites

You’ll need the following to follow along with this tutorial:

  • A GitHub account
  • A Jenkins instance
  • A GitHub repository that contains a project with a build script [e.g. a Jenkinsfile]

Configuring the Jenkins Project

First, we’ll need to configure our Jenkins project to listen for webhooks from GitHub.

1. First, create a new project by clicking on ‘New Item’. Choose ‘Freestyle project’ and give your project a name.

2. Under the ‘Source Code Management’ tab, select Git and paste your GitHub repository URL in the ‘Repository URL’ field.

3. Then Under “Build Triggers,” select “GitHub hook trigger for GITScm polling.” This will enable Jenkins to listen for webhooks from GitHub.

4. Save your changes.

Configuring the GitHub Repository

Next, we’ll need to configure our GitHub repository to send webhooks to Jenkins whenever changes are pushed to the repository.

1. Open your GitHub repository and navigate to the “Settings” tab.

2. Select “Webhooks” from the sidebar.

3. Click the “Add webhook” button.

4. In the “Payload URL” field, enter the URL of your Jenkins instance followed by /github-webhook/ [e.g. https://jenkins.example.com/github-webhook/].

5. In the “Content type” field, select “application/json.”

6. Under “Which events would you like to trigger this webhook?”. Select ‘Let me select individual events‘ and check ‘Pull requests’ and ‘Pushes‘.

7. Save your changes.

Triggering a Build

With our Jenkins project and GitHub repository now connected, we can trigger a build by pushing changes to the repository.

1. Make a change to your project’s codebase and commit your changes. Then Push your changes to the GitHub repository.
2. Open your Jenkins instance and navigate to your project’s dashboard.
3. You should see a new build has been triggered. Click on the build to view the build log and results.

Congratulations! We’ve now successfully integrated our GitHub repository with our Jenkins project using webhooks. Once changes are pushed to the repository, Jenkins will automatically trigger a build and test your code. Thanks for reading, and be sure to follow us for more insights and tips on DevOps and Cloud.

Leave a Reply

Your email address will not be published. Required fields are marked *