avatarAl-Waleed Shihadeh

Summary

This article provides a step-by-step guide on integrating GitHub webhooks with Jenkins pipelines to automate the execution of jobs in response to specific GitHub events.

Abstract

The guide begins by explaining the utility of GitHub webhooks for triggering actions based on various events such as code pushes or pull requests. It details the process of setting up a webhook in GitHub, including specifying the payload URL with the appropriate Jenkins URL and path. The article then proceeds to describe how to configure Jenkins to work with GitHub by installing the GitHub plugin and setting up a pipeline to respond to push notifications. It emphasizes the importance of using static values for the repository URL and branches within the pipeline configuration. The article concludes with a complete example of a Jenkins pipeline configured to execute tasks upon new commits to the master branch.

Opinions

  • The author suggests that automating actions through webhooks can streamline the development process, particularly for tasks like packaging and deployment after code changes.
  • The preference for disabling SSL verification is mentioned as an option if a valid SSL certificate for the Jenkins server is not available, indicating a pragmatic approach to development environments.
  • The article endorses using static values for the GitHub repository URL and branches in the Jenkins pipeline configuration, implying a best practice for reliability.
  • The recommendation for an AI service at the end of the article suggests the author's belief in the value and cost-effectiveness of such tools in enhancing productivity.

How to Add Github Webhooks to a Jenkins Pipeline

Trigger the execution of Jenkins jobs based on GitHub events

Photo by JJ Ying on Unsplash

Github provides a handy way to call webhooks’ URLs when certain events are triggered. The list of these events is long and includes: on pushing to a repository, on a pull request opening, closing, and reopening, on creation of a release, and many more. You can check the complete list here.

To make it simple, Github will perform a POST request with a payload object contains data about the triggered event to your defined webhook URL once the event is triggered. This will allow us to automate the executions of some actions based on the Github events. For example, if after each push to the master branch we want to package or deploy our software, we can simply add a webhook to the Jenkins task responsible for packaging or deploying the service.

In this post, I’ll show how we can define a Jenkins webhook and how to defile the Jenkins task that is linked to the Github webhook.

Adding Jenkins Webhook in Github

  1. Open Github repository.
  2. Go to “settings” and then to “hooks”.
  3. Click the “Add webhook” button.
  4. Fill in the form, as shown in the image below. For the payload URL, provide your Jenkins URL and the GitHub webhook path at the end of the URL — https://${jenkins_url}/github-webhook/. You can disable SSL verifications if you don’t have a valid SSL cert for your Jenkins server. Finally, choose the option “Calling the webhook only for push events.” (This actually depends on what you are trying to do. You can change this by selecting other options).

5. Finally, when all the changes have been made, click on “Add webhook”

Building a Jenkins Pipeline to Handle the Webhook Call

  1. Make sure you’ve installed the Github plugin and configured it on Jenkins. To configure the plugin you need to go the Jenkins Configure system page, scroll down to the GitHub section add a new GitHub instance, as shown:

2. The next step is to start writing the pipeline. The first line configures it to run whenever Jenkins receive push notifications from GitHub:

properties([pipelineTriggers([githubPush()])])

3. The next step is to configure the pipeline to be executed for the Github repository. This can be done by adding a checkout stage at the beginning of your pipeline. One important thing to note is that the url and branches attributes must be static values and can’t include any kind of variables.

3. The final step is to add the Jenkins Pipeline to the Jenkins server and execute it to load the configuration. The Jenkins task will be executed every time we push a new commit to the master branch.

Finally, here is a complete example of the Jenkins pipeline:

Jenkins
Github
Programming
Continuous Integration
Ci
Recommended from ReadMedium