Bitbucket Branch Based Generic Webhook Trigger on Jenkins
Tags: jenkins, devops, bitbucket
Published: 2022-03-02 13:47:56

Namaskaram!
I faced a recent challenge when I used BitBucket Webhook to trigger a Jenkins build, the challenge was webhooks on bitbucket are based on the entire repository, which means a push to any of the BitBucket Branch will trigger a Jenkins build.
What was I actually looking for was a branch based webhook to trigger Jenkins Build.

<h3>What you'll need</h3>
- Jenkins
- BitBucket

<h3>Configuring BitBucket:</h3>

1. Go to the Repository Settings of BitBucket and then to Webhooks.
2. Add new webhook (Title, URL- <b><i>http://JenkinsUsername:JenkinsPassword@Jenkins PublicIPorPublicDomain:8080/generic-webhook-trigger/invoke</i></b>, Status is Active, Triggers on Repository Push).
3. ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wpmp3r6111kddsr0iqbm.png)
4. Save it.
5. Once saved go to view requests, and enable request history collection. 

<h3>Update code in the specific branch of BitBucket:</h3>

In this part you need to make some changes in the branch of BitBucket that you want to trigger the webhook from.
- Once the code is updated it will trigger a webhook request.
- Go to repository settings then to webhook, and then to view requests you should see something like this. (The status code may be different 404 is ok, but 301 means you are facing authentication issue)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4uyy81cealn9q9g2hwq.png)
- Click on view details, and scroll down to the bottom, you can see the Request - headers and body, we need to expand the body.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scasar77c1m5a78vj64g.png)
- Once the body is expanded, copy the entire body from top to bottom.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9etdauifb9cv3hfs3xb.png)
- Go to https://jsonpath.curiousconcept.com/ and paste the JSON data you copied in your clipboard.
- Run this Json Path Expression - <b>push.changes[0].new.links.commits.href</b>
- In the result you should get the commit link with the repo and it's branch we are pushing to, keep this link saved.
Our job here is done, now we move to Jenkins.

<h3>Configuring Jenkins:</h3>

- First we shall install the Generic WebHook Trigger Plugin for Jenkins.
Go to Manage Jenkins - Plugin Manager - and Install this plugin
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/moaxfxvg077fpxwmwyrn.png)
- Go to the Configuration of the Jenkins Job you wish to trigger using BitBucket.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qkbkodgl30t07yowgsn.png)
- In Build Triggers you should see Generic Webhook Trigger
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/suuoj9bnyi9r2zg99ee5.png)
- Click on add Post content Parameters - Add a variable named branch, in the expression put the JSON Path Expression we used earlier ( push.changes[0].new.links.commits.href ) and select JSONPath.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlknkxx7os4qu3nejdwz.png)
- Scroll down and in Optional Filter, provide the RegEx 

[.+?(?=repository/commits/branch)] - replace the repository and branch with your repo and branch name.

You can refer the JSON Path Result that you saved earlier
 https://api.bitbucket.org/2.0/repositories/project/<b>repository</b>/commits/<b>branch</b>, from this you shall get the repository and branch.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5y9y97k3a74km80xe41k.png)
 
- Considering you have your build setup, you can save the job.

</h4>Make some changes in the code in the specific branch and see your Jenkins Job being triggered!</h4>

If you wish to connect or face some issues while performing this demo, you can reach out @ https://www.linkedin.com/in/rajitpaul/
See ya!