1. 程式人生 > >持續整合之整合Jenkins與程式碼質量管理平臺Sonar並實現構建失敗郵件通知

持續整合之整合Jenkins與程式碼質量管理平臺Sonar並實現構建失敗郵件通知

一:Sonar是什麼?
Sonar 是一個用於程式碼質量管理的開放平臺,通過外掛機制,Sonar 可以整合不同的測試工具,程式碼分析工具,以及持續整合工具。與持續整合工具(例如 Hudson/Jenkins 等)不同,Sonar 並不是簡單地把不同的程式碼檢查工具結果(例如FindBugs,PMD等)直接顯示在Web頁面上,而是通過不同的外掛對這些結果進行再加工處理,通過量化的方式度量程式碼質量的變化,從而可以方便地對不同規模和種類的工程進行程式碼質量管理。在對其他工具的支援方面,Sonar 不僅提供了對 IDE 的支援,可以在 Eclipse 和 IntelliJ IDEA 這些工具裡聯機檢視結果;同時Sonar還對大量的持續整合工具提供了介面支援,可以很方便地在持續整合中使用 Sonar,此外,Sonar 的外掛還可以對 Java 以外的其他程式語言提供支援,對國際化以及報告文件化也有良好的支援。

#官方網站:http://www.sonarqube.org/

Sonar部署
Sonar的相關下載和文件可以在下面的連結中找到:http://www.sonarqube.org/downloads/。需要注意最新版的Sonar需要至少JDK 1.8及以上版本

cd /usr/local/src
cd /usr/local/src/
wget https://sonarsource.bintray.com/Distribution/sonarqube//sonarqube-5.6.6.zip
unzip sonarqube-5.6.6.zip
mv sonarqube-5.6.6 /usr/local/
ln -s /usr/local/sonarqube-5.6.6/ /usr/local/sonarqube  

準備Sonar資料庫(mysql版本要等於5.6或者5.6以上,否則sonar無法啟動)

mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql
> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY '[email protected]';
mysql
> FLUSH PRIVILEGES;

配置Sonar

# cd /usr/local/sonarqube/conf/

修改配置檔案的資料庫配置

# egrep '^[a-Z]' sonar.properties
sonar.jdbc.username=sonar

[email protected]
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

啟動Sonar
你可以在Sonar的配置檔案來配置Sonar Web監聽的IP地址和埠,預設是9000埠。

# vim sonar.properties
sonar.web.host=0.0.0.0
sonar.web.port=9000 

# 啟動有建立表和其他操作,速度會有點慢

[[email protected] conf]# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
Starting SonarQube...
Started SonarQube.

報錯:
org.sonar.api.utils.MessageException: Unsupported mysql version: 5.5. Minimal supported version is 5.6.
2017.07.01 11:16:27 ERROR web[o.a.c.c.StandardContext] One or more listeners failed to start. Full details will be found in the appropriate container log file
2017.07.01 11:17:09 INFO web[o.a.c.u.SessionIdGeneratorBase] Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [41,747] milliseconds.
2017.07.01 11:17:09 ERROR web[o.a.c.c.StandardContext] Context [] startup failed due to previous errors
2017.07.01 11:17:09 WARN web[o.a.c.l.WebappClassLoaderBase] The web application [ROOT] appears to have started a thread named [Abandoned 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.07.01 11:17:09 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)
2017.07.01 11:17:09 INFO web[o.a.c.h.Http11NioProtocol] Starting ProtocolHandler ["http-nio-0.0.0.0-9000"]
2017.07.01 11:17:09 INFO web[o.s.s.a.TomcatAccessLog] Web server is started
2017.07.01 11:17:09 INFO web[o.s.s.a.EmbeddedTomcat] HTTP connector enabled on port 9000
2017.07.01 11:17:09 WARN web[o.s.p.ProcessEntryPoint] Fail to start web
java.lang.IllegalStateException: Webapp did not start
at org.sonar.server.app.EmbeddedTomcat.isUp(EmbeddedTomcat.java:84) ~[sonar-server-5.6.6.jar:na]
at org.sonar.server.app.WebServer.isUp(WebServer.java:47) [sonar-server-5.6.6.jar:na]
at org.sonar.process.ProcessEntryPoint.launch(ProcessEntryPoint.java:105) ~[sonar-process-5.6.6.jar:na]
at org.sonar.server.app.WebServer.main(WebServer.java:68) [sonar-server-5.6.6.jar:na]
2017.07.01 11:17:09 INFO web[o.a.c.h.Http11NioProtocol] Pausing ProtocolHandler ["http-nio-0.0.0.0-9000"]
2017.07.01 11:17:16 INFO web[o.a.c.h.Http11NioProtocol] Stopping ProtocolHandler ["http-nio-0.0.0.0-9000"]
2017.07.01 11:17:30 INFO web[o.a.c.h.Http11NioProtocol] Destroying ProtocolHandler ["http-nio-0.0.0.0-9000"]

解決辦法:
升級mysql版本到5.6或者5.6以上

登陸:http://192.168.3.199:9000/

賬號密碼預設都是admin

 

#到此sonar就安裝完成了,下一步將進入配置使用階段。

二:配置並使用sonar
2.1:安裝外掛部分:
2.1.1:預設的外掛目錄:

# ll /usr/local/sonarqube-5.6.6/extensions/plugins/

#如果線上安裝外掛不成功,可以把外掛下載後放在此目錄在重啟sonar服務也可以實現安裝外掛,jenkins也可以通過此方式安裝,另外jenkins還支援上傳外掛

2.1.2:安裝外掛:
administration-system-update center-available,在後面的搜尋框搜尋外掛名稱,然後點install安裝:


或在外掛目錄/usr/local/sonarqube/extensions/plugins執行

wget https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/download/sonar-l10n-zh-plugin-1.11/sonar-l10n-zh-plugin-1.11.jar(中文外掛:)然後重啟服務:

# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart

Sonar外掛安裝包下載地址:
https://sonarsource.bintray.com/Distribution/

[email protected] plugins]# pwd

/usr/local/sonarqube/extensions/plugins
[[email protected] plugins]# ll
total 12312
-rw-r--r-- 1 root root 128 Feb 16 18:19 README.txt
-rw-r--r-- 1 root root 4840602 Jul 4 17:05 sonar-java-plugin-4.11.0.10660.jar
-rw-r--r-- 1 root root 3733262 Jul 4 17:05 sonar-php-plugin-2.10.0.2087.jar
-rw-r--r-- 1 root root 4024311 Jul 4 17:05 sonar-python-plugin-1.8.0.1496.jar

2.1.3:程式碼檢測測試,把sonar-scanner和sonarqube關聯起來

sonar-scanner下載地址:https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/

cd /usr/local/src
wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.0.3.778-linux.zip
unzip sonar-scanner-2.6.1.zip
mv sonar-scanner-2.6.1 /usr/local/
cd /usr/local/
ln -s sonar-scanner-2.6.1 sonar-scanner

配置sonar-scanner:

[[email protected] conf]# vim /usr/local/sonar-scanner/conf/sonar-scanner.properties
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=sonar
[email protected]
sonar.jdbc.url=jdbc:mysql://192.168.3.12:3306/sonar?useUnicode=true&characterEncoding=utf8

檢測PHP專案,需要在sonar中先安裝SonarPHP外掛,github搜尋php-sonar-runner專案,對這個專案進行檢測
https://github.com/hasanyousuf/php-sonar-runner-unit-tests

unzip php-sonar-runner-unit-tests-master.zip
cd php-sonar-runner-unit-tests-master

[[email protected] php-sonar-runner-unit-tests-master]# pwd
/root/php-sonar-runner-unit-tests-master

# 直接在php目錄執行sonar-scanner,用於實現程式碼質量分析

[[email protected] php-sonar-runner-unit-tests-master]# /usr/local/sonar-scanner/bin/sonar-scanner

在sonar管理介面檢視掃描結果:
dashboard --> home 點專案名稱可以檢視更具體的資訊

2.1.4:程式碼規則:

2.4:如何讓jenkins關聯到sonar scanner?
有兩種方式儲存配置檔案,一是儲存在專案裡面,二是在jenkins管理介面進行配置:
2.4.1:在jenkins外掛安裝介面安裝SonarQuebe Scanner for Jenkins外掛:

2.4.2將jenkins關聯sonar:
jenkins中操作:系統管理-系統設定,找到 SonarQube servers 部分

新增sonar訪問地址,然後點儲存

2.4.3新增掃描器:
2.4.3.1:#系統管理–global-tool-ocnfigration --> 新增本地sonar scanner,然後點儲存

2.5:配置jenkins專案構建操作:

2.5.1:複製之前sonar scanner的程式碼檢測配置檔案內容,如:

# cat /root/php-sonar-runner-unit-tests-master/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

2.5.1:選擇自己的專案(web-demo)-構建觸發器-構建-execute sonarqube scanner,將配置檔案的內容修改成如下格式填寫完成後點儲存:

sonar.projectKey=web-demo
sonar.projectName=web-demo
sonar.projectVersion=1.0
sonar.sources=./
sonar.language=php
sonar.sourceEncoding=UTF-8

可以看到,右邊多了個快捷方式

2.6:測試jenkins專案構建:
2.6.1:在jenkins選擇自己的專案點選立即構建,以下是構建成功的介面:

2.6.2:在sonar檢視是否有程式碼掃質量分析結果:

2.7:新增構建後操作
2.7.1:新增郵件通知,當構建失敗後向指定的郵箱通知失敗資訊:
#發件箱設定:

2.7.2:將github服務關閉,然後構建專案,由於git服務無法訪問所以肯定會導致專案構建失敗觸發郵件通知:
2.7.2.1:關閉git服務:
# gitlab-ctl stop
2.7.2.2:構建專案,以下是構建失敗的控制檯輸出資訊:

2.7.2.3:以下是失敗的郵件通知: