DevOps & Cloud Solutions

Acquia-Gitlab-Sonarqube Integration

SonarQube is a web-based open source platform used to measure and analyse the source code quality. Code quality analysis makes the code more reliable and more readable.

SonarQube is written in java but it can analyse and manage code of more than 20 programming languages, including c/c++, PL/SQL, Cobol, PHP etc. through plugins. Plugins extend the functionality of SonarQube. More than 50 plugins are available.

Acquia Cloud is a cloud-based hosting platform tuned for Drupal performance and scalability. Acquia manages the servers and provides an easy-to-use workflow for developing, staging, and publishing of Drupal applications.

Sonar covers the 7 sections of code quality

  1. Architecture and Design
  2. Unit tests
  3. Duplicated code
  4. Potential bugs
  5. Complex code
  6. Coding standards
  7. Comments

 

Configuration and setup Project on Sonar-Qube console

  1. Open the project specific sonar-qube console

Ex: sonar.<project-Name>.com

  1. Go to your account on top right corner.
  2. Click on My Account
  3. On account page click on security.
  4. Give some token name in “Generate New Token” then click on Generate.

Ex: sonar-token

  1. Sonar token will generate and display on the page.
  2. Take the sonar token and provide under sonar-project.properties for sonar.login
  3. Go to Administration on sonarqube home page
  4. Click on projects, under projects click on Management option
  5. Click on “Create Project”
  6. Provide the project name and key name.
  7. Set Visibility as Public.
  8. Click on Create.

Note: User need Administration permission to create project and Administration option on top navigation.

Installation of Sonar Scanner:

Install sonar scanner to run the sonarqube test runs on Drupal site.

  1. Download the latest SonarScanner from sonar source site

Ex: https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.3.0.1492-linux.zip

  1. Expand the downloaded file in to directory to post-code-deploy of the environment

Ex: /var/www/html/<environment name>/hooks/dev/post-code-deploy

  1. Navigate to config folder of Sonar-scanner

Ex:  /var/www/html/<environment>/hooks/dev/post-code-deploy/sonar-scanner-3.x.x.xxx-linux/conf

  1. Open the sonar-scanner.properties in edit mode and update the below entry to project sonarqube console URL

#—– Default Sonarqube server

sonar.host.url=<Enter project Sonarqube URL>

  1. Navigate environment root directory

Ex: /var/www/html/<environment name>

  1. Create a configuration file in the root directory of the project: sonar-project.properties
  2. Open sonar-project.properties in edit mode

Update the below mentioned details

sonar.projectKey=<Enter project key>

Note: This project key needs to be created in Sonarqube console while configuring the project.

sonar.projectName=<Enter Project Name>

Note: The Project Name created on sonarqube console

sonar.projectVersion=<enter the version of the project>

ex: 1.0

sonar.sources=< .>

ex: sonar.sources=.

sonar.login=<Enter Sonar Token>

Note: Sonar Token will be available in Sonarqube console

sonar.exclusions=<Enter the code base to be exclude to run code quality check>

ex: docroot/themes/custom/<project name>/js/**/*, docroot/themes/custom/<project name/css/**/*

sonar.inclusions=< Enter the code base paths to run code quality check>

ex: docroot/modules/custom/**/*, docroot/themes/custom/**/*

Create the script to run the sonar code script

  1. Go to post-code-deploy under hooks/dev folder
  2. Create sonarcode.sh
  3. Add the command to run when shell script executes

 

#!/bin/sh

#

# This sample Cloud Hook script just echos “Hello, Cloud!” to standard

# output. It will work in any hook directory.

 

echo “Hello, Cloud!”

# mvn test

./hooks/dev/post-code-deploy/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner -X

 

Configure script to run the code test for every code commit on gitlab

  1. Navigate to project root directory on gitlab repository.
  2. Create .gitlab-ci.yml file
  3. Open the .gitlab-ci.yml file in edit mode
  4. Add the commands/scripts paths to run the sonar code job

Ex:

test:

script:

– export JAVA_HOME=./hooks/dev/post-code-deploy/jdk1.8.0_191/bin

– echo $JAVA_HOME

– chmod -R 775 ./hooks/dev/post-code-deploy/*

– pwd

– ./hooks/dev/post-code-deploy/sonarcode.sh

About The Author