Hello everyone! In this post, we will see how we can configure Jenkins to automatically trigger a Jenkins build job, whenever a Pull Request is merged on Github. To follow along with this tutorial make sure that you have an initial setup of Jenkins running and a Github repository to test it out. When I started searching on how to do this, I was not able to find many useful resources. Hence, I thought to make a post.
Why you need this - Github Workflow
I like to follow the Github workflow which is followed in most companies. The recommended workflow is to create a separate branch if you are working on a feature/hot-fix and then merge the branch with the master branch at the end. In this post, I have explained how I use the same workflow for creating or editing blog posts for my site.
The below sequence diagram explains the workflow I follow.
Github Webhook - Limitations
Webhooks allows you to set up an automated integration workflow with Github or other OAuth applications. Web-hook subscribes to Github events. So, whenever a particular event occurs(say commit, PR, etc.), Github webhook will send a POST request to the webhook URL with some payload. Payload data will contain all the details about the event. More about Github webhooks here and here.
Example Payload Data
To know about all the types of events available, you can check here.
The event in which we are interested is the “pull request” event, which handles all the events related to pull request. More info here.
|pull-request||Triggered when a pull request is assigned, unassigned, labeled, unlabeled, opened, edited, closed, reopened, synchronize, ready_for_review, locked, unlocked or when a pull request review is requested or removed.|
There are multiple sub-events inside the pull request event like edited, created, merged, etc. The idea here is to configure a normal Github webhook for pull request event. Once the event occurs, Github sends the payload to Jenkins, which is then parsed and necessary actions are taken.
Jenkins Plugin - Generic Webhook Trigger
I found this useful Generic Webhook Trigger plugin in Jenkins which helps in parsing the contents of any HTTP request into JSON/XML. It also allows to save the parsed contents inside variables and send them to Jenkins jobs.
We are going to use this plugin to parse the contents of the Github webhook payload and then trigger Jenkins jobs if the pull request even is a “merged” event. We do nothing/skip if it is any other type of subevent.
Setting Up Jenkins and Github Web-hook
- Installing the plugin
The plugin can be downloaded and installed from the inbuilt plugins marketplace available on the Jenkins. You can search for “Generic Webhook Trigger Plugin” and then install the latest version.
- Set up the plugin
Once the plugin is installed, you will be able to see additional options while configuring the build. Our goal here is to parse the JSON contents and then check for the event type and then trigger job if it is a pull-request event and the status is merged.
The JSON data will look something like this.
The two values which we need to extract from the above JSON data are “action” and then the key “merged” inside “pull_request”. JsonPath syntax is used to extract the values from the JSON data. More info on how to use JsonPath here.
We can also add a secret token for additional security purposes. Only if this secret key is received in the payload, we parse the data. Else, the data can be ignored.
- Setup Github Webhook Now set up the Github webhook from the settings section of your repository.
Make sure to send the secret token which you have set up earlier in the webhook URL(invoke?token=mySecretToken).
Also, set up the trigger event to pull request.
- Configure the pipeline
Now, we need to configure the pipeline/build job to access the variables from the Jenkins script. Only on certain a condition, we need to execute the build.
In the above pipeline definition script, we are checking if the parameter current_status is closed and merged is set to true. Else the Build step will be skipped.
Testing the plugin
Now, if you have followed everything from the previous section, then the build should happen successfully when a new pull-request is opened on Github. To test our plugin, we can’t open a new pull request every time from Github and then merge it. Instead, we can also pass the parameters manually by using build with parameters in Jenkins. This can be done by changing the job as a parameterized job.
After configuring the job as parameterized, then you can manually build the job by passing the parameters to the job.
Now that we have completely configured the plugin and GitHub, we can check the results. I am sharing a sample result for my website pipeline build jobs below.
In the above image, you can see the pipeline builds history. Now first, let’s take a look at build 44. Build 44 got triggered automatically when the pull-request was initially created. But, since it is not a merge event, none of the steps inside the build was executed.
From the console log, we can see the parameters which we have received(current_status is opened and merged is false). So, each of the build steps is skipped in the job.
Now, let us take another pipeline job build 45.
Build 45 got triggered while the pull-request got merged. After the pull-request got merged, GitHub triggers the webhook with the payload. Since we have received current_status as closed and merged as True, all the steps in the build were executed. Console logs confirm the same.
Hope you found this helpful. It might seem complicated to this with the plugin at first. But once everything is configured then there is not less manual intervention in the workflow. I was entirely new to using Jenkins and it was a good learning process for me. If you found a better way to do this/have any suggests or queries or comments, feel free to add your comment.