How to integrate GitHub repository with Jenkins using webhooks - cloudeguru/Jenkins GitHub Wiki

In this post we will see step by step procedure on how to connect GitHub with Jenkins using webhooks so that on every commit changes will be pushed to Jenkins for Continuous Integration and Continuous Delivery.

The integration between GitHub and Jenkins using webhooks works by sending HTTP POST requests from GitHub to the Jenkins server. Here's how it works in more detail:

Setting Up Webhooks in GitHub:

When you configure a webhook in your GitHub repository, you provide a URL to which GitHub should send webhook payloads. This URL is typically the Jenkins server's URL followed by /github-webhook/.

GitHub Webhook Payload:

Whenever an event occurs in your GitHub repository (such as a push to a branch or a new pull request), GitHub generates a webhook payload containing information about the event. This payload includes details such as the event type, the repository affected, and other relevant data.

Sending HTTP POST Request:

GitHub sends an HTTP POST request to the webhook URL configured in your Jenkins server. This request contains the webhook payload as JSON data in the request body.

Jenkins Receives the Request:

Jenkins listens for incoming webhook requests on the /github-webhook/ endpoint. When Jenkins receives a webhook request from GitHub, it processes the payload to determine the type of event that occurred (e.g., push, pull request, etc.).

Triggering Jenkins Job:

Based on the event type and any configured triggers (such as "GitHub hook trigger for GITScm polling"), Jenkins triggers the appropriate job or pipeline. This starts the build process in Jenkins, which can include tasks such as cloning the repository, building the code, running tests, and deploying artifacts.

## Steps:

# Install GitHub Plugin in Jenkins:

Go to your Jenkins dashboard. Click on "Manage Jenkins" -> "Manage Plugins". Go to the "Available" tab and search for "GitHub Plugin". Install the plugin and restart Jenkins if prompted.

# Configure Jenkins Credentials for GitHub:

Go to "Manage Jenkins" -> "Manage Credentials". Click on "Global credentials (unrestricted)" -> "Add Credentials". Choose the appropriate credential type (e.g., Username with password or SSH key) and provide your GitHub credentials. Save the credentials.

# Set Up Webhook in GitHub:

Go to your GitHub repository. Navigate to "Settings" -> "Webhooks". Click on "Add webhook". In the "Payload URL" field, enter the URL of your Jenkins server followed by /github-webhook/ (e.g., http://your-jenkins-server/github-webhook/). Don't forget the / at the end of this jenkins url Choose the content type as application/json. Select the events you want Jenkins to trigger builds for (e.g., "Pushes" or "Pull requests"). Click "Add webhook" to save.

## Settings:

## Webhooks:

## Add Webhook

## Add Jenkins URL and Choose Content Type as Application/Json

## Choose an event for which a webhook trigger is required

## Now verify if webhook is working fine:

Click on your added webhook

If there is green tick on recent deliveries tab the connection is sucessful

In Jenkins server choose GitHub hook trigger for GITScm polling option.

For more such content please subscribe to our Youtube Channel KloudBlogs Youtube