Overview

To use Webhook To Jenkins to allow your repositories to communicate with your CI/CD setup, you will need to do some configuration on both the Jenkins and Bitbucket sides.

This is the configuration guide for people who use the Bitbucket Branch Source Plugin in Jenkins. If you use the Git Plugin, see here.

Get Webhook to Jenkins from the Atlassian Marketplace!

Configuring Jenkins

Configuring the Bitbucket URL in Jenkins

StepsScreenshot
  1. Install the Bitbucket Branch Source plugin in Jenkins.
  2. Go to Manage Jenkins → Configure System. Scroll down to Bitbucket Endpoints.
  3. Click the Add button and select Bitbucket Server from the dropdown.
  4. Enter the name and URL for your endpoint. If you have a Manage Hooks checkbox, make sure it's unchecked.

Configuring your Jenkins Job


You can use either the Multibranch Pipeline job or the Bitbucket Team job with the plugin. Both configurations are very similar. Here's an example:


StepsScreenshot
  1. On the Job configuration screen, scroll down to the Branch Sources section (for a Bitbucket Project configuration, this section is called Projects).
  2. Click the Add Source button and select Bitbucket.
  3. For server, select the Bitbucket Server endpoint you added before (see Configuring Jenkins above).
  4. Add your administrator Bitbucket credentials if you have not already, and select them.
  5. For owner, put either the Bitbucket project or username which the repository is hosted under.

When updating a job...

If you have changed the Bitbucket URL in the Configure System section of Jenkins since the last time you configured this job, note that sometimes the Server field will have been switched to Bitbucket Cloud. Make certain the Server field is pointed at the right Bitbucket Server setting.


Configuring Bitbucket

Configuring on a per Repository Basis

You can configure this app at the repository or the project level. See Project Configuration.


StepScreenshot and Notes

In Bitbucket, install Webhook to Jenkins for Bitbucket.


Navigate to your repository and hit the Repository Settings tab.  If you wish to configure on a per project basis, see Project Configuration.

In the left-side navigation, click the Hooks link. 

Select Enabled from the dropdown for the Post-Receive hook, Bitbucket Server Webhook to Jenkins. 


Prior to Bitbucket 5.2.0

Versions of Bitbucket prior to 5.2.0 will not have a dropdown, and instead will just have a toggle.

Bitbucket 5.2.0+

If you're using Bitbucket 5.2.0+ and you've already setup a project configuration that you would like to use,
you can select Inherited and stop here.


Enter the URL for your Jenkins instance and the Repository URL that you configured Jenkins to use. You can use the dropdown to get the clone URL for each supported protocol. Afterwards, feel free to change it to match your Jenkins instance. 

Make sure you select the Bitbucket Branch Source Plugin as the Jenkins endpoint.

Click the Trigger Jenkins button to test your configuration. If it worked, it’d look like this: 

With both Jenkins and Bitbucket now configured, if you commit code, a build trigger will automatically occur. What happens is the post-receive hook fires a GET request to Jenkins, which then tells it to poll the repository. The poll checks to see if there are actually any changes. Since there are, it triggers the build!


Configuring on a per-Project Basis

This configuration is very similar to Configuring a repository. You're just setting up a configuration at the Project level.

StepScreenshots and Notes

In Bitbucket, install Webhook to Jenkins for Bitbucket.


Navigate to your project and hit the Project Settings tab. 

In the left-side navigation, click the Hooks link. 

Toggle the Post-Receive hook, Bitbucket Server Webhook to Jenkins to Enabled.
Enter the URL for your Jenkins instance and the Repository URL that you configured Jenkins to use. You can use the dropdown to get the clone URL for each supported protocol. Afterwards, feel free to change it to match your Jenkins instance. Make sure you include the {repository} field so Jenkins knows where to add repository id for the URL.

Click the Trigger Jenkins button to test your configuration. If it worked, it’d look like this: 

With both Jenkins and Bitbucket now configured, if you commit code, a build trigger will automatically occur. What happens is the post-receive hook fires a GET request to Jenkins, which then tells it to poll the repository (why you need to have polling turned on). The poll checks to see if there are actually any changes. Since there are, it triggers the build!


Configure Builds shortcut by adding a Jenkins Server

You can view your Jenkins builds in Bitbucket, if you configure this setting.

See How to Use Bitbucket Builds for setup instructions.