Skip to main content

Create a quality gate with Bitbucket

Written by David Martin

What is a Quality Gate?

A quality gate is the best way to enforce a quality policy in your organization. Setting up a quality gate allows you to prevent any code that doesn't pass code reviews from being merged into certain branches.

Key Benefits

  • Enforce code quality consistency across the entire project.

  • Ensure that newly committed code doesn't introduce problems.

  • Ensure issues are addressed before being merged.

Before to start

Before proceeding with creating a quality gate in your VCS for your repo, you must enable the Code Reviews Automation for your project.

Enable Automation

To automatically monitor your code with Code Reviews, you need to turn on and select a protection mode. Code Reviews automatically creates webhooks to monitor relevant events in your repository. You can select your protection mode from the homepage by navigating to Settings and then Protection. From there, you can view the current settings and adjust the protection mode as needed.


Connection User

Bitbucket mandates that Pull Requests (PRs) receive approvals from a user distinct from the original author. To ensure the quality gate operates fully with Code Reviews, it is crucial to set up a dedicated connection user for Code Reviews within your project.

Setting up brach protection

  1. In Bitbucket, hover over your project and click the ... menu that appears.

  2. Select Settings.

  3. Under Workflow, select Branch restrictions.

  4. Click Add a branch restriction.


Suggested Setup

Once you've added a branch restriction, select the Merge settings tab to configure your merge checks and conditions.

Under Merge checks, enable the following:

  • Minimum number of approvals: Set this to the total number of reviewers needed before a merge. For example, if you need two developers and Code reviews t to approve, set this to three.

  • Minimum number of approvals from default reviewers: Use this to require sign-off from specific people, such as a team lead or architect.

  • Minimum number of successful builds for the last commit with no failed builds and no in progress builds: Set this to match the number of active Code Reviews policies in your project. For example, if you have six active policies, set this to six.

Under Merge conditions, enable both:

  • Reset requested changes when the source branch is modified: Makes sure any new commits to the source branch trigger a fresh review, so stale approvals can't carry over.

  • Prevent a merge with unresolved merge checks: Blocks merges until all checks pass, so no code can bypass your quality gate.

Click Save when done.

Bitbucket documentation

For more detail on configuring branch restrictions, see Bitbucket's own guides:



Did this answer your question?