SonarQube

Last modified by Boris Folgmann on 2025/02/25 14:44

Introduction

Since the management of projects, users and roles in SonarQube is supported. SonarQube is offered as an additional optional tool with full integration into DevOps-as-a-Service. The latest version of SonarQube s offered.

Information

Please note that SonarQube projects are different to projects in the DevOps portal and the existing tools. In Bitbucket a project can have multiple git repositories which all have their own build pipelines in Jenkins. In contrast SonarQube projects contain the scan results of just one git repository.

To use SonarQube to perform source code quality and security analyses controlled by Jenkins, two components are required:

  1. SonarQube server to perform analysis, evaluation, data storage, generate reports, define quality gates etc. To order it create a Service Request at the Servicedesk DevOps-as-a-Service. To activate SonarQube it's required to save every project in the DevOps portal first. After this step SonarQube can be added to individual users just by editing them.
  2. Sonar Scanner in Jenkins to perform the actual scanning of source code. The scanner transfers the intermediate results to the SonarQube server for analysis and evaluation. Since v2.6 the Jenkins Shared Library offers the automated creation of new projects on SonarQube, running the analysis on Jenkins and uploading the intermediate result for further processing to the SonarQube server.

Integration into Delivery Pipelines

Using Jenkins Shared Library

Since v3.0.0 of the Jenkins Shared Library, not only scanning maven projects, but also scanning all other types of projects is supported. Just use a Jenkinsfile as usual like e.g.:

Standard invocation
 
@Library('sdcloud') _
 
sdcPipeline()

The library will automatically detect if SonarQube was configured on the Jenkins controller or not. If SonarQube is available and the branch you are building is the default branch (usually master) the first analysis stage will be Sonar Scan. Additionally, at the end, the pipeline will wait for Sonar Results. If the build does not pass the Quality Gate, the build will be marked as unstable.

image2022-12-6_18-17-14.png

It's possible to explicitly disable SonarQube for all branches by using a Jenkinsfile like:

Disable SonarQube scanning
 
@Library('sdcloud') _
 
sdcPipeline(sonarQube: false)

If this is the case, Sonar Scan is skipped and Sonar Results not called at all. The same happens if no SonarQube was configured in the Jenkins controller.

image2022-12-6_18-12-36.png

The sonarQube switch of sdcPipeline is true by default for the default branch. If you want to scan another branch, e.g. 'main' then a Jenkinsfile like the following one will do the job.

Define an alternative default branch
@Library('sdcloud') _
 
sdcPipeline(defaultBranch: 'main')

Please note that SonarQube in the Community Edition supports only one branch per project. And this branch will always be called 'master' in the SonarQube UI even if the real name is a different one.

image2022-12-6_18-27-41.png

If you have the SonarQube Developer or Enterprise Edition, you can enable scanning all branches like this:

Scan all branches
@Library('sdcloud') _
 
sdcPipeline(sonarQube: true)

Jenkins Plugin

DevOps-as-a-Service includes the SonarQube plugin https://plugins.jenkins.io/sonar/ in its Jenkins controller. Documentation is available at https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-jenkins/

The SonarQube plugin provides these added values:

  • Injection of globally defined parameters (URL, project name, token, options...) into the build. This is used and handled by the Jenkins Shared Library.
  • Furthermore, decorations for the build, as shown below in the table.

Screenshot from Jenkins

Description

image2021-9-27_16-59-21.png

Integration of a SonarQube link at the repo/branch Jenkins page 

image2021-9-27_12-33-33.png

Integration of a SonarQube link next to the build in the build list. The link forwards to the corresponding project/event in SonarQube server. 

image2021-9-27_12-38-17.png

Jenkins can wait for analysis at SonarQube server and display the result of a quality gate in the pipeline view. The sonar result will be added to the repo/branch page below the Stage View. No further results or details will be available in the Jenkins UI. The SonarQube UI must be used to check results and define the quality gates.

Using SonarQube

Browsing Projects

The portal offers a link to SonarQube for each project on the Homepage in the Source Code Quality tile

1732883272804-687.png

and on the Projects page in the Tools column

image2022-12-7_17-15-52.png

Click on SonarQube. If you have no existing session on your SonarQube instance, you will see the login page. Click the "Log in with keycloak" button to use the single-sign-on provided by the DevOps portal. That means if you're currently logged in to the portal, you will not need to specify your credentials again.

image2022-12-7_17-16-52.png

Now you are logged in to SonarQube. The search box was prefilled with the project name prefix. In the example it's DEVOPSAAS/. Therefore, you will see all SonarQube projects with results that originate from scans of git repositories.

image2022-12-7_17-23-45.png

Instead of using the SonarQube links in the DevOps Portal you can also clear the search box and see all SonarQube projects in which you have at least a VIEWER role.

Project Roles

When a project is opened, different pages are available to work with it.

image2022-12-7_18-7-16.png

It depends on you role in the project which permissions you have.

Project Role

Browse

See Source Code

Administer Issues

Administer Security Hotspots

Administer

Execute Analysis

Admin(tick)(tick)(tick)(tick)(error)(tick)
Master(tick)(tick)(tick)(tick)(error)(tick)
Developer(tick)(tick)(tick)(error)(error)(error)
Viewer(tick)(tick)(error)(error)(error)(error)

Please note that "Execute Analysis" is usually performed in Jenkins. For more information, see above.

Due to security considerations, no user is allowed to directly administer a project in SonarQube. For more information, see below.

Managing Quality Profiles and Gates

There are two sections in SonarQube which are accessibly read-only for all logged-in users:

  1. Quality Profiles for each programming language.
    image2022-12-7_17-31-20.png
  2. Quality gates, which decide if a build is ok or failed.
    image2022-12-7_17-32-8.png

If the default settings do not fit for your instance, create a Service Request at the Servicedesk DevOps-as-a-Service providing a list of users that should be part of the sonar-quality-admins group.

The members of this group have read-write access to Quality Profiles and Quality Gates. It should be clear that you will need an internal governance so that sonar-quality-admins coordinate their work and communicate with the project admins.

To find out which Quality Profiles and Gate are used by a project, open the project and click on Project Information on the right-hand side.

image2022-12-7_17-39-50.png

Members of sonar-quality-admins can associate projects with other alternative quality gates that they have created themselves. The screenshot shows an alternative non-default quality gate. As you can see, the search box at the bottom can be used to find specific projects. Then simply use the checkmark to associate the project with the quality gate.

image2022-12-13_15-21-21.png

Plugins

By default, your SonarQube instance will have these plugins installed:

  • Checkstyle
  • Dependency-Check
  • Findbugs (in fact, it's for SpotBugs)
  • Groovy
  • PMD
  • SoftVis3D Sonar plugin

Commercial licences

To scan more than one branch per project and to get scan results shown in Pull-Requests in Bitbucket, the Developer Edition of SonarQube is required. The Enterprise Edition offers even more features, like e.g. additional programming languages or parallel processing of analysis reports. For more details, see https://www.sonarsource.com/plans-and-pricing/

Customers inside Deutsche Telekom can order commercial licenses at the Servicedesk DevOps-as-a-Service. External customers can bring their own self-managed SonarQube license.