目錄
- SonarQube的架構和集成
- 部署 SonarQube
SonarQube的架構和集成
架構
SonarQube 做代碼審查福稳,主要包括兩部分:SonarQube Server 和 SonarQube Plugins,其中 SonarQube Server 主要是用來處理代碼分析報告并把結果存儲到 SonarQube Database 中娄帖,其中的分析報告來自構建或者持續(xù)繼承環(huán)境的 SonarQube Scanners 的掃描結果。
集成
部署 SonarQube 服務
SonarQube的部署分兩步
- 安裝 SonarQube Server
- 安裝 Plugin
安裝 SonarQube Server
環(huán)境要求
- CentOS
- JDK 8
- MySQL 5.6 | 5.7
- UTF8 charset
- InnoDB storage engine
- Chrome | IE 11 | Firefox
安裝配置數(shù)據(jù)庫
安裝MySQL數(shù)據(jù)庫喊废,并創(chuàng)建所需數(shù)據(jù)庫
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
mysql> FLUSH PRIVILEGES;
安裝SonarCube(CentOS服務器)
//下載 SonarQube 5.6 L.T.S.
$ sudo wget -O /opt/sonarqube-5.6.4.zip https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.6.4.zip
// 解壓 sonarqube
$ sudo cd /opt/ && unzip /sonarqube-5.6.4.zip
// 重命名目錄
$ sudo mv sonarqube-5.6.4 sonar
配置數(shù)據(jù)庫連接
編輯配置文件 /opt/sonar/conf/sonar.properties
# DATABASE
#
# IMPORTANT: the embedded H2 database is used by default. It is recommended for tests but not for
# production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.
# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
#----- MySQL 5.6 or greater
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
修改服務WEB配置(可選)
編輯配置文件 /opt/sonar/conf/sonar.properties
sonar.web.host=0.0.0.0 // 默認所有IP可訪問
sonar.web.port=9000 // 默認端口
sonar.web.context= // 默認上下文 /
配置防火墻
// 添加8080端口(Jenkins默認服務端口)到防火墻
$ sudo firewall-cmd --permanent --add-port=9000/tcp
$ sudo firewall-cmd --reload
啟動服務
$ sudo cd /opt/sonar/bin/linux-x86-64
$ sudo ./sonar.sh start
瀏覽器訪問 SonarQube
- 用戶名/密碼:admin/admin
- 訪問地址:http://10.10.4.171:9000
安裝插件
在線安裝
進入菜單:Administration - System - Update Center
Paste_Image.png
Paste_Image.png
建議安裝的插件清單:
- Chinese Pack: 漢化插件
- SonarQube Scanner: Launch analysis from the command line
- SonarQube Scanner for MSBuild: Launch analysis of .Net projects
- SonarQube Scanner for Ant: Launch analysis from Ant
- SonarQube Scanner for Maven: Launch analysis from Maven with minimal configuration
- SonarQube Scanner for Gradle: Launch Gradle analysis
- SonarQube Scanner For Jenkins: Launch analysis from Jenkins
- Checkstyle
離線安裝
下載插件:
http://docs.sonarqube.org/display/PLUG/SonarPython
拷貝插件到目錄:
$SONARQUBE_HOME/extensions/plugins
重啟 SonarQube
生成 Token
瀏覽器訪問 SonarQube沃测,http://sonar.demo.com:9000/users
Paste_Image.png
點擊更新 Token,輸入名稱肮疗,生成Token爬泥,后面在Gradle構建腳本中集成代碼審查時會用到
Paste_Image.png
FAQ
- SonarQube 啟動報錯:Unsupported major.minor version 52.0
【解決辦法】
下載使用最新的JDK
http://download.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.tar.gz
- SonarQuber 啟動報錯:Cannot create PoolableConnectionFactory (Table 'performance_schema.session_variables' doesn't exist)
【解決辦法】
在數(shù)據(jù)庫節(jié)點上執(zhí)行已下兩個命令:
# mysql_upgrade -u root -p --force
# systemctl restart mysqld