持續集成之②:整合jenkins與代碼質量管理平臺Sonar並實現構建失敗郵件通知php
一:Sonar是什麼?
Sonar 是一個用於代碼質量管理的開放平臺,經過插件機制,Sonar 能夠集成不一樣的測試工具,代碼分析工具,以及持續集成工具。與持續集成工具(例如 Hudson/Jenkins 等)不一樣,Sonar 並非簡單地把不一樣的代碼檢查工具結果(例如FindBugs,PMD等)直接顯示在Web頁面上,而是經過不一樣的插件對這些結果進行再加工處理,經過量化的方式度量代碼質量的變化,從而能夠方便地對不一樣規模和種類的工程進行代碼質量管理。在對其餘工具的支持方面,Sonar 不只提供了對 IDE 的支持,能夠在 Eclipse 和 IntelliJ IDEA 這些工具裏聯機查看結果;同時Sonar還對大量的持續集成工具提供了接口支持,能夠很方便地在持續集成中使用 Sonar,此外,Sonar 的插件還能夠對 Java 之外的其餘編程語言提供支持,對國際化以及報告文檔化也有良好的支持。java
#官方網站:http://www.sonarqube.org/node
Sonar部署
Sonar的相關下載和文檔能夠在下面的連接中找到:http://www.sonarqube.org/downloads/。須要注意最新版的Sonar須要至少JDK 1.8及以上版本python
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
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar@341Jpw'; mysql> FLUSH PRIVILEGES;
配置Sonarlinux
# cd /usr/local/sonarqube/conf/
修改配置文件的數據庫配置git
# egrep '^[a-Z]' sonar.properties sonar.jdbc.username=sonar sonar.jdbc.password=sonar@pw sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
啓動Sonar
你能夠在Sonar的配置文件來配置Sonar Web監聽的IP地址和端口,默認是9000端口。github
# vim sonar.properties sonar.web.host=0.0.0.0 sonar.web.port=9000
# 啓動有建立表和其餘操做,速度會有點慢web
[root@node1 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以上sql
登錄: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/
主要的是sonar對代碼的分析是經過插件完成的,即分析java代碼要安裝java插件,分析php代碼要安裝php插件,分析什麼語言就安裝什麼語言的插件
本次咱們安裝了php、python、java語法檢測插件
root@node1 plugins]# pwd
/usr/local/sonarqube/extensions/plugins [root@node1 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:
[root@node1 conf]# vim /usr/local/sonar-scanner/conf/sonar-scanner.properties sonar.host.url=http://localhost:9000 sonar.sourceEncoding=UTF-8 sonar.jdbc.username=sonar sonar.jdbc.password=sonar@341Jpw 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 [root@node1 php-sonar-runner-unit-tests-master]# pwd /root/php-sonar-runner-unit-tests-master
# 直接在php目錄運行sonar-scanner,用於實現代碼質量分析
[root@node1 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:如下是失敗的郵件通知: