Utilisez Jenkins pour créer la gestion de la qualité du code Sonar du système de publication automatisé
La perspective DevOps de Niu Hengbo
Gestion de la qualité du code sonar
Installer le sondeur
cd /usr/local/src/
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.6.6.zip
mv sonarqube-5.6.6 /data/app/
ln -s /data/app/sonarqube-5.6.6/ /data/app/sonarqube
Installez la base de données
# 下载mysql二进制包
cd /usr/local/src
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
# 创建mysql用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
# 解压mysql二进制包
cd /usr/local/src
tar zxf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.30-linux-glibc2.5-x86_64 /usr/local/
chown -R mysql:mysql /usr/local/mysql-5.6.30-linux-glibc2.5-x86_64
# 初始化mysql
ln -s /usr/local/mysql-5.6.30-linux-glibc2.5-x86_64/ /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
# 上传压缩包中的my.cnf到/usr/local/mysql目录下
#初始化 mysql数据库
/usr/local/mysql/scripts/mysql_install_db \
--defaults-file=/usr/local/mysql/my.cnf \
--user=mysql --basedir=/usr/local/mysql/ \
--datadir=/usr/local/mysql/data
# 启动mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql-5.6.30-linux-glibc2.5-x86_64/
/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf &
# 连接mysql
/usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock
Connectez-vous à mysql pour créer des bases de données associées
# mysql -uroot -p12345678
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar@qw';
FLUSH PRIVILEGES;
sonar好像不支持MySQL 5.5,请安装mysql5.6 或者更高版本
2017.03.01 18:52:01 ERROR web[o.a.c.c.C.[.[.[/]] Exception sending context initialized event to listener instance of class org.sonar.ser
ver.platform.PlatformServletContextListener
org.sonar.api.utils.MessageException: Unsupported mysql version: 5.5. Minimal supported version is 5.6.
2017.03.01 18:52:01 ERROR web[o.a.c.c.StandardContext] One or more listeners failed to start. Full details will be found in the appropri
ate container log file
2017.03.01 18:52:01 ERROR web[o.a.c.c.StandardContext] Context [] startup failed due to previous errors
2017.03.01 18:52:01 WARN web[o.a.c.l.WebappClassLoaderBase] The web application [ROOT] appears to have started a thread named [Abandone
d connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2017.03.01 18:52:01 WARN web[o.a.c.l.WebappClassLoaderBase] The web application [ROOT] appears to have started a thread named [Timer-0]
but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.util.TimerThread.mainLoop(Timer.java:552)
java.util.TimerThread.run(Timer.java:505)
Modifier le fichier de configuration du sondeur
Modifier la façon dont le sondeur se connecte à la base de données
vim /data/app/sonarqube/conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar@qw
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerf
ormance
Configurer l'IP et le port d'écoute du sondeur
vim /data/app/sonarqube/conf/sonar.properties
sonar.web.host=0.0.0.0
sonar.web.port=9000
Démarrez le service sondeur
/data/app/sonarqube/bin/linux-x86-64/sonar.sh start
Configurer le sondeur
Connexion du navigateur au sondeur, nom d'utilisateur admin, mot de passe: admin
Installer le plug-in chinois
Installez les plug-ins de langue appropriés
Nous installons un plugin python
Ensuite, installez les plug-ins php et java, puis redémarrez.
Installer le scanner SonarQube
installation
wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
unzip sonar-scanner-2.8.zip
mv sonar-scanner-2.8 /data/app/
ln -s /data/app/sonar-scanner-2.8/ /data/app/sonar-scanner
Modifier le fichier de configuration du scanner sondeur
cat >>/data/app/sonar-scanner/conf/sonar-scanner.properties <<EOF
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar@qw
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
EOF
Lorsque soncar-scanner est en version 2.8, il n'est pas nécessaire de configurer soncar.jdbc.username, sonar.jdbc.password, sonar.jdbc.url. Il suffit de configurer soncar.host.url.
WARN: Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
Téléchargez le package de test officiel Sonar-examples
cd /data/db/
git clone https://github.com/SonarSource/sonar-examples.git
Le code qui doit être analysé par le scanner doit inclure sorna-project.properties
cd /data/db/sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests
vim sorna-project.properties
sonar.projectKey=org.sonarqube:php-ut-sq-scanner # sonar中的key,必须唯一。
sonar.projectName=PHP :: PHPUnit :: SonarQube Scanner ##在sonar中展示的名称
sonar.projectVersion=1.0 ##项目版本
sonar.sources=src ##源码路径
sonar.tests=tests
sonar.language=php ##源码语言
sonar.sourceEncoding=UTF-8 ##源码编译方式
# Reusing PHPUnit reports
sonar.php.coverage.reportPath=reports/phpunit.coverage.xml
sonar.php.tests.reportPath=reports/phpunit.xml
sonar.projectKey=org.sonarqube:example-it-jacoco-sonar-scanner
sonar.projectName=Java :: IT Coverage with JaCoCo :: SonarQube Scanner
sonar.projectVersion=1.0
sonar.sources=src
sonar.binaries=classes
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.jacoco.itReportPath=reports/jacoco.exec
Lorsque nous commencerons l'analyse, nous analyserons le fichier sonar-project.properties dans le répertoire courant sans rien spécifier, et analyserons en fonction du fichier de configuration. Après la numérisation, nous pouvons le voir sur l'interface Web
pwd
# /data/db/sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests
/data/app/sonar-scanner/bin/sonar-scanner
- Connectez-vous au sondeur - voyez la vérification que nous venons d'exécuter dans le tableau de bord
- Cliquez pour ouvrir pour voir des informations détaillées