Before we get started, I assume you already know how to configure pipelines in Jenkins. Therefore, configuring the pipeline from scratch is not done in this article. If you are not familiar with configuring the pipeline, then it would be better to go through the previous articles in this Jenkins series before diving into this one.
So far we configured pipelines in Jenkins and triggered them manually. How can we configure them to run automatically, like when we push some new changes to our repository? We can do that with Webhooks in Jenkins. . In this article, we will look into how we can automatically trigger Jenkins pipelines.
Configuring webhooks is not that hard in Jenkins. Let’s look at the steps we need to follow in order to automatically trigger Jenkins pipelines when we push new changes to our GitHub repository.
Step 1: Install GitHub Plugin in Jenkins
The first thing we need to do is making sure that the necessary plugin for integrating Jenkins with GitHub is installed in our Jenkins server.
From Jenkins dashboard, go to Manage Jenkins -> Manage Plugins and see if GitHub Plugin is available under “Installed” section. This would be enabled by default. But if not, you can install it from “Available” section.
If your project is on a different SCM such as GitLab for instance, you can find and install an appropriate plugin for that.
You can click on the plugin link from above page and find the documentation on how to use the plugin. On this occasion, we can refer to the GitHub hook trigger for GITScm polling section in the documentation. Our next steps in this article are mostly based on that.
Step 2: Configure the Build Triggers in Pipeline
Next step is configuring the build trigger from the pipeline. This is as simple as checking the “GitHub hook trigger for GITScm polling” check box in “Build Triggers” section as shown below.
Once this is done, we need to configure this webhook from GitHub side as well, so that GitHub would know to notify Jenkins whenever some changes are pushed to the repository.
Step 3: Add Webhook to the GitHub Repository
In GitHub, go to the repository and go to Settings -> Webhooks and click on “Add webhook” button to add a webhook.
- Enter the payload URL. In general the URL is of the form
$JENKINS_BASE_URL/github-webhook/
- Select
application/json
content type. - Under “which events would you like to trigger this webhook?”, I have ticked “Just the push event”. If you select “Let me select individual events”, you can select individual events of your choice, on which the webhook will be triggered.
Make sure to check the active check box as well and click on “Add webhook” and that’s it. Now to trigger the pipeline, all we need to do is push something to our GitHub repository.
Step 4: Trigger Pipeline by Pushing Something to the Repository
Once you push to the repository, you would notice the clock icon flickering and a build starting.
You can see the build progress as usual from the pipeline page as shown below.
And in GitHub Hook Log, you can see that the event was triggered by our GitHub hook.
If you try this same method for multibranch pipelines, then you may notice that this webhook method is not available for multibranch pipelines out of the box. So how do we do the same for multibranch pipelines? We would look into that in the next article.