Bitbucket Server integration
SonarQube's integration with Bitbucket Server allows you to maintain code quality and security in your Bitbucket Server repositories.
With this integration, you'll be able to:
- Import your BitBucket Server repositories: Import your Bitbucket Server repositories into SonarQube to easily set up SonarQube projects.
- Report your Quality Gate status to your pull requests (starting in Developer Edition): See your Quality Gate and code metric results right in Bitbucket Server so you know if it's safe to merge your changes.
- Integration with Bitbucket Server requires at least Bitbucket Server version 5.15.
- You've set a SonarQube Server base URL in Administration > Configuration > General Settings > General.
Community Edition doesn't support the analysis of multiple branches, so you can only analyze your main branch. Starting in Developer Edition, you can analyze multiple branches and pull requests.
Setting up the import of BitBucket Server repositories into SonarQube allows you to easily create SonarQube projects from your Bitbucket Server repositories. If you're using Developer Edition or above, this is also the first step in adding pull request decoration.
To import your Bitbucket repositories into SonarQube, you can only have one global configuration of Bitbucket, including Bitbucket Server and Bitbucket Cloud. See the Configuring multiple DevOps platform instances section below for more information.
To set up the import of BitBucket Server repositories:
- Set your global DevOps Platform integration settings
- Add a personal access token for importing repositories
To set your global DevOps Platform Integration settings, navigate to Administration > Configuration > General Settings > DevOps Platform Integrations, select the Bitbucket tab, and select Bitbucket Server as the variant you want to configure. From here, specify the following settings:
- Configuration Name (Enterprise and Data Center Edition only): The name used to identify your Bitbucket Server configuration at the project level. Use something succinct and easily recognizable.
- Bitbucket Server URL: your instances URL. For example,
- Personal Access Token: A Bitbucket Server user account is used to decorate Pull Requests. We recommend using a dedicated Bitbucket Server account with Administrator permissions. You need a Personal Access Token from this account with Read permission for the repositories that will be analyzed. Administrators can encrypt this token at Administration > Configuration > Encryption. See the Settings Encryption section of the Security page for more information. This personal access token is used to report your Quality Gate status to your pull requests. You'll be asked for another personal access token for importing projects in the following section.
After setting your global settings, you can add a project from Bitbucket Server by clicking the Add project button in the upper-right corner of the Projects homepage and selecting Bitbucket.
Then, you'll be asked to provide a personal access token from your user account with
Read permissions for both projects and repositories. This token will be stored in SonarQube and can be revoked at any time in Bitbucket Server.
After saving your personal access token, you'll see a list of your Bitbucket Server projects that you can set up and add to SonarQube. Setting up your projects this way also sets your project settings for pull request decoration.
After you've set up SonarQube to import your Bitbucket Server repositories as shown in the previous section, SonarQube can report your quality gate status and analysis metrics directly to your Bitbucket Server pull requests.
To do this, add a project from Bitbucket by selecting the Add project button in the upper-right corner of the Projects homepage and select Bitbucket from the drop-down menu.
Then, follow the steps in SonarQube to analyze your project. SonarQube automatically sets the project settings required to show your quality gate in your pull requests.
To report your quality gate status in your pull requests, a SonarQube analysis needs to be run on your code. You can find the additional parameters required for pull request analysis on the Pull request analysis page.
If you're creating your projects manually or adding quality gate reporting to an existing project, see the following section.
SonarQube can also report your quality gate status to Bitbucket Server for existing projects and manually-created projects. After you've updated your global settings as shown in the Importing your Bitbucket Server repositories into SonarQube section above, set the following project settings at Project Settings > General Settings > DevOps Platform Integration:
- Configuration name: The configuration name that corresponds to your DevOps Platform instance.
- Project Key: the project key is part of your BitBucket Server repository URL
- Repository SLUG: The repository slug is part of your BitBucket Server repository URL
Reporting your quality gate status on pull requests in a mono repository
Reporting quality gate statuses to pull requests in a mono repository setup is supported starting in Enterprise Edition.
In a mono repository setup, multiple SonarQube projects, each corresponding to a separate project within the mono repository, are all bound to the same Bitbucket Server repository. You'll need to set up each SonarQube project that's part of a mono repository to report your quality gate status.
You need to set up projects that are part of a mono repository manually as shown in the Reporting your quality gate status in manually created or existing project section above. You also need to set the Enable mono repository support setting to true at Project Settings > General Settings > DevOps Platform Integration.
After setting your project settings, ensure the correct project is being analyzed by adjusting the analysis scope and pass your project names to the scanner. See the following sections for more information.
You need to adjust the analysis scope to make sure SonarQube doesn't analyze code from other projects in your mono repository. To do this, set up a Source file inclusion for your project at Project Settings > Analysis Scope with a pattern that will only include files from the appropriate folder. For example, adding
./MyFolderName/**/* to your inclusions would only include code in the
MyFolderName folder for analysis. See the page Analysis scope for more information on narrowing the scope of your analysis.
Because of the nature of a mono repository, SonarQube scanners might read all project names of your mono repository as identical. To avoid having multiple projects with the same name, you need to pass the
sonar.projectName parameter to the scanner. For example, if you're using the Maven scanner, you would pass
mvn sonar:sonar -Dsonar.projectName=YourProjectName.
Configuring multiple DevOps platform instances
SonarQube can report your quality gate status to multiple DevOps platform instances. To do this, you need to create a configuration for each DevOps platform instance and assign that configuration to the appropriate projects.
When adding a quality gate status to your pull requests, individual issues will be linked to their SonarQube counterparts automatically. For this to work correctly, you need to set the instance's Server base URL (Administration > Configuration > General Settings > General > General) correctly. Otherwise, the links will default to
After setting up pull request analysis, you can block pull requests from being merged if it is failing the quality gate. To do this:
- In Bitbucket Server, navigate to Repository settings > Code Insights.
- Add a Required report called
- Select Must pass as the Required status.
- Select Must not have any annotations as the Annotation requirements.
Preventing pull request merges when the quality gate fails is not supported for mono repositories.
© 2008-2023, SonarSource S.A, Switzerland. Except where otherwise noted, content in this space is licensed under a Creative Commons Attribution-NonCommercial 3.0 United States License. SONARQUBE is a trademark of SonarSource SA. All other trademarks and copyrights are the property of their respective owners.