In the previous article Automatically Trigger Jenkins Pipelines with Webhooks, we configured our Jenkins Pipeline to automatically trigger a build using the GitHub plugin for Jenkins when we pushed some changes into the source code repository. However that plugin cannot be used to trigger Multibranch Pipelines. So in this article, we are going to look into how we can achieve the same thing for Multibranch Pipelines as well.
Step 1: Install Multibranch Scan Webhook Trigger Plugin in Jenkins
The first thing we need to do is installing the “Multibranch Scan Webhook Trigger” plugin in the Jenkins server.
In order to do that, from Jenkins dashboard, go to Manage Jenkins -> Manage Plugins and go to “available” tab. Then search for “Multibranch Scan Webhook Trigger” and install the plugin.
You can click on the plugin link from above page and find the documentation on how to use the plugin. Next steps we follow would be based on that documentation.
Once the plugin is installed we can configure our Multibranch Pipeline.
Step 2: Configure the Scan Multibranch Pipeline Triggers Section in the Multibranch Pipeline
Once the plugin is installed, when you go to configure the Multibranch Pipeline, you would see a new section named “Scan Multibranch Pipeline Triggers”.
Here you can simply check the “Scan by webhook” check box and specify a token of your choice.
This is all we have to do from Jenkins. 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 the 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/multibranch-webhook-trigger/invoke?token=<TOKENHERE>
Here you have to give the trigger token you specified in step 2 as the token. - 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 the Multibranch Pipeline by Pushing Something to the Repository
Once you push some changes to the repository, under “Build Queue”, you would see a new build. This happens pretty quickly after you push the changes, so you would have to be quick!
Since I pushed some changes into the master branch, you can see in below picture that the build has been triggered for the master branch.
If you go inside the Multibranch Pipeline, you should see that a build has been triggered only for the master branch. Notice the timestamp in master branch.