Adding analysis to Bitbucket pipeline
How to add SonarQube Community Build analysis to Bitbucket Pipelines.
Once you have created your project in SonarQube, you can add the SonarQube analysis to your Bitbucket pipeline. To do so, you need to:
- Configure your project analysis parameters. 
- Configure your - bitbucket-pipelines.ymlfile.
Prerequisites
You should clone the full depth to make sure the scanner has access to all of your history when running analysis. For more information, see the documentation about Git clone behavior in Bitbucket.
Configuring your project analysis parameters
For general information about the configuration of project analysis parameters, see Configuration overview.
You can set environment variables securely for all pipelines in Bitbucket Cloud’s settings. See User-defined variables for more information.
You need to set the following environment variables in Bitbucket Cloud for analysis:
- SONAR_TOKEN: Generate a SonarQube token for Bitbucket Cloud and create a custom, secure environment variable in Bitbucket Cloud with- SONAR_TOKENas the Name and the token you generated as the Value.
- SONAR_HOST_URL: Create a custom environment variable with- SONAR_HOST_URLas the Name and your SonarQube URL as the Value.
See Managing your tokens for more information.
Configuring your bitbucket-pipelines.yml file
This section shows you how to configure your bitbucket-pipelines.yml file. Bitbucket Pipelines can build specific branches and pull requests if you use the branches and pull-requests pipelines as shown in the example configurations below.
Failing the pipeline job when the quality gate fails
You can use the SonarQube quality gate check Bitbucket Pipe to ensure your code meets your quality standards by failing your pipeline job when your quality gate fails.
If you do not want to use the SonarQube quality gate Check Pipe, you can instruct the scanner to wait for the SonarQube quality gate status at the end of the analysis. To enable this, pass the -Dsonar.qualitygate.wait=true parameter to the scanner in the bitbucket-pipelines.yml file.
This will make the analysis step poll SonarQube regularly until the quality gate is computed. This will increase your pipeline duration. Note that, if the quality gate is red, this will make the analysis step fail, even if the actual analysis itself is successful. We advise only using this parameter when necessary (for example, to block a deployment pipeline if the quality gate is red). It should not be used to report the quality gate status in a pull request.
You can set the sonar.qualitygate.timeout property to an amount of time (in seconds) that the scanner should wait for a report to be processed. The default is 300 seconds.
Related pages
Setting up Bitbucket Cloud integration at global level Importing Bitbucket Cloud repositories Troubleshooting
Last updated
Was this helpful?

