You can run Jenkins build for a Pull Request when it is ready to be reviewed or merged. For example:
To save build-minutes on your CI-server for work-in-progress
To filter specific premature CI-steps like 'Deploy to staging', 'Create release' etc.
A Webhook to Jenkins option allows you to trigger build only when a Pull Request is mergeable. For example, when the Merge button is available in the Bitbucket Pull Request UI.
To enable this option:
Go to the repository or project settings.
Click the Advanced Configuration tab and select the Trigger Pull request only if mergeable option.
If you have many pull requests in your repo or project, enabling this option can cause a significant load on Bitbucket. For this reason, the feature is disabled by default.
With this option enabled, Jenkins is notified only if the pull request is mergeable. For example, when the Pull Request:
has no unresolved conflicts
passes all configured Merge Checks
If these conditions are not met, the Jenkins build will not trigger.
Using built-in Merge Checks
Bitbucket provides several useful Merge Checks out-of-box, which can be used with the Trigger Pull request only if mergeable option to limit the 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: requires the reviewers to approve the Pull Request
No 'needs work' status: none of the approvers has marked Pull Request as 'Needs work.'
Some Bitbucket apps, such as YACC, may provide custom Merge Checks - these checks will be respected when you enable the option.