How to Integrate Sonarqube Quality Gate with Gitlab CI/CD for Maven Project

Integrating SonarQube into GitLab CI

In this post, I will guide you on How to Integrate Sonarqube Quality Gate with Gitlab CI/CD for Maven Projects

Here are some basics before we start

What is a Gitlab?

Gitlab is a Git repository manager with a great CI/CD integration, without managing many plugins like Jenkins. And the fact that we are already close to the source code makes the process easier. We can visualize code and the build process a the same place.

Table of Content :


What are the Features of Gitlab

Gitlab offers many features as below:

  • Built-in CI/CD
  • Publish static websites for free with GitLab Pages
  • Push Rules
  • Container Scanning
  • Multiple approvers in code review
  • Efficient Merge Request reviews
  • Code Quality Reports
  • Remote repository pull mirroring
  • Protected Environments
  • Merge Trains
  • Create test cases from within GitLab

There are two critical files that we need to pay attention to and focus on to build our Java application using Maven on Gitlab. Let’s see each of them:

 1)  .gitlab-ci.yml
 2). settings.xml


YML file

GitLab CI uses a YAML file (.gitlab-ci.yml) for project configuration. This file is placed at the root of the repository and defines the project's Pipelines, Jobs, and Environments.:

The YAML file defines a set of jobs with constraints for when they should be run

Let's analyze the structure and some basic terms used in the YML step by step:

The YML file starts by specifying a custom Docker image to run jobs in. Docker allows you to run jobs in independent "containers" within a single Linux instance. Docker containers allow us to make utility applications (such as curl and Node) available to our jobs without leaking any project-specific data between projects.:

What are a Stages in Gitlab Pipeline?

Stages are steps that can be used by multiple different jobs. Specifying stages allows us to create flexible, multi-stage pipelines.

The order of elements in stages defines the order of job execution.

Jobs of the same stage are run in parallel.

Jobs of the next stage are run after the jobs from the previous stage are completed successfully.

Variables in GitLab

GitLab CI allows you to set your own variables in .gitlab-ci.yml. 

These variables are available in the job environment when it executes. These variables are stored in the Git repository and are meant to store non-sensitive project configurations. 

These variables can be used later in all executed commands and scripts. Credentials and other secrets should be stored as Secret Variables instead.:

To use GitLab CI/CD, you need:

Application code hosted in a Git repository.

A file called .gitlab-ci.yml in the root of your repository, which contains the CI/CD configuration. In the .gitlab-ci.yml file, you can define:

  • The scripts you want to run.
  • Other configuration files and templates you want to include.
  • Dependencies and caches.
  • The commands you want to run in sequence and those you want to run in parallel.
  • The location to deploy your application.
  • Whether you want to run the scripts automatically or trigger any of them manually.

The scripts are grouped into jobs, and jobs run as part of a larger pipeline. You can group multiple independent jobs into stages that run in a defined order. The CI/CD configuration needs at least one job that is not hidden.

You should organize your jobs in a sequence that suits your application and is in accordance with the tests you wish to perform. To visualize the process, imagine the scripts you add to jobs are the same as CLI commands you run on your computer.

When you add a .gitlab-ci.yml file to your repository, GitLab detects it and an application called GitLab Runner runs the scripts defined in the jobs.

.gitlab-ci.yml sonarqube example

Here is the file that we needed to use in our project:

variables: SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" stages: - sonar-check - docker image build before_script: - chmod +x mvnw sonar-check: stage: sonar-check script: - mvn --batch-mode clean verify install sonar:sonar -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_USERNAME -Dsonar.password=$SONAR_PASSWORD tags: - shell_executor artifacts: expire_in: '1 day' paths: - target/ docker-build: stage: docker image build script: - docker build -t gitlab-maven-sonarqube-cicd . - docker run -dp 8080:8080 gitlab-maven-sonarqube-cicd tags: - shell_executor only: refs: - development

In this YAML file $SONAR_URL,$SONAR_USERNAME, and $SONAR_PASSWORD are the variables we need to set in the variables section in the GitLab repository.



In this post, We have seen How to Integrate Sonarqube Quality Gate with Gitlab CI/CD for Maven Project.