使用 Sonar 進行代碼質量管理及郵件報警

1、Sonar 概述

Sonar 是一個用於代碼質量管理的開放平臺。經過插件機制,Sonar 能夠集成不一樣的測試工具,代碼分析性工具,以及持續集成工具。javascript

與持續集成工具(例如 Hudson/Jenkins 等)不一樣,Sonar 並非簡單地把不一樣的代碼檢查工具結果(例如 FindBugs,PMD 等)直接顯示在 Web 頁面上,而是經過不一樣的插件對這些結果進行再加工處理,經過量化的方式度量代碼質量的變化,從而能夠方便地對不一樣規模和種類的工程進行代碼質量管理。php

在對其餘工具的支持方面,Sonar 不只提供了對 IDE 的支持,能夠在 Eclipse 和 IntelliJ IDEA 這些工具裏聯機查看結果;同時 Sonar 還對大量的持續集成工具提供了接口支持,能夠很方便地在持續集成中使用 Sonar。java

此外,Sonar 的插件還能夠對 Java 之外的其餘編程語言提供支持,對國際化以及報告文檔化也有良好的支持。mysql

Sonar 的功能就是來檢查代碼是否有 BUG。除了檢查代碼是否有 bug 還有其餘的功能,好比說:你的代碼註釋率是多少,代碼有一些建議,編寫語法的建議。因此稱之爲質量管理。linux

Sonar 的相關下載和文檔能夠在下面的連接中找到:http://www.sonarqube.org/downloads/
須要注意最新版的 Sonar 須要至少 JDK 1.8 及以上版本。git

2、部署Sonar

注意:接下來的配置須要基於博文Jenkins+Gitlab實現持續集成的環境進行部署的。web

部署環境中所需的軟件包及插件都可經過此連接進行下載。
關於Sonar所需的插件也可經過https://docs.sonarqube.org/display/PLUG 該網址進行下載!sql

[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

3、安裝MySQL數據庫

sonar須要使用數據庫,MySQL、Oracle、SQL server都是支持的,本次博文采用MySQL數據庫。若是環境已經存在MySQL數據庫,那麼就無需再次部署了,建立相應的數據庫及用戶便可!數據庫

爲了簡單、快速的部署Mysql數據庫,採用腳本的方式安裝Mysql數據庫,下載一鍵安裝mysql編程

友情提示:sonar 好像不支持 mysql 5.5,建議安裝mysql 5.6或更高的版本,提供的腳本安裝的是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;

3、配置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的端口已經在監聽

訪問sonar的web頁面,以下:
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警

4、配置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

5、測試代碼掃描功能

[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      #進行測試

當執行完成對js及PHP的測試後,便可在sonar的web界面看到以下內容:
使用 Sonar 進行代碼質量管理及郵件報警
點擊以後便可查看詳細信息,如圖:
使用 Sonar 進行代碼質量管理及郵件報警

6、配置Jenkins開啓Sonar

登陸到Jenkins的web界面,須要安裝插件,有在線安裝及離線安裝兩種方式,我這裏選擇離線安裝,能夠自行進行在線安裝的方式。

1)依次點擊:系統管理——>插件管理——>高級,而後下拉頁面:

下載所需插件按照「sonar」——>「gerrit-trigger」——>「sonar-gerrit」進行安裝便可,在線安裝順序:「SonarQube Scanner」——>「Gerrit Trigger」——>"Sonar Gerrit Plugin"
如圖:
使用 Sonar 進行代碼質量管理及郵件報警

2)依次點擊:系統管理——>系統設置,配置以下:

使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警

3)依次點擊:系統管理——>全局工具配置,而後下拉頁面:

使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警

4)進入上篇博文構建的項目中,以下:

使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警

爲了便於區分,輸入如下代碼:

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

使用 Sonar 進行代碼質量管理及郵件報警

5)回到終端,向gitlab提交代碼,便可實現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

如圖:
使用 Sonar 進行代碼質量管理及郵件報警
若是沒有出現的話,建議上Jenkins頁面查看是否構建成功!

7、配置Jenkins實現郵件報警

1)獲取郵箱受權碼

本次以QQ郵箱爲例:
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警

2)配置郵件報警:

在Jenkins的web界面依次點擊:系統管理——>系統設置,而後下拉輸入系統管理員的郵箱地址並保存:
使用 Sonar 進行代碼質量管理及郵件報警
下拉列表,繼續填寫!
使用 Sonar 進行代碼質量管理及郵件報警

3)配置項目

使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警
使用 Sonar 進行代碼質量管理及郵件報警

4)測試

重啓gitlab服務,從新構建項目,以下:

[root@jenkins ~]# gitlab-ctl restart

如圖:
使用 Sonar 進行代碼質量管理及郵件報警
收到的郵件以下:
使用 Sonar 進行代碼質量管理及郵件報警

————————————本文到此爲止,感謝閱讀————————————

相關文章
相關標籤/搜索