You may want to run Jenkins build for Pull Request when it's ready to be reviewed / merged, for example:

  • save build-minutes on your CI-server for work-in-progress
  • filter specific premature CI-steps like 'Deploy to staging', 'Create release' etc.

There's an option which allows you to trigger build only when Pull Request is 'mergeable', i.e. when Merge button is available in Bitbucket Pull Request UI.

Do the following to use it:

  1. Go to repository or project settings
  2. Check option Advanced Configuration → Trigger Pull request only if mergeable
  3. Save hook settings

If you have a lot of pull requests in your repo or project, enabling this option can cause significant load on Bitbucket, so it's disabled by default.

When you enable this option, Jenkins will be notified only if pull request is mergeable, i.e. when Pull Request:

  1. has no unresolved conflicts
  2. passes all configured Merge Checks

If one of these conditions is not met, Jenkins build won't be triggered.

Using built-in Merge Checks

Bitbucket provides several useful Merge Checks out-of-box, which can be used with Trigger Pull request only if mergeable option to limit number of Jenkins builds.

Below is a list of such built-in Merge Checks to use:

  • No incomplete tasks: all tasks in Pull Request are resolved
  • All reviewers approve: require number of approvers has approved the Pull Request
  • No 'needs work' status: none of approvers has marked Pull Request as 'Needs work'

Some Bitbucket apps (like YACC) may provide custom Merge Checks - these checks will be also respected when you enable the option.