Continuous integration of code quality management --- Sonar

Sonar is an open platform for code quality management through plug-in mechanism, Sonar can integrate disparate testing tools, code analysis tools and continuous integration tool. Tools and continuous integration (e.g., Hudson / Jenkins, etc.) different, Sonar is not simply the result of different check codes (e.g.: FindBugs, PMD, etc.) directly on the web UI interface, but further processing of these results through 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 has a 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.

First, the deployment of Sonar

Bowen next configuration is based on the deployment Jenkins + Gitlab achieve continuous integration environment to deploy
all of the source packages and plug-ins are available on this link (extraction code: t976) Download

[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/

Second, install MySQL

sonar need to use the database, here I use the MySQL database, if there is a database environment, you do not need to deploy, only need to create the appropriate bank and account number to

#采用RPM包的方式部署MySQL
[root@jenkins ~]# mkdir mysql
[root@jenkins ~]# cd mysql/
[root@jenkins mysql]# rz
[root@jenkins mysql]# ls            # 确定有如下几个包
mysql-community-client-5.7.25-1.el7.x86_64.rpm 
mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm
mysql-community-common-5.7.25-1.el7.x86_64.rpm  
mysql-community-server-5.7.25-1.el7.x86_64.rpm
mysql-community-libs-5.7.25-1.el7.x86_64.rpm
[root@jenkins mysql]# yum -y localinstall mysql-community-*       # 安装MySQL
[root@jenkins mysql]# systemctl start mysqld         # 启动
[root@jenkins mysql]# netstat -anput | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      11411/mysqld        
[root@jenkins mysql]# grep password /var/log/mysqld.log        # 在MySQL日志中查看登陆密码 
2020-03-19T17:29:37.381630Z 1 [Note] A temporary password is generated for root@localhost: DfkgCtPsu5:4
#上面末尾就是MySQL的默认root密码
[root@jenkins mysql]# mysql -u root -p'DfkgCtPsu5:4'
mysql> alter user 'root'@'localhost' identified by '[email protected]';
mysql> create database sonar character set utf8 collate utf8_general_ci;
mysql> grant all on sonar.* to 'sonar'@'%' identified by '[email protected]';
mysql> grant all on sonar.* to 'sonar'@'localhost' identified by '[email protected]';
mysql> flush privileges;

Third, the configuration Sonar

[root@jenkins mysql]# cd /usr/local/sonarqube/conf/
[root@jenkins conf]# sed -i 's/#sonar.jdbc.username=/sonar.jdbc.username=sonar/g' sonar.properties 
#定义连接数据库的用户
[root@jenkins conf]# sed -i 's/#sonar.jdbc.password=/[email protected]/g' sonar.properties
#指定数据库用户的密码
[root@jenkins conf]# echo 'sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance' >> sonar.properties 
#用来定义连接数据库的地址及端口
[root@jenkins conf]# sed -i 's/#sonar.web.port=9000/sonar.web.port=9000/g' sonar.properties
#配置sonar支持中文页面及支持PHP
[root@jenkins conf]# cd /usr/local/sonarqube/extensions/plugins/
[root@jenkins plugins]# cp ~/sonar-l10n-zh-plugin-1.11.jar ./
[root@jenkins plugins]# cp ~/sonar-php-plugin-2.9-RC1.jar ./
[root@jenkins plugins]# sonar.sh start            #启动sonar,给它点初始化的时间
[root@jenkins conf]# tail -2 /usr/local/sonarqube/logs/sonar.log              #查看sonar日志
2020.03.20 02:00:29 INFO  ce[o.s.ce.app.CeServer] Compute Engine is up
2020.03.20 02:00:29 INFO  app[o.s.p.m.Monitor] Process[ce] is up
#当出现上面两行以“up”结尾的则表示sonar启动正常。
[root@jenkins conf]# netstat -anput | grep 9000
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN      52610/java   

client access sonar web interface:
Continuous integration of code quality management --- Sonar

Fourth, the scan function configuration code

#配置代码扫描
[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 ~]# cd /usr/local/sonar-scanner/conf/
[root@jenkins conf]# egrep -v '^#|^$' sonar-scanner.properties            # 修改配置文件至如下
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
#以下是从sonar的主配置文件中复制:/usr/local/sonarqube/conf/sonar.properties中复制过来的,用于连接数据库
sonar.jdbc.username=sonar
[email protected]
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          #编码格式
#测试PHP代码
[root@jenkins php-sonar-runner-unit-tests]# pwd           # 确定当前路径
/root/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 ../../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:
Continuous integration of code quality management --- Sonar
you can see details Click to enter:
Continuous integration of code quality management --- Sonar

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 on their own line of
1, then click: System Management Plug-In Manager == === >> = >> advanced, then scroll down:
download the plug I provide, and then click Add in the following order, if the online installation, and then click Search "sonarQube Scanner", "Gerrit Trigger ", "Sonar Gerrit plugin" can be installed:
Continuous integration of code quality management --- Sonar
2, and Hits: system Administration === >> system settings, then configured as follows:
Continuous integration of code quality management --- Sonar
Continuous integration of code quality management --- Sonar
Remember to save points
3, then click: system Administration === >> global configuration tool, and then click the following:
Continuous integration of code quality management --- Sonar
Continuous integration of code quality management --- Sonar
4, built into the project, and then click to enter as follows:
Continuous integration of code quality management --- Sonar
Continuous integration of code quality management --- Sonar
Continuous integration of code quality management --- Sonar
copy the code in the terminal:

[root@jenkins /]# cd ~/testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests/
[root@jenkins php-sonar-runner-unit-tests]# egrep -v '^#|^$' sonar-project.properties 
#复制如下代码
sonar.projectKey=org.sonarqube:php-ut-sq-scanner
sonar.projectName=PHP :: PHPUnit :: SonarQube Scanner
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, and then paste the copied code in FIG. (For convenience to distinguish between the test results, it is recommended to modify the value "sonar.projectKey") is:
Continuous integration of code quality management --- Sonar
6, back to the terminal, to submit code gitlab, can automatically scan sonar

[root@jenkins php-sonar-runner-unit-tests]# pwd
/root/testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests
[root@jenkins php-sonar-runner-unit-tests]# cp -r * ~/test1/
#将测试代码复制到本地git库(如果没有本地git库,可以再次克隆一下)
[root@jenkins php-sonar-runner-unit-tests]# cd ~/test1/           # 进入到本地git库
[root@jenkins test1]# rm -rf sonar-project.properties              #删除这个文件,不需要使用它来给我们指定要测试的代码了
#因为我们在刚才的web界面已经将其配置文件写在了web界面。优先使用web界面配置的检测代码。
#不删除这个文件也行
#然后提交到远端的gitlab库
[root@jenkins test1]# git add *
[root@jenkins test1]# git commit -m "test sonar"
[root@jenkins test1]# git push origin master

When submitted to the distal end gitlab library to see sonar web interface code has been scanned, and scan results showed as follows:
Continuous integration of code quality management --- Sonar
7, arranged Jenkins alert message
start messages alarm configuration:
Click, Jenkins web interface : systems management === >> system settings, and then enter the email address below and save the system administrator:
Continuous integration of code quality management --- Sonar
click again: system management === >> system settings
Continuous integration of code quality management --- Sonar
configuration items, as follows:
Continuous integration of code quality management --- Sonar
Continuous integration of code quality management --- Sonar
Continuous integration of code quality management --- Sonar
Continuous integration of code quality management --- Sonar
click here >> project configuration
Continuous integration of code quality management --- Sonar
Continuous integration of code quality management --- Sonar
Continuous integration of code quality management --- Sonar
Continuous integration of code quality management --- Sonar
so far, E-mail alarm on the configuration, this time, you can turn off the console gitlab or manually build the code, test whether you can receive alarm messages.
Continuous integration of code quality management --- Sonar

Guess you like

Origin blog.51cto.com/14227204/2480387