持續集成之代碼質量管理-Sonar

持續集成之代碼質量管理-Sonar

轉自持續集成之代碼質量管理-Sonar [三]

Sonar介紹

  Sonar 是一個用於代碼質量管理的開放平台。通過插件機制,Sonar 可以集成不同的測試工具,代碼分析工具,以及持續集成工具。與持續集成工具(例如 Hudson/Jenkins 等)不同,Sonar 並不是簡單地把不同的代碼檢查工具結果(例如 FindBugs,PMD 等)直接顯示在 Web 頁面上,而是通過不同的插件對這些結果進行再加工處理,通過量化的方式度量代碼質量的變化,從而可以方便地對不同規模和種類的工程進行代碼質量管理。

  在對其他工具的支持方面,Sonar 不僅提供了對 IDE 的支持,可以在 EclipseIntelliJ IDEA 這些工具里聯機查看結果;同時 Sonar 還對大量的持續集成工具提供了介面支持,可以很方便地在持續集成中使用 Sonar。

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

Sonar部署

  Sonar的相關下載和文檔可以在下面的鏈接中找到:sonarqube.org/downloads。需要注意最新版的Sonar需要至少JDK 1.8及以上版本。

 上篇文章我們已經可以成功的使用git進行拉去,Sonar的功能就是來檢查代碼是否有BUG。除了檢查代碼是否有bug還有其他的功能,比如說:你的代碼注釋率是多少,代碼有一些建議,編寫語法的建議。所以我們叫質量管理

Sonar還可以給代碼打分,並且引用了技術宅的功能(告訴你有很多地方沒改)

Sonar部署

[root@linux-node1 ~]# yum install -y java-1.8.0[root@linux-node1 ~]# cd /usr/local/src軟體包我們通過wget或者下載,rz上傳到伺服器#軟體包下載:https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.6.zip[root@linux-node1 src]# unzip sonarqube-5.6.zip[root@linux-node1 src]# mv sonarqube-5.6 /usr/local/[root@linux-node1 src]# ln -s /usr/local/sonarqube-5.6/ /usr/local/sonarqube

準備Sonar資料庫

如果沒有資料庫請執行yum install -y mariadb mariadb-server

[root@linux-node1 ~]# systemctl start mariadb[root@linux-node1 ~]# systemctl enable mariadbCreated symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.[root@linux-node1 ~]# mysql_secure_installation[root@linux-node1 ~]# mysql -uroot -p123456

特別提示:

sonar好像不支持mysql 5.5,所以如果看日誌出現以上error 請安裝mysql5.6 或者更高版本

blog.csdn.net/onothing1

執行sql語句

mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;mysql> GRANT ALL ON sonar.* TO sonar@localhost IDENTIFIED BY sonar@pw;mysql> GRANT ALL ON sonar.* TO sonar@% IDENTIFIED BY sonar@pw;mysql> FLUSH PRIVILEGES;

配置Sonar

[root@linux-node1 ~]# cd /usr/local/sonarqube/conf/[root@linux-node1 conf]# lssonar.properties wrapper.conf

編寫配置文件,修改資料庫配置

[root@linux-node1 conf]# vim sonar.properties#我們只需要去配置文件裡面修改資料庫的認證即可 14 sonar.jdbc.username=sonar #資料庫用戶 15 sonar.jdbc.password=sonar@pw #資料庫密碼 23 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&character Encoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

配置Java訪問資料庫驅動(可選)

  默認情況Sonar有自帶的嵌入的資料庫,那麼你如果使用類是Oracle資料庫,必須手動複製驅動類到${SONAR_HOME}/extensions/jdbc-driver/oracle/目錄下,其它支持的資料庫默認提供了驅動。其它資料庫的配置可以參考官方文檔:

docs.sonarqube.org/disp

啟動Sonar

  你可以在Sonar的配置文件來配置Sonar Web監聽的IP地址和埠,默認是9000埠。

[root@linux-node1 conf]# vim sonar.properties 99 #sonar.web.host=0.0.0.0106 #sonar.web.port=9000

啟動命令如下:

[root@linux-node1 ~]# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh startStarting SonarQube...Started SonarQube.

如果有什麼問題可以看一下日誌[/usr/local/sonarqube/logs/sonar.log]

檢查是否有相應的埠

[root@linux-node1 ~]# netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 2239/unicorn mastertcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 505/nginx: master ptcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 569/sshdtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 971/mastertcp 0 0 127.0.0.1:43163 0.0.0.0:* LISTEN 5205/javatcp 0 0 0.0.0.0:8060 0.0.0.0:* LISTEN 505/nginx: master ptcp 0 0 127.0.0.1:32000 0.0.0.0:* LISTEN 4925/javatcp 0 0 0.0.0.0:43044 0.0.0.0:* LISTEN 4952/javatcp 0 0 0.0.0.0:33350 0.0.0.0:* LISTEN 5205/javatcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 5011/javatcp 0 0 0.0.0.0:33385 0.0.0.0:* LISTEN 5011/javatcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN 4952/javatcp6 0 0 :::3306 :::* LISTEN 4658/mysqldtcp6 0 0 :::34993 :::* LISTEN 2348/javatcp6 0 0 :::8081 :::* LISTEN 2348/javatcp6 0 0 :::22 :::* LISTEN 569/sshdtcp6 0 0 ::1:25 :::* LISTEN 971/masterudp6 0 0 :::33848 :::* 2348/javaudp6 0 0 :::5353 :::* 2348/java

#埠是9000哦!

Web登陸:IP:9000

提示:

sonar跟jenkins類似,也是以插件為主

sonar安裝插件有2種方式:第一種將插件下載完存放在sonar的插件目錄,第二種使用web界面來使用安裝

存放插件路徑[/usr/local/sonarqube/extensions/plugins/]

安裝中文插件

登陸:用戶名:admin 密碼:admin

需要重啟才會生效

生效後如下圖:

我們在安裝一個php語言

溫馨提示:如果下載不下來我們直接去github進行下載,因為我們這個插件都是使用wget進行下載的

我們現在只能使用java的jar包和php,因為我們只安裝了java和php的語言插件。如果想使用Python的程序,就需要安裝Python的語言插件

Sonar 插件--->語言插件 (分析什麼語言,你就需要安裝什麼語言的插件)

Sonar通過SonarQube Scanner(掃描器)來對代碼進行分析

官方文檔:docs.sonarqube.org/disp

下載掃描器插件

[root@linux-node1 ~]# wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip[root@linux-node1 ~]# unzip sonar-scanner-2.8.zip[root@linux-node1 ~]# mv sonar-scanner-2.8 /usr/local/[root@linux-node1 ~]# ln -s /usr/local/sonar-scanner-2.8/ /usr/local/sonar-scanner

我們要將掃描器和sonar關聯起來

[root@linux-node1 ~]# cd /usr/local/sonar-scanner[root@linux-node1 sonar-scanner]# lsbin conf lib[root@linux-node1 sonar-scanner]# cd conf/[root@linux-node1 conf]# lssonar-scanner.properties[root@linux-node1 conf]# vim sonar-scanner.propertiessonar.host.url=http://localhost:9000 #sonar地址sonar.sourceEncoding=UTF-8 #字符集sonar.jdbc.username=sonar #資料庫賬號sonar.jdbc.password=sonar@pw #資料庫密碼sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8 #資料庫連接地址#打開注釋即可

我們現在需要找一個代碼進行分析。

sonar插件提供了一個代碼的庫

github:github.com/SonarSource/

我們下載軟體包:github.com/SonarSource/

解壓

[root@linux-node1 src]# unzip sonar-examples-master.zip[root@linux-node1 php]# cd sonar-examples-master/projects/languages/php[root@linux-node1 php]# cd php-sonar-runner-unit-tests/[root@linux-node1 php-sonar-runner-unit-tests]# lltotal 8-rw-r--r-- 1 root root 647 Dec 14 09:57 README.mddrwxr-xr-x 2 root root 51 Dec 14 09:57 reports-rw-r--r-- 1 root root 346 Dec 14 09:57 sonar-project.propertiesdrwxr-xr-x 3 root root 31 Dec 14 09:57 srcdrwxr-xr-x 2 root root 25 Dec 14 09:57 tests#這裡就是PHP的目錄

配置文件解釋:

如果你想讓我掃描,就需要在代碼路徑下放一個配置文件

[root@linux-node1 php-sonar-runner-unit-tests]# cat sonar-project.propertiessonar.projectKey=org.sonarqube:php-ut-sq-scanner #Keysonar.projectName=PHP :: PHPUnit :: SonarQube Scanner #這裡的名稱會顯示在一會的web界面上sonar.projectVersion=1.0 #版本,這裡的版本一會也會顯示在web界面上sonar.sources=src #軟體包存放路徑sonar.tests=testssonar.language=php #語言sonar.sourceEncoding=UTF-8 #字體# Reusing PHPUnit reportssonar.php.coverage.reportPath=reports/phpunit.coverage.xmlsonar.php.tests.reportPath=reports/phpunit.xml

#也就是說在項目裡面必須有這個配置文件才可以進行掃描

掃描

#提示:需要在項目文件裡面進行執行

[root@linux-node1 php-sonar-runner-unit-tests]# /usr/local/sonar-scanner/bin/sonar-scannerINFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.propertiesINFO: Project root configuration file: /usr/local/src/sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests/sonar-project.propertiesINFO: SonarQube Scanner 2.8INFO: Java 1.8.0_111 Oracle Corporation (64-bit)INFO: Linux 3.10.0-514.2.2.el7.x86_64 amd64INFO: User cache: /root/.sonar/cacheINFO: Load global repositoriesINFO: Load global repositories (done) | time=211msWARN: Property sonar.jdbc.url is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.WARN: Property sonar.jdbc.username is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.WARN: Property sonar.jdbc.password is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.INFO: User cache: /root/.sonar/cacheINFO: Load plugins indexINFO: Load plugins index (done) | time=3msINFO: Download sonar-csharp-plugin-5.0.jarINFO: Download sonar-java-plugin-3.13.1.jarINFO: Download sonar-l10n-zh-plugin-1.11.jarINFO: Plugin [l10nzh] defines l10nen as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.INFO: Download sonar-scm-git-plugin-1.2.jarINFO: Download sonar-php-plugin-2.9.1.1705.jarINFO: Download sonar-scm-svn-plugin-1.3.jarINFO: Download sonar-javascript-plugin-2.11.jarINFO: SonarQube server 5.6INFO: Default locale: "en_US", source code encoding: "UTF-8"INFO: Process project propertiesINFO: Load project repositories..................................................................................................

提示:我們什麼都不指定就會在當面目錄下掃描sonar-project.properties文件,根據配置文件進行掃描工作。掃描之後我們在web界面上就可以看到代碼的掃描結果

這裡的名字,版本 都是在sonar-project.properties文件中定義的

質量閾幫我們設定好一個閾值,超過相應的閾值就算有bug

為了讓jenkins可以在構建項目的時候執行sonar,所以我們需要在jenkins上安裝插件

現在就可以進行配置,讓jenkins和sonar結合在一起。這樣我們構建項目的時候就會進行代碼檢測

點擊保存

配置

編輯我們的項目,選擇最下放。找到構建

對PHP文件進行複製

[root@linux-node1 php-sonar-runner-unit-tests]# cat /usr/local/src/sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests/sonar-project.propertiessonar.projectKey=org.sonarqube:php-ut-sq-scannersonar.projectName=PHP :: PHPUnit :: SonarQube Scannersonar.projectVersion=1.0sonar.sources=srcsonar.tests=testssonar.language=phpsonar.sourceEncoding=UTF-8# Reusing PHPUnit reportssonar.php.coverage.reportPath=reports/phpunit.coverage.xmlsonar.php.tests.reportPath=reports/phpunit.xml

Analysis properties 分析的參數

填寫完畢後,我們點擊保存

我們選擇立即構建

提示:此時的SonarQube是無法點擊的

點擊Console Output可以查看構建輸出的內容

#提示:只要沒有error就可以

構建完成後,我們發現這裡的SonarQube可以點擊,我們點擊SonarQube就會鏈接到192.168.56.11:9000 就是代碼查看器的地址

現在我們已經做到了可以在git上進行拉取代碼。並進行檢測

我們還可以配置一個構建失敗發送郵箱:

在我們項目裡面設置構建後操作,選擇E-mail Notification

溫馨提示:使用163郵箱發送的通知被163伺服器退回了,因此我將設置在jenkins的郵箱改成了QQ郵箱

QQ:郵箱需要設置如下:

**1、需要開啟POPE3/SMTP服務

2、在jenkins上配置的密碼我們需要點擊生成授權碼進行使用**

QQ郵件默認會收到如下提示:

當再次構件成功時,郵件內容如下:


推薦閱讀:

TAG:科技 | 持續集成CI | 代碼質量 |