A, Sonar Overview
Sonar is an open platform for code quality management. By plug-in mechanism, Sonar can integrate disparate testing tools, code analysis tools, as well as continuous integration tool.
And continuous integration tools (for example, Hudson / Jenkins, etc.) different, Sonar is not simply the result of a different code checking tool (such as FindBugs, PMD, etc.) directly on the Web page, but then these results were different plug-ins processing, code quality metrics by quantifying changes in the way, so you can easily to different sizes and types of engineering code quality management.
In support of the other tools, Sonar not only provides support for IDE, you can online see the results in Eclipse and IntelliJ IDEA these tools in; at the same time Sonar also a large number of continuous integration tools provide interface support, can be easily sustained integrated use of Sonar.
In addition, Sonar plugin can also provide support for other programming languages other than Java, for international reporting and documentation is also good support.
Sonar function is to check whether the code is BUG. In addition to check whether the code is bug has other features, such as: your code rate is the number of comments, the code has some suggestions to prepare recommendations grammar. So called quality management.
Sonar and download related documents can be found at the following links: http://www.sonarqube.org/downloads/ .
Note that the latest version of Sonar requires at least JDK 1.8 and above.
Second, the deployment of Sonar
Note: The following configuration needs to be based Bowen Jenkins + Gitlab achieve continuous integration environment for deployment.
Packages and plug-ins required deployment environment can be accessed through this link to download.
Sonar on the required plug-ins can be downloaded via the website https://docs.sonarqube.org/display/PLUG!
[root@jenkins ~]# wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-5.6.zip
[root@jenkins ~]# unzip sonarqube-5.6.zip
[root@jenkins ~]# mv sonarqube-5.6 /usr/local/sonarqube
[root@jenkins ~]# ln -s /usr/local/sonarqube/bin/linux-x86-64/sonar.sh /usr/local/bin
Third, install the MySQL database
sonar need to use the database, MySQL, Oracle, SQL server are supported in this blog uses MySQL database. If the environment already exist MySQL database, then you do not need to re-deploy, and create the appropriate database and users can!
For simplicity, rapid deployment Mysql database, using scripts installed Mysql database, download a key to install mysql
Tips: sonar seems not to support mysql 5.5, or higher is recommended to install mysql version 5.6, the installation script provides a version of mysql5.7!
[root@jenkins ~]# ls mysql*
mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz mysql.sh
[root@jenkins ~]# sh mysql.sh
Starting MySQL.. SUCCESS!
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@jenkins ~]# mysql -u root -p123 #脚本提供的root的密码是123
#登录mysql数据库
mysql> create database sonar character set utf8 collate utf8_general_ci;
mysql> grant all on sonar.* to 'sonar'@'%' identified by '123.com';
mysql> grant all on sonar.* to 'sonar'@'localhost' identified by '123.com';
mysql> flush privileges;
Third, the configuration Sonar
[root@jenkins ~]# vim /usr/local/sonarqube/conf/sonar.properties
#编辑sonar的主配置文件
sonar.jdbc.username=sonar
sonar.jdbc.password=123.com #指定连接数据库使用的用户及密码
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerforman
#定义连接mysql数据库的地址及端口(默认存在去除注释即可)
sonar.web.host=0.0.0.0
sonar.web.port=9000 # 定义Sonar提供Web页面监听的IP地址和端口
#接下来配置sonar支持中文页面以及支持PHP语言
[root@jenkins ~]# cd /usr/local/sonarqube/extensions/plugins/
[root@jenkins plugins]# ls *.jar #确保这个目录下有这两个jar包
sonar-l10n-zh-plugin-1.11.jar sonar-php-plugin-2.9-RC1.jar
[root@jenkins ~]# sonar.sh start #启动sonar
[root@jenkins ~]# tail -2 /usr/local/sonarqube/logs/sonar.log
2020.02.13 14:59:03 INFO ce[o.s.ce.app.CeServer] Compute Engine is up
2020.02.13 14:59:03 INFO app[o.s.p.m.Monitor] Process[ce] is up
#确保sonar的日志出现以上两行内容
[root@jenkins ~]# ss -lnt | grep 9000
LISTEN 0 25 *:9000 *:*
#确保sonar的端口已经在监听
Access sonar web page, as follows:
Fourth, the implementation code scanning configuration Sonar
[root@jenkins ~]# wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.3.0.1492-linux.zip
[root@jenkins ~]# unzip sonar-scanner-cli-3.3.0.1492-linux.zip
[root@jenkins ~]# mv sonar-scanner-3.3.0.1492-linux/ /usr/local/sonar-scanner
[root@jenkins ~]# ln -s /usr/local/sonar-scanner/bin/sonar-scanner /usr/local/bin/
[root@jenkins ~]# ln -s /usr/local/sonar-scanner/bin/sonar-scanner /usr/bin
[root@jenkins ~]# vim /usr/local/sonar-scanner/conf/sonar-scanner.properties
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
#以下是从sonar的主配置文件中复制:/usr/local/sonarqube/conf/sonar.properties中复制过来的,用于连接数据库
sonar.jdbc.username=sonar
sonar.jdbc.password=123.com
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
Fifth, test code scanning capabilities
[root@jenkins ~]# unzip testalyzer-master.zip #该软件包可从博文开头连接中获取
[root@jenkins ~]# cd testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests/
[root@jenkins php-sonar-runner-unit-tests]# cat sonar-project.properties
#看一下下面的文件中都包含了些什么
sonar.projectKey=org.sonarqube:php-ut-sq-scanner #自定义秘钥,如果秘钥一样,就会自动覆盖之前的测试结果
sonar.projectName=PHP :: PHPUnit :: SonarQube Scanner #web界面显示的名称
sonar.projectVersion=1.0 #版本
sonar.sources=src #软件包存放路径
sonar.tests=tests #测试路径
sonar.language=php #要测试的语言
sonar.sourceEncoding=UTF-8 #编码格式
sonar.php.coverage.reportPath=reports/phpunit.coverage.xml
sonar.php.tests.reportPath=reports/phpunit.xml
#也就是说在项目里面必须有这个配置文件才可以进行扫描
#测试PHP代码
[root@jenkins php-sonar-runner-unit-tests]# pwd #确定在当前路径
/usr/src/testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests
[root@jenkins php-sonar-runner-unit-tests]# sonar-scanner #进行测试
#测试js代码
[root@jenkins php-sonar-runner-unit-tests]# cd /root/testalyzer-master/projects/languages/javascript/javascript-sonar-runner
[root@jenkins javascript-sonar-runner]# sonar-scanner #进行测试
When the execution is complete tests on js and PHP, you can see the following in sonar web interface:
to view detailed information after clicking, as shown:
Sixth, open the configuration Jenkins Sonar
Jenkins log on to the web interface, you need to install plug-ins, there are online and offline installation to install in two ways, I choose here offline installation, you can install their own way online.
1) Click: System Management -> Plugin Manager -> Advanced, then scroll down:
Download the required plug- in accordance with "sonar" -> "gerrit- trigger" -> "sonar-gerrit" can be installed, on-line installation sequence: "SonarQube Scanner" -> " Gerrit Trigger" -> "Sonar Gerrit Plugin "
Figure:
2) Click: System Management -> System Settings, configuration is as follows:
3) Click: System Management -> Global Configuration Tools, and then scroll down:
4) built into the blog articles on the project, as follows:
For convenience of distinction, enter the following code:
sonar.projectKey=web-demo
sonar.projectName=web-demo
sonar.projectVersion=1.0
sonar.sources=src
sonar.tests=tests
sonar.language=php
sonar.sourceEncoding=UTF-8
sonar.php.coverage.reportPath=reports/phpunit.coverage.xml
sonar.php.tests.reportPath=reports/phpunit.xml
5) back to the terminal, to submit code gitlab, can achieve automatic scanning Sonar
[root@jenkins ~]# cp -r testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests/* test01/
[root@jenkins ~]# cd test01/
[root@jenkins test01]# git add .
[root@jenkins test01]# git commit -m "test sonar"
[root@jenkins test01]# git push origin master
Figure:
If it does not, then, on the recommendation Jenkins page to see if building a successful!
Seven, configure Jenkins realized email alert
1) to obtain an authorization code E-mail
The QQ-mail with an example:
2) Alarm Configuration Message:
Jenkins's web interface Click: System Management -> System Settings, then enter the email address of the drop-down and save the system administrator:
drop-down list, continue to fill!
3) configuration items
4) Test
Restart gitlab services, rebuild the project, as follows:
[root@jenkins ~]# gitlab-ctl restart
Figure:
incoming mail as follows:
------------ paper so far, thanks for reading ------------