1. 程式人生 > >持續部署之jenkins與gitlab(三)

持續部署之jenkins與gitlab(三)

sonar簡介

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

sonar部署

    sonar稱之為程式碼質量管理,sonar的相關下載和文件可以在官網下載,官網地址:https://www.sonarqube.org/downloads/ 。需要特別注意最新版的Sonar需要jdk1.8至及以上版本及其資料庫版本在5.6以上(這個必須重視,不然啟動服務是報錯為資料庫連線失敗)。上篇文章我們已經可以成功的使用git進行從gitlab拉取程式碼,Sonar的功能就是來檢查程式碼是否有BUG,除此之外還有其他的功能,比如說:你的程式碼註釋率是多少,程式碼有一些建議,編寫語法的建議等。

1.編譯安裝資料庫5.6以上版本

cd    /opt 
tar xf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
cd    /usr/local/
ln -sv mysql-5.6.34-linux-glibc2.5-x86_64    mysql
useradd mysql -s /sbin/nologin -M 
mkdir /data ;chown -R mysql.mysql /data
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data --basedir=/usr/local/mysql 
cd /usr/local/mysql/;cp support-files/mysql.server /etc/init.d/mysqld ;cp /usr/local/mysql/my.cnf  /etc/my.cnf 
chmod +x /etc/init.d/myqld ;chkconfig mysqld on 
#編輯/etc/profile.d/新增mysql.sh  
export PATH=/usr/local/mysql/bin:$PATH   
#編輯/etc/my.cnf,指明datadir和port,及其啟動MySQL建立sonar使用者並授權sonar使用者
grant all on sonar.* to ‘sonar‘@‘localhost‘ identified by ‘pass‘; 
grant all on sonar.* to ‘sonar‘@‘10.1.10.%‘ identified by ‘pass‘; 
flush privileges;

2.下載解壓安裝sonar,在安裝之前需要配置jdk的環境,jdk的版本需要在1.8以上的版本

unzip sonarqube-5.6.6.zip 

ln -sv /usr/local/sonarqube-5.6.6  /usr/local/sonar   

編輯sonar配置檔案,開啟如下配置:/usr/local/sonar/conf/sonar.properties

sonar.jdbc.username=sonar   #連線資料庫使用者名稱
sonar.jdbc.password=chen    #連線資料庫時密碼 jdbc.url=jdbc:mysql://10.1.10.67則表示 
sonar.jdbc.url=jdbc:mysql://10.1.10.67:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.port=8800         #web介面的監聽埠

既然使用到MySQL資料庫則需連線MySQL資料庫,建立庫和授權使用者

create database sonar character set utf8 collate utf8_general_ci;
grant all on sonar.*  to ‘sonar‘@‘localhost‘ identified by ‘pass‘; 
grant all on sonar.*  to ‘soanr‘@‘10.1.10.%‘ identified by ‘pass‘; 
flush privileges;

啟動服務:/usr/local/sonar/bin/linux-x86_64/sonar.sh start #檢視sonar監聽的埠8800師傅開啟,及其MySQL伺服器中sonar庫中是否創建出多張表。。。

通過瀏覽器輸入IP地址進行訪問,預設情況下為英文,需要安裝外掛漢化,但是漢化的效果不是很好

預設登入sonar時,使用者名稱:admin 密碼:admin 

安裝中文外掛如下配置:

 

使用IP地址訪問如下:

 

如果安裝失敗可將外掛下載好後解壓至sonar預設存放外掛的目錄,重啟服務即可,安裝可掃描php/java/python的外掛。 

 

 sonar通過sonarqube scanner掃描器來對程式碼進行掃描分析

官方文件:

sonar-scanner安裝部署

unzip sonar-scanner-2.6.1.zip 

ln -sv /usr/local/sonar-scanner-2.6.1  /usr/local/sonar-scanner  

cd /usr/local/sonar-scanner/ 

grep "^[[:alpha:]]" sonar-scanner.properites

sonar.host.url=  #sonar地址 
sonar.sourceEncoding=UTF-8 #設定字符集
sonar.jdbc.password=chen   #sonar資料庫密碼    #如下為連線資料庫地址
sonar.jdbc.url=jdbc:mysql://10.1.10.67:3306/sonar?useUnicode=true&characterEncoding=utf8

掃描時需要在原始碼目錄中有掃描配置檔案及其原始碼

[[email protected] php-sonar-runner]# ls
README.md  sonar-project.properties  src  validation.txt
[[email protected] php-sonar-runner]# cat sonar-project.properties
# Required metadata
sonar.projectKey=org.sonarqube:php-simple-sq-scanner #sonar專案key名稱
sonar.projectName=PHP ::    #sonar專案名稱
Simple Project :: SonarQube Scanner 
sonar.projectVersion=2.0  #版本號
# Comma-separated paths to directories with sources (required)
sonar.sources=src #原始碼路徑
# Language
sonar.language=php #掃描語言
# Encoding of the source files
sonar.sourceEncoding=UTF-8  #字元編碼
[[email protected] php-sonar-runner]# /usr/local/sonar-scanner/bin/sonar-scanner #再次執行sonar-scanner
INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /home/mageedu/sonar-examples-master/projects/languages/php/php-sonar-runner/sonar-project.properties
INFO: SonarQube Scanner 2.6.1
INFO: Java 1.8.0_111 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-327.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Load global repositories
INFO: Load global repositories (done) | time=1777ms
WARN: 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/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=156ms
INFO: Plugin [l10nzh] defines ‘l10nen‘ as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
INFO: SonarQube server 5.6.6
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Process project properties
INFO: Load project repositories
INFO: Load project repositories (done) | time=832ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=1532ms
INFO: Load active rules
INFO: Load active rules (done) | time=6049ms
WARN: SCM provider autodetection failed. No SCM provider claims to support thisproject. Please use sonar.scm.provider to define SCM of your project.
INFO: Publish mode
INFO: -------------  Scan PHP :: Simple Project :: SonarQube Scanner
INFO: Language is forced to php
INFO: Load server rules
INFO: Load server rules (done) | time=4650ms
INFO: Base dir: /home/mageedu/sonar-examples-master/projects/languages/php/php-sonar-runner
INFO: Working dir: /home/mageedu/sonar-examples-master/projects/languages/php/php-sonar-runner/.sonar
INFO: Source paths: src
INFO: Source encoding: UTF-8, default locale: en_US
INFO: Index files
INFO: 0 files indexed
INFO: Quality profile for php: Sonar way
INFO: Sensor Lines Sensor
INFO: Sensor Lines Sensor (done) | time=1ms
INFO: Sensor SCM Sensor
INFO: No SCM system was detected. You can use the ‘sonar.scm.provider‘ propertyto explicitly specify it.
INFO: Sensor SCM Sensor (done) | time=0ms
INFO: Sensor Analyzer for "php.ini" files
INFO: Sensor Analyzer for "php.ini" files (done) | time=9ms
INFO: Sensor SonarJavaXmlFileSensor
INFO: Sensor SonarJavaXmlFileSensor (done) | time=0ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=1ms
INFO: Sensor Code Colorizer Sensor
INFO: Sensor Code Colorizer Sensor (done) | time=0ms
INFO: Sensor CPD Block Indexer
INFO: DefaultCpdBlockIndexer is used for php
INFO: Sensor CPD Block Indexer (done) | time=14ms
INFO: Calculating CPD for 0 files
INFO: CPD calculation finished
INFO: Analysis report generated in 337ms, dir size=8 KB
INFO: Analysis reports compressed in 63ms, zip size=3 KB
INFO: Analysis report uploaded in 643ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://10.1.10.12:8888/dashboard/index/org.sonarqube:php-simple-sq-scanner
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://10.1.10.12:8888/api/ce/task?id=AVrrR_tSXasaRrmKgvaL
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 32.914s
INFO: Final Memory: 40M/103M
INFO: ------------------------------------------------------------------------
[[email protected] php-sonar-runner]#

掃描後的結果會在web介面中給與顯示掃描結果

 

Jenkins結合sonar-scanner自動進行程式碼掃描

掃描之前需安裝sonarqube plugin外掛

系統管理-->管理外掛-->搜尋sonarqub plugin外掛安裝即可 

 

Jenkins結合sonar則需在系統管理-->系統設定-->sonar配置段進行配置,輸入name、server URL、server version即可

 

系統管理-->Global tools configuration找到sonarqube填寫sonar的安裝路徑及其name   

 

全域性配置段配置完成之後則在接下來的專案中即可繼承全集配置段中的配置,在需要掃描的專案中嵌入,點開專案-->設定-->構建-->execute sonarqube scanner 將掃描引數填寫在文字框中

 

完成配置,點選構建,即可自動進行程式碼掃描測試

 

構建成功顯示為藍色的球,在旁邊會顯示sonar的圖示,點選圖示可檢視程式碼的質量展示資訊

 

構建完成後,我們發現這裡的SonarQube可以點選,我們點選SonarQube就會連結到10.1.10.65:8800就是程式碼檢視器的地址 

 

同時,可以在專案設定中配置構建後操作,例如構建失敗可定義郵件通知等

 

構建失敗後則會郵件提醒