Jenkins — How to trigger build if only a push is made to a specific branch on Bitbucket

Cloudbooklet
4 min readFeb 11, 2020

Jenkins — How to build a specific branch on Bitbucket. In this guide you are going to learn how to configure Jenkins to trigger build automatically once a change is made to a specific branch in Bitbucket.

Here is a tutorial to configure Jenkins to build if only push is made to a specific branch on GitHub.

By default if you configure CI/CD using Jenkins to build automatically once a commit is made, it triggers the build for commits made to any branch in a repository. So using this method you can control the build if only a push is made to a specific branch in your repository.

Prerequisites

Make sure you have the above mentioned prerequisites are completed.

Step 1: Install Generic Webhooks Trigger Plugin

Login to your Jenkins installation setup and navigate to Manage Jenkins >> Manage Plugins and click on the Available tab and install Generic Webhook Trigger Plugin.

Restart Jenkins to use the plugin.

Step 2: Setup Bitbucket App password

Login to your Bitbucket account and go to your profile Settings >> App passwords.

Click Create app password.

Enter a name in the Label field.

Provide read and write permissions to the Repositories.

Click Create.

Now a new password will be visible only this time.

Copy the new password.

Step 3: Setup Webhooks in Bitbucket

Login to your Bitbucket account and go to the repository that is configured in Jenkins. Click on Settings >> Webhooks.

In the Title enter the name to identify.

In the URL enter the the URL in the following format.

Replace JENKINS_URL with your own URL.

Replace YOUR_PASSWORD with the personal access token you created in the previous step.

https://JENKINS_URL/generic-webhook-trigger/invoke?token=YOUR_PASSWORD

In the Triggers choose Repository push.

Click Save.

Now click on View Requests and click Enable History.

Step 4: Configure Jenkins to build only specific branch

Once you have your Webhook created you can configure Jenkins to trigger build only if a push is made to the specific repository.

This configuration is for Freestyle Project in Jenkins.

Login to your Jenkins installation and go you your job and click configure.

In the Source Code Management section under Build Triggers check the Generic Webhook Trigger.

Click Add next to the Post content parameters.

Once a push is made, Bitbucket passes the branch name in the JSON format with the ref key.

The example payload will look something as shown below.

...
"new": {
"name": "branch_name",
"links": {
...

In the Variable enter the name of the variable as branch.

In the Expression enter $.push.changes[0].new.name to match the key and choose the JSONPath format of payload.

Again click Add to add new post content parameter to check the repository. This is because if you have same branch name in two different repositories the build will get triggered on both jobs in Jenkins which are configured with the same branch name. So to fix this here you can add an additional parameter to check if the repository is also matches the correct one.

...
"repository": {
"scm": "git",
"website": "",
"name": "repository_name",
"links": {
...

In the Variable enter the name of the variable as repository.

In the Expression enter $.repository.name to match the key and choose the JSONPath format of payload.

In the Token field enter the app password you created before.

Next we can configure Optional Filter.

In the Expression field enter the branch name to match the key ref as shown below.

branch_name repository_name

In the Text field enter the variable name you assigned before as $branch $repository.

Save the configuration.

Now you have configured your job to trigger the build only a push is made to a specific branch.

Conclusion

Now you have learned how to configure Jenkins to trigger build automatically if a push is made to the specific branch to Bitbucket.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

This post is originally posted on Cloudbooklet.

--

--

Cloudbooklet

Discover the power of AI, data science, and cloud technology with our insightful blog. Stay ahead of the curve with the latest trends and strategies.