Adding the SonarQube analysis to your GitLab CI/CD pipeline
Once you have created your project in SonarQube, you can add the SonarQube analysis to your GitLab CI/CD pipeline:
- Configure the project analysis parameters.
- Add the analysis to your GitLab CI/CD pipeline.
- Commit and push your code to start the analysis.
You can fail the pipeline when the quality gate fails (see below). If you use a monorepo, see the section If you use a monorepo below. To manage other project-level features, see Setting up GitLab integration features at the project level.
For more information on configuring your build with GitLab CI/CD, see the GitLab CI/CD pipeline configuration reference.
Configuring the project analysis parameters
For general information, see Analysis parameters and the respective SonarScanner section: Maven, Gradle, NET, CLI, and NPM.
With GitLab CI/CD, you can securely set sonar.token
and sonar.host.url
properties through CI/CD variables: see Setting the authentication to the SonarQube Server below.
In addition, starting from the Developer Edition, SonarScanners running in GitLab CI/CD can automatically detect branches and merge requests being built so you don't need to specifically pass them as parameters to the scanner. See Branch analysis and Pull request analysis for more information.
Setting the authentication to the SonarQube Server
You have to create the Sonar token used to authenticate to the SonarQube Server during the analysis of the monorepo project and store it securely in the pipeline environment. You can either use a global-level or (recommended) project-level token.
Proceed as follows:
- Generate the token in SonarQube:
- For a project token, go to the Security page of your SonarQube account and create a token.
- For a global token, ask your system administrator (The procedure is similar but you need the global Administer system permission.).
- Create a custom environment variable in GitLab with:
- Key: SONAR_TOKEN
- Value: the corresponding token value.
- Create a custom environment variable in GitLab with:
- Key: SONAR_HOST_URL
- Value: SonarQube Server URL
Configuring your .gitlab-ci-yml file
This section shows you how to configure your GitLab CI/CD .gitlab-ci.yml
file. The allow_failure
parameter in the examples allows a job to fail without impacting the rest of the CI suite.
You'll set up your build according to your SonarQube edition: You'll set up your build according to your SonarQube edition:
- Community Edition: Community Edition doesn't support multiple branches, so you should only analyze your main branch. You can restrict the analysis to your main branch by using rules to add the branch name in your .yml file.
- Developer Edition and above: By default, GitLab will build all branches but not merge requests. To build merge requests, you need to use rules in your
.gitlab-ci.yml
. See the example configurations below for more information.
Select the scanner you're using below to expand an example configuration:
SonarScanner for Gradle
SonarScanner for Maven
SonarScanner CLI
Project key
A project key has to be provided through sonar-project.properties
or through the command line parameter. For more information, see the SonarScanner CLI documentation.
Self-signed certificates
If you secure your SonarQube instance with a self-signed certificate, you may need to build a custom image based on sonarsource/sonar-scanner-cli
. See the section Advanced docker configuration within the SonarScanner CLI documentation.
SonarScanner for .NET
For C/C++/Objective-C configuration examples, you can refer to the sonarsource-cfamily-examples repository.
The errors “Missing blame information…” and “Could not find ref…” can be caused by checking out with a partial or shallow clone, or when using Git submodules. You should disable git shallow clone to make sure the scanner has access to all of your history when running analysis with GitLab CI/CD.
For more information, see Git shallow clone.
Failing the pipeline when the quality gate fails
In order for the quality gate to fail on the GitLab side when it fails on the SonarQube side, the scanner needs to wait for the SonarQube quality gate status. To enable this, set the sonar.qualitygate.wait=true
parameter in the .gitlab-ci.yml
file. See the configuration examples in Configuring your .gitlab-ci-yml file above.
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.
If you use a monorepo
In a monorepo setup, multiple SonarQube projects, each corresponding to a separate project within the monorepo, are all bound to the same GitLab repository. If the GitLab integration with SonarQube has been properly set up, then you can easily import the projects managed in a GitLab monorepo from the SonarQube UI and thus, benefit from the integration features, such as reporting quality gate status to merge requests.
The monorepo feature is supported starting in the Enterprise Edition.
To add the SonarQube analysis to your GitLab's monorepo CI/CD pipeline:
- If not already done, create the SonarQube projects related to your monorepo: see Managing the projects of a monorepo. To set up integration features at project level, see Setting up GitLab integration features at the project level.
- For each project in the monorepo:
- Set up the authentication to SonarQube Server (
sonar.token
andsonar.host.url
properties): see Setting up the authentication to the SonarQube Server below. - Set the other necessary analysis parameters: see Configuring the project analysis parameters above. The mandatory parameter is:
sonar.projectKey
property.
- Set up the authentication to SonarQube Server (
- Add a CI/CD YAML syntax reference (
.gitlab-ci.yml
) in the home directory of the monorepo: Define a job for each monorepo project in.gitlab-ci.yml
. - You can fail the pipeline when the quality gate fails: see above.
Setting up the authentication to the SonarQube Server
You have to create the Sonar tokens used to authenticate to the SonarQube Server during the analysis of the monorepo projects and store them securely in the pipeline environment. You can either use one single global-level token for the monorepo or use a project-level token for each project in the monorepo. Note that the user account used to generate the token must have the Execute analysis permission.
Proceed as follows:
- Generate the token(s) in SonarQube:
- For project tokens, create a token for each project (you need the Administer permission on the project): Go to the Security page of your SonarQube account and create a Project analysis token.
- For a global token, ask your administrator (The procedure is similar but you need the global Administer system permission.).
- Create a custom environment variable in GitLab and set the Key as follows:
- If you use a global token: enter SONAR_TOKEN.
- Otherwise: enter SONAR_TOKEN_1 (or another unique identifier within the monorepo) for the token of your first project in the monorepo
- In the Value field, enter the corresponding token value.
- If you use project-level tokens, repeat steps 2 to 3 for each additional project in the monorepo.
- Create a custom environment variable in GitLab with:
- Key: SONAR_HOST_URL
- Value: SonarQube Server URL
Was this page helpful?