使用jenkins构建自动化发布系统之Sonar 代码质量管理

使用jenkins构建自动化发布系统之Sonar 代码质量管理

牛恒博 DevOps视角

Sonar 代码质量管理

安装sonar



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 

安装数据库



# 下载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

登录mysql创建相关的数据库


# 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)

编辑sonar的配置文件

编辑sonar连接数据库的方式



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

配置sonar的监听ip和端口



vim /data/app/sonarqube/conf/sonar.properties 
sonar.web.host=0.0.0.0
sonar.web.port=9000

启动sonar服务



/data/app/sonarqube/bin/linux-x86-64/sonar.sh start 

配置sonar

浏览器登录sonar,用户名admin,密码:admin

安装中文插件

使用jenkins构建自动化发布系统之Sonar 代码质量管理

安装相关的语言插件


我们安装一个python的插件

使用jenkins构建自动化发布系统之Sonar 代码质量管理

接着把php,java的插件也安装上,然后重启。

安装SonarQube Scanner

安装


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

编辑sonar scanner的配置文件



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&amp;characterEncoding=utf8
EOF

soncar-scanner 在2.8版本的时候,可以不用配置,soncar.jdbc.username,sonar.jdbc.password,sonar.jdbc.url。只需要配置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.

下载官方测试包 Sonar-examples



cd /data/db/
git clone https://github.com/SonarSource/sonar-examples.git

需要scanner扫描的代码必须包含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

开始扫描 我们什么都不指定就会在当面目录下扫描sonar-project.properties文件,根据配置文件进行扫描工作。扫描之后我们在web界面上就可以看到


pwd 
# /data/db/sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests
/data/app/sonar-scanner/bin/sonar-scanner
  • 登录sonar-在仪表盘中看到了我们刚刚运行的检查

使用jenkins构建自动化发布系统之Sonar 代码质量管理

  • 点开可以看到详细的信息

使用jenkins构建自动化发布系统之Sonar 代码质量管理

上一篇:SonarQube+Jenkins安装工程中遇到的吭


下一篇:sonar安装和使用