DEV Community

Rajit Paul
Rajit Paul

Posted on

Bitbucket Branch Based Generic Webhook Trigger on Jenkins

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.

What you'll need

  • Jenkins
  • BitBucket

Configuring BitBucket:

  1. Go to the Repository Settings of BitBucket and then to Webhooks.
  2. Add new webhook (Title, URL- http://JenkinsUsername:JenkinsPassword@Jenkins PublicIPorPublicDomain:8080/generic-webhook-trigger/invoke, Status is Active, Triggers on Repository Push).
  3. Image description
  4. Save it.
  5. Once saved go to view requests, and enable request history collection.

Update code in the specific branch of BitBucket:

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
  • 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
  • Once the body is expanded, copy the entire body from top to bottom. Image description
  • Go to and paste the JSON data you copied in your clipboard.
  • Run this Json Path Expression - push.changes[0].new.links.commits.href
  • 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.

Configuring Jenkins:

  • First we shall install the Generic WebHook Trigger Plugin for Jenkins. Go to Manage Jenkins - Plugin Manager - and Install this plugin Image description
  • Go to the Configuration of the Jenkins Job you wish to trigger using BitBucket. Image description
  • In Build Triggers you should see Generic Webhook Trigger Image description
  • 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
  • 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, from this you shall get the repository and branch.
Image description

  • Considering you have your build setup, you can save the job.

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

If you wish to connect or face some issues while performing this demo, you can reach out @
See ya!

Top comments (3)

redoxate profile image

Thank you ! saved me big time

rajitpaul profile image
Rajit Paul

Glad it helped :)

willredington profile image

this is gold thank you