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
- Architecture and Design
- Unit tests
- Duplicated code
- Potential bugs
- Complex code
- Coding standards
Configuration and setup Project on Sonar-Qube console
- Open the project specific sonar-qube console
- Go to your account on top right corner.
- Click on My Account
- On account page click on security.
- Give some token name in “Generate New Token” then click on Generate.
- Sonar token will generate and display on the page.
- Take the sonar token and provide under sonar-project.properties for sonar.login
- Go to Administration on sonarqube home page
- Click on projects, under projects click on Management option
- Click on “Create Project”
- Provide the project name and key name.
- Set Visibility as Public.
- 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.
- Download the latest SonarScanner from sonar source site
- 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
- Navigate to config folder of Sonar-scanner
- 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>
- Navigate environment root directory
Ex: /var/www/html/<environment name>
- Create a configuration file in the root directory of the project: sonar-project.properties
- 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>
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
- Go to post-code-deploy under hooks/dev folder
- Create sonarcode.sh
- Add the command to run when shell script executes
# This sample Cloud Hook script just echos “Hello, Cloud!” to standard
# output. It will work in any hook directory.
echo “Hello, Cloud!”
# mvn test
Configure script to run the code test for every code commit on gitlab
- Navigate to project root directory on gitlab repository.
- Create .gitlab-ci.yml file
- Open the .gitlab-ci.yml file in edit mode
- Add the commands/scripts paths to run the sonar code job
– export JAVA_HOME=./hooks/dev/post-code-deploy/jdk1.8.0_191/bin
– echo $JAVA_HOME
– chmod -R 775 ./hooks/dev/post-code-deploy/*