|
|
# SonarQube
|
|
|
You can find SonarQube at [sonar.mphslaats.com](https://sonar.mphslaats.com).
|
|
|
|
|
|
# Table of contents
|
|
|
- [SonarQube](#sonarqube)
|
|
|
- [Table of contents](#table-of-contents)
|
|
|
- [Resources](#resources)
|
|
|
- [Ports](#ports)
|
|
|
- [Installation](#installation)
|
|
|
- [Setup SonarQube for Maven (Jenkins)](#setup-sonarqube-for-maven-jenkins)
|
|
|
|
|
|
## Resources
|
|
|
It is [recommended](https://docs.sonarqube.org/display/SONAR/Requirements#Requirements-HardwareRequirements) to use at least 2 GB of RAM for SonarQube. Therefore I chose to go with 2 GB RAM and 2 CPU cores.
|
|
|
|
|
|
## Ports
|
|
|
By default SonarQube runs on port 9000, however SonarQube is a website therefore you only need to add a new server section to the Proxy server, which handles the rest.
|
|
|
|
|
|
## Installation
|
|
|
* Let's install java: `apt-get install openjdk-8-jre`;
|
|
|
* Install PostgreSQL as a database service: `apt-get install postgresql && systemctl start postgresql && systemctl enable postgresql`;
|
|
|
* Setup PostgreSQL as follows:
|
|
|
* `su - postgres`;
|
|
|
* `createuser sonar`;
|
|
|
* Let's first [initialize](https://gist.github.com/ffmike/877447) the database for UTF8: `psql`:
|
|
|
```bash
|
|
|
update pg_database set datallowconn = TRUE where datname = 'template0';
|
|
|
\c template0
|
|
|
update pg_database set datistemplate = FALSE where datname = 'template1';
|
|
|
drop database template1;
|
|
|
create database template1 with template = template0 encoding = 'UTF8';
|
|
|
update pg_database set datistemplate = TRUE where datname = 'template1';
|
|
|
\c template1
|
|
|
update pg_database set datallowconn = FALSE where datname = 'template0';
|
|
|
\q
|
|
|
```
|
|
|
* Now let's create our database: `psql`:
|
|
|
```bash
|
|
|
ALTER USER sonar WITH ENCRYPTED password '****';
|
|
|
CREATE DATABASE sonar OWNER sonar encoding = 'UTF8';
|
|
|
\q
|
|
|
```
|
|
|
* Now go back to the root user by using `exit`;
|
|
|
* Now let's download SonarQube: `wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.7.1.zip` (you can check the latest version [here](https://www.sonarqube.org/downloads/));
|
|
|
* Download unzip by using `apt-get install unzip`;
|
|
|
* Unzip SonarQube in `/opt` by using `unzip sonarqube-6.7.1.zip -d /opt`;
|
|
|
* Rename the folder: `mv /opt/sonarqube-6.7.1 /opt/sonar`;
|
|
|
* Now edit the configuration file `/opt/sonar/conf/sonar.properties`:
|
|
|
```bash
|
|
|
sonar.jdbc.username=sonar
|
|
|
sonar.jdbc.password=****
|
|
|
sonar.jdbc.url=jdbc:postgresql://localhost/sonar
|
|
|
```
|
|
|
* Now create a service file `/etc/systemd/system/sonar.service`:
|
|
|
```bash
|
|
|
[Unit]
|
|
|
Description=SonarQube service
|
|
|
After=syslog.target network.target
|
|
|
|
|
|
[Service]
|
|
|
Type=forking
|
|
|
|
|
|
ExecStart=/opt/sonar/bin/linux-x86-64/sonar.sh start
|
|
|
ExecStop=/opt/sonar/bin/linux-x86-64/sonar.sh stop
|
|
|
|
|
|
User=sonar
|
|
|
Group=sonar
|
|
|
#Restart=always
|
|
|
|
|
|
[Install]
|
|
|
WantedBy=multi-user.target
|
|
|
```
|
|
|
* Now let's create a sonar user because otherwise we will get [an error](https://stackoverflow.com/questions/46857688/getting-elasticsearch-can-not-run-as-root-error-after-upgrading-from-sonarqube) with elasticsearch by using `useradd sonar`;
|
|
|
* Now change the rights of the sonar folder by using `chown -R sonar:sonar /opt/sonar`;
|
|
|
* Edit the `/opt/sonar/bin/linux-x86-64/sonar.sh` file so it uses the correct user:
|
|
|
```bash
|
|
|
RUN_AS_USER=sonar
|
|
|
```
|
|
|
* Now let's try starting the server by using `systemctl start sonar && systemctl enable sonar`;
|
|
|
* Now go to <IP-address>:9000 and use admin - admin as credentials.
|
|
|
|
|
|
## Setup SonarQube for Maven (Jenkins)
|
|
|
* Go to the Maven's `pom.xml` file and add the following:
|
|
|
```xml
|
|
|
<project>
|
|
|
<properties>
|
|
|
<!-- Jacoco -->
|
|
|
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
|
|
|
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
|
|
|
</properties>
|
|
|
|
|
|
<build>
|
|
|
<plugins>
|
|
|
<!-- Jacoco -->
|
|
|
<plugin>
|
|
|
<!-- https://mvnrepository.com/artifact/org.jacoco/jacoco-maven-plugin -->
|
|
|
<groupId>org.jacoco</groupId>
|
|
|
<artifactId>jacoco-maven-plugin</artifactId>
|
|
|
<version>0.8.3</version>
|
|
|
<configuration>
|
|
|
<append>true</append>
|
|
|
</configuration>
|
|
|
<executions>
|
|
|
<execution>
|
|
|
<goals>
|
|
|
<goal>prepare-agent</goal>
|
|
|
</goals>
|
|
|
</execution>
|
|
|
<execution>
|
|
|
<id>post-unit-test</id>
|
|
|
<phase>test</phase>
|
|
|
<goals>
|
|
|
<goal>report</goal>
|
|
|
</goals>
|
|
|
</execution>
|
|
|
</executions>
|
|
|
</plugin>
|
|
|
</plugins>
|
|
|
</build>
|
|
|
</project>
|
|
|
```
|
|
|
* Now go to the item in Jenkins and enable `Prepare SonarQube Scanner environment` in the Bindings;
|
|
|
* In the build step you have to add `clean install test $SONAR_MAVEN_GOAL -Dsonar.host.url=$SONAR_HOST_URL -e` in the Goals and options;
|
|
|
* To fail a job because of SonarQube you have to add a Post Step, which is described [here](https://git.mphslaats.com/mphslaats/server-documentation/snippets/17) |
|
|
\ No newline at end of file |