User Tools
Writing /app/www/public/data/meta/toolsandtechnologies/sonarqube.meta failed
toolsandtechnologies:sonarqube
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| toolsandtechnologies:sonarqube [2017/05/18 15:53] – pmurphy | toolsandtechnologies:sonarqube [2021/06/25 10:09] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== SonarQube Code Quality ====== | ||
| + | Author : Paddy Murphy | ||
| + | \\ Date : 2017-05-03 | ||
| + | |||
| + | **What is it?** | ||
| + | |||
| + | SonarQube is an open source tool which gives a snapshot of code quality. It uses the following quality metrics: | ||
| + | * **Potential Bugs** - This uses coding rules to detect potential bugs, bugs can be ranked as Blocker, Critical, Major, Minor, or Info | ||
| + | * **Tests** - This measures unit-test coverage | ||
| + | * **Duplications** - Detect copy-paste code and give a percentage value | ||
| + | * **Comments** - Measures the percentage of comments. Only API comments are measured; inline comments are ignored. | ||
| + | * **Architecture & Design** - only supported for Java analysis, LCOM4 determines how the code stacks up from an object oriented design perspective, | ||
| + | * **Complexity** - Measures how complex the logic of the code is; this is calculated by adding the complexity value for each file analysed. | ||
| + | |||
| + | |||
| + | It consists of two components a SonarQube server which displays the the results and a SonarQube scanner which carries out the code analysis. | ||
| + | |||
| + | **Where is it installed? | ||
| + | |||
| + | The SonarQube server is currently installed on the erato server. | ||
| + | View dashboard at: [[http:// | ||
| + | |||
| + | Log in with the usual credentials, | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ===== How to Install SonarQube Server on Ubuntu ===== | ||
| + | SonarQube requires Java 8 and MySQL – 5.6 or greater | ||
| + | * wget https:// | ||
| + | * sudo unzip sonarqube-5.6.4.zip | ||
| + | * sudo nano sonarqube-5.6.4/ | ||
| + | * uncomment the following lines | ||
| + | \\ # | ||
| + | \\ # | ||
| + | \\ # | ||
| + | |||
| + | Add the following values: | ||
| + | \\ sonar.web.host=< | ||
| + | \\ sonar.web.context=/ | ||
| + | \\ sonar.web.port=9000 | ||
| + | |||
| + | **Start Application** | ||
| + | * cd / | ||
| + | * sudo ./sonar.sh start | ||
| + | * point browser at: | ||
| + | * http:// | ||
| + | |||
| + | **Add sonar database** | ||
| + | \\ By default SonarQube uses the H2 database engine. This is not suitable for a production instance. | ||
| + | \\ Ensure that a MySQl version 5.6 or greater is installed. | ||
| + | * login to mysql server | ||
| + | * create database sonar; | ||
| + | * sudo nano sonarqube-5.6.4/ | ||
| + | * uncomment the JDBC MySQL Driver line & the following lines: | ||
| + | |||
| + | \\ # | ||
| + | \\ # | ||
| + | |||
| + | Change values: | ||
| + | \\ # | ||
| + | \\ # | ||
| + | |||
| + | If everything is configured correctly the dashboard page should display when SonarQube is restarted. | ||
| + | |||
| + | ===== Troubleshooting on erato server ===== | ||
| + | |||
| + | Logs can be viewed at: / | ||
| + | |||
| + | **Stop application** | ||
| + | |||
| + | * cd / | ||
| + | * sudo ./sonar.sh stop | ||
| + | |||
| + | |||
| + | |||
| + | **Restart Application** | ||
| + | |||
| + | * cd / | ||
| + | * sudo ./sonar.sh start | ||
| + | |||
| + | **Sonarqube won't start with ElasticSearch exceptions in the log** | ||
| + | * you will see something like ' | ||
| + | * most likely the elasticsearch data is corrupted | ||
| + | * you will need to stop the sonarqube and delete the elasticsearch folder: | ||
| + | * //sudo rm -rf / | ||
| + | * start sonarqube again | ||
| + | * sonarqube will rebuild the elasticsearch data startup (which can take few minutes) | ||
| + | * check the ui if it works | ||
| + | |||
| + | |||
| + | ===== Jenkins 2 Integration ===== | ||
| + | Install the Jenkins SonarQube plugin | ||
| + | |||
| + | **Add SonarQube Server** | ||
| + | |||
| + | See Setting up SonarQube in [[toolsandtechnologies: | ||
| + | |||
| + | **Add SonarQube scanner** | ||
| + | |||
| + | * Go to Jenkins, Manage Jenkins, Global Tool Configuration | ||
| + | * In the JDK section install a Java 8 version if not already installed | ||
| + | * In SonarQube Scanner section select Add SonarQube Scanner | ||
| + | * Give the scanner a suitable name | ||
| + | * From dropdown select Sonarqube Scanner 2.8 | ||
| + | * At bottom of page select Apply and Save | ||
| + | |||
| + | **Add SonarQube Scanner build step to Grails 2 Project** | ||
| + | |||
| + | As this build step can take some time on larger projects this build step should be last. | ||
| + | |||
| + | * Choose add build step and select Execute SonarQube Scanner. | ||
| + | * For JDK Select a Java 8 version | ||
| + | * In Analysis properties use template below and edit to suit your needs. | ||
| + | |||
| + | < | ||
| + | # must be unique in a given SonarQube instance | ||
| + | sonar.projectKey=com.errigal.reporting-manager | ||
| + | # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. | ||
| + | sonar.projectName=reporting-manager | ||
| + | sonar.projectVersion=1.0 | ||
| + | |||
| + | # Path is relative to the sonar-project.properties file. Replace " | ||
| + | # Since SonarQube 4.2, this property is optional if sonar.modules is set. | ||
| + | # If not set, SonarQube starts looking for source code from the directory containing | ||
| + | # the sonar-project.properties file. | ||
| + | sonar.sources=. | ||
| + | |||
| + | # Encoding of the source code. Default is default system encoding | ||
| + | # | ||
| + | |||
| + | # Exclude files from SonarQube analysis | ||
| + | sonar.exclusions=src/ | ||
| + | # Language | ||
| + | # | ||
| + | </ | ||
| + | SonarQube Scanner Build Example | ||
| + | |||
| + | {{: | ||
| + | |||
| + | * If no language is specified an analysis is done on all Java, Groovy, & JavaScript code. | ||
| + | * Use the following syntax to specify an analysis for a single language only. | ||
| + | |||
| + | < | ||
| + | # Groovy | ||
| + | sonar.language=grvy | ||
| + | |||
| + | # Java | ||
| + | sonar.language=java | ||
| + | |||
| + | # JavaScript | ||
| + | sonar.language=js | ||
| + | </ | ||
| + | |||
| + | **Grails 3 Projects** | ||
| + | \\ Grails 3 projects use the Gradle build system. | ||
| + | \\ See the Alarm-cache Project for an example of grails 3 and SonarQube integration (http:// | ||
| + | |||
| + | build.gradle | ||
| + | < | ||
| + | dependencies { | ||
| + | classpath " | ||
| + | classpath " | ||
| + | classpath " | ||
| + | classpath " | ||
| + | } | ||
| + | } | ||
| + | ... | ||
| + | |||
| + | apply plugin: " | ||
| + | if (JavaVersion.current() == JavaVersion.VERSION_1_8) { | ||
| + | apply plugin: " | ||
| + | //Define sonar url and login token for all sub project | ||
| + | // | ||
| + | sonarqube { | ||
| + | properties { | ||
| + | property " | ||
| + | property " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Jenkinsfile.groovy | ||
| + | |||
| + | < | ||
| + | #!groovy | ||
| + | // Define versioning strategy | ||
| + | versionNumber = " | ||
| + | currentBuild.displayName = versionNumber | ||
| + | // ID of the Jenkins credentials for interaction with BitBucket | ||
| + | bitbucketCredentialsId = ' | ||
| + | // The hostname/ | ||
| + | repoLocation = ' | ||
| + | |||
| + | ... | ||
| + | |||
| + | stage(' | ||
| + | try { | ||
| + | sh ' | ||
| + | } finally { | ||
| + | // Archive test result so they are browsable from Jenkins | ||
| + | step([$class: | ||
| + | } | ||
| + | } | ||
| + | |||
| + | stage(' | ||
| + | try { | ||
| + | sh ' | ||
| + | } finally { | ||
| + | // Archive test result so they are browsable from Jenkins | ||
| + | step([$class: | ||
| + | } | ||
| + | } | ||
| + | |||
| + | stage(' | ||
| + | try { | ||
| + | sh ' | ||
| + | } finally { | ||
| + | // Archive coverage result so they are browsable from Jenkins | ||
| + | step([$class: | ||
| + | } | ||
| + | } | ||
| + | |||
| + | stage(' | ||
| + | sh " | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Jenkins 2 & Grails 3 integration | ||
| + | {{: | ||