SonarQube Code Quality Check Tool Configuration

Author: pseudoyu | 489 words, 3 minutes | comments | 2021-10-27 | Category: Tools

code check, devops

Translations: ZH, DE

Preface

Recently, I’ve been responsible for managing code repositories and conducting code reviews for some of the company’s projects. I’ve been using SonarQube, a code quality check tool that, when integrated with GitLab CI, can automatically perform code quality checks and output inspection reports for each merge request or commit.

This article documents the complete configuration process for importing projects through GitLab repositories, serving as a reference for configuring other projects.

SonarQube Project Configuration

Project Dashboard

sonarqube_homepage

The SonarQube project dashboard is shown in the image above, which analyzes project code quality using a rating system. After each code analysis, it provides a multi-dimensional analysis of the code in a visually intuitive manner. Before merging branches, submitters can refer to the analysis results to modify and improve their code, reducing unnecessary workload for code reviewers.

sonarqube_code_detail

Clicking on specific metrics allows for a deeper dive into code files, identifying detected issues and providing an effective reference for manual code review.

Project Setup

how_to_analyze

Click “Add Project” in the upper right corner to choose from different analysis methods. It supports common code repository automation workflows such as Jenkins, GitLab CI, and GitHub Actions. This article will primarily explain the configuration method for GitLab CI.

import_gitlab_project

After selecting GitLab CI, choose the project repository from your associated GitLab account to proceed with further configuration.

project_code

Taking a Go project as an example, first, we need to manually create a sonar-project.properties file and paste the configuration information as instructed.

create_token.png

config_cicd_var

Then, we need to create a Token for the project and fill in the Token and URL variable values in GitLab under “Settings” - “CI/CD” - “Variables” configuration options.

CI Configuration

After the basic project configuration, we need to configure the GitLab CI workflow through .gitlab-ci.yml. My configuration is shown in the image below:

config_gitlan_ci

I have primarily set it up so that when a merge request is made to the repository, if there are changes in the src directory, the testing pipeline is executed, performing a code quality check through SonarQube.

GitLab CI can also include deployment scripts, used in conjunction with the SonarQube tool to optimize workflows. The project’s CI script needs to add corresponding Runners to execute.

sonar_check_begin

When a merge request is detected, the sonarqube-check will be triggered and executed, ultimately returning the execution results.

sonar_check_success

sonarqube_status

At this point, opening the project page in SonarQube will show the analysis information, completing this code quality check.

Conclusion

The above describes the complete process of configuring the SonarQube code quality check tool for an existing Go project in a GitLab repository. Automated code quality checks are an important part of standardized development and operations processes, especially in team projects. Good standards help optimize workflows and improve overall project quality.

In the future, I will continue to document the configuration and use of open-source tools for development and operations standards used in work. If there are any errors or omissions, please feel free to communicate and correct.

References

  1. SonarQube Document

Related Posts

pseudoyu

Author

pseudoyu

Backend & Smart Contract Developer, MSc Graduate in ECIC(Electronic Commerce and Internet Computing) @ The University of Hong Kong (HKU). Love to learn and build things. Follow me on GitHub


Comments