Sonar step by step
Machine A (192.168.1.1): Centos + SonarQube
Machine B (192.168.1.2): Centos + SonarQube Runner + Jenkins + SCM + build tools...
Machine A: Centos + SonarQube
192.168.1.1
Download SonarQube:
http://www.sonarqube.org/downloads/
http://downloads.sonarsource.com/sonarqube/sonarqube-5.1.1.zip
Install MySQL
- Create user root with password "root1"
- Create a database "sonar" for user "sonar", password "sonar"
yum install mysql mysql-server
service mysqld start
mysql -u root -p
UPDATE mysql.user SET Password=PASSWORD('root1') WHERE User='root';
exit;
service mysqld restart
mysql -u root -p
(type root1)
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;
Unzip SonarQube.
Hide password in property files.
Assume database password is "sonar", create a new file like this (I recommend use the password "sonar", we can change it later).
/opt/sonar/sonarqube-5.1.1/conf/sonar-secret.txt
YC4GY+p0MvzL/MB+B1uxTw==
Configure SonarQube.
Open this file and edit like below.
/opt/sonar/sonarqube-5.1.1/conf/sonar.properties
sonar.secretKeyPath=/opt/sonar/sonarqube-5.1.1/conf/sonar-secret.txt
#--------------------------------------------------------------------------------------------------
# DATABASE
#
# IMPORTANT: the embedded H2 database is used by default. It is recommended for tests but not for
# production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.
# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonar
sonar.jdbc.password={aes}eCETYLJdWsM9qrCRL4bpzw==
#----- Embedded Database (default)
# It does not accept connections from remote hosts, so the
# server and the analyzers must be executed on the same host.
#sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092
#----- MySQL 5.x
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported.
sonar.jdbc.url=jdbc:mysql://192.168.1.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
Start the SonarQube
/opt/sonar/sonarqube-5.1.1/bin/sonar.sh start
Open this link in web browser and log in with account "admin", password "admin".
http://localhost:9000
or http://192.168.1.1:9000
To change password and encrypt, go here and generate:
http://192.168.1.1:9000/settings?category=security&subcategory=encryption
Machine B: Centos + SonarQube Runner + Jenkins + SCM + build tools...
192.168.1.2
Download SonarQube Runner:
http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
Unzip SonarQube Runner and edit sonar-runner.properties
/opt/sonar/sonar-runner-2.4/conf/sonar-runner.properties
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here
#----- Default SonarQube server
#sonar.host.url=http://localhost:9000
#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#----- MySQL
sonar.jdbc.url=jdbc:mysql://192.168.1.1:3306/sonar?useUnicode=true&characterEncoding=utf8
#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
#----- Global database settings
sonar.jdbc.username=sonar
sonar.jdbc.password={aes}eCETYLJdWsM9qrCRL4bpzw==
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
#----- Security (when 'sonar.forceAuthentication' is set to 'true')
sonar.login=admin
sonar.password={aes}2PKkHMGvtpPJiaACizGZGw==
Jenkins
192.168.1.2
Download and install the SonarQube plugin
https://wiki.jenkins-ci.org/display/JENKINS/SonarQube+plugin
Configure SonarQube plugin, go to:
http://192.168.1.2:8080/configure
SonarQube Runner > SonarQube Runner installation...
SONAR_RUNNER_HOME = /opt/sonar/sonar-runner-2.4
Uncheck: Install automatically
SonarQube > Advanced...
Server URL = http://192.168.1.1:9000/
SonarQube account login = admin
SonarQube account login = admin
...
For Maven project, we need to input others fieds. But I assume that we use Ant. Then we only use SonarQube Runner to analysis our project.
Create a new Free style Jenkins Jobs
In the Build section, add a Invoke Standalone SonarQube Analysis step
Edit this one:
Analysis properties
sonar.projectBaseDir=/opt/mysource
# required metadata
sonar.projectKey=my:ProjectKey
sonar.projectName=MyProject
sonar.projectVersion=1.0
# path to source directories (required)
sonar.sources=/opt/mysource/java/src
# Uncomment this line to analyse a project which is not a java project.
# The value of the property must be the key of the language.
sonar.language=java
sonar.scm.provider=git
sonar.branch=my_branch
sonar.exclusions=**/temp/**.java,
Backup and Restore MYSQL database
# backup
mysqldump -u root -p sonar > sonar511.sql
# restore
mysql -u root -p -p sonar < sonar511.sql