Trigger Jenkins to only build pull requests

When a pull request is created in Bitbucket, Webhook to Jenkins sends pull request references(**/from) as well as the branch name. If the pull request is from a fork, the app only sends the pull request reference. This allows users to set up the plugin to only build from pull requests.

These instructions are for the Jenkins Git Plugin 4.0 and higher. For older versions of the Git Plugin, go here.

The cross-repository pull requests with fork repositories are only supported with the Bitbucket Branch Source endpoint.

Configure Bitbucket Server

To configure Jenkins to only build pull requests::

  1. From your Bitbucket repository, click the Repository Settings icon > Hooks and select Enabled or edit the previously enabled Webhook to Jenkins configuration.

  2. From the Advanced Configuration tab, select Build All from the Branch Options dropdown. This skips the allowlist and blocklist checks so that all new commits and tags send the required Jenkins notification.

The Omit SHA1 Hash Code option must be unchecked in this configuration since it triggers polling mode on the Jenkins side. In this mode, Jenkins processes refs/heads/* only. Click the General Settings tab and confirm the Omit SHA1 Hash Code option is not selected.

Set up a Jenkins Job for Pull Requests

In the job, set the branch specifier to **/from. This configures the job so that it only responds to new pull requests.

Configure Pipeline Job for Pull Requests

When Jenkins to Webhook receives notification it needs to map repository URL and branches to a particular job. Pipeline job implementation uses information from previous builds.

This information includes:

  • repository URL

  • branch specifier

Configuring the pipeline job can be challenging. The build needs to be started from the Jenkins side:

  • when the job is initially configured

  • whenever the job is reconfigured (for example, the repository URL or branch specifier is changed)

When only **/from branch spec is used, then build can't be started properly. Because **/from branches are not fetched. To solve this issue second branch spec can be added (like for example */master ) and when job is successfully invoked then it can be removed.