安裝Sonar
Sonar是一個用於代碼質量管理的開源平台,用於管理Java源代碼的質量。通過插件機制,Sonar 可以集成不同的測試工具,代碼分析工具,以及持續集成工具,比如pmd-cpd、checkstyle、findbugs、Jenkins。通過不同的插件對這些結果進行再加工處理,通過量化的方式度量代碼質量的變化,從而可以方便地對不同規模和種類的工程進行代碼質量管理。同時 Sonar 還對大量的持續集成工具提供了接口支持,可以很方便地在持續集成中使用 Sonar。 此外,Sonar 的插件還可以對 Java 以外的其他編程語言提供支持,對國際化以及報告文檔化也有良好的支持。
這里使用docker安裝sonar. 要求宿主機器安裝了docker和docker-compose
docker-compose.yaml
version: "3"
services:
sonarqube:
image: sonarqube:7.7-community
ports:
- "9000:9000"
networks:
- sonarnet
environment:
- SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
volumes:
- sonarqube_conf:/opt/sonarqube/conf
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins
db:
image: postgres:11-alpine
networks:
- sonarnet
environment:
- POSTGRES_USER=sonar
- POSTGRES_PASSWORD=sonar
volumes:
- postgresql_data:/var/lib/postgresql/data
networks:
sonarnet:
driver: bridge
volumes:
sonarqube_conf:
sonarqube_data:
sonarqube_extensions:
sonarqube_bundled-plugins:
postgresql_data:
sonar的配置文件會掛載出來,目錄類似/var/lib/docker/volumes/sonarqube_sonarqube_conf/_data
.
修改sonar.properties,以添加自定義配置,比如sso,比如ldap
命令行執行啟動
docker-compose up -d
瀏覽器訪問localhost:9000
Jenkins配置sonar
安裝sonar插件 SonarQube Scanner for Jenkins
然后
Jenkins 系統配置SonarQube servers
name: SonarQube
Jenkins 全局工具配置Sonar Scanner
name: SonarQube Scanner
Maven Jenkins Job配置
如果選擇構建一個maven類型的Jenkins Job, 需要在構建后添加步驟執行sonar scan
添加post step, 需要指定sonar-project.properties配置文件
sonar-project.properties會指定項目的分組key。Sonar通過分組key前綴可以設置分組權限。比如
# must be unique in a given SonarQube instance
# 項目的唯一性id,使用分組標識做為前綴,比如項目屬於部門my
sonar.projectKey=my:demo
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=my:demo
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
# 要掃描的模塊位置,如果是根目錄,就是.
sonar.sources=web-api
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
# 這里需要指定編譯后jar所在目錄
sonar.java.binaries=**/target/classes
然后,點擊構建即可在sonar上看到結果。
Pipeline Jenkins Job配置
如果采用Pipeline作為Jenkins Job, 需要修改Jenkinsfile的步驟。
參考官方文檔
添加兩個stage
stage('SonarQube analysis') {
def sonarqubeScannerHome = tool name: 'SonarQube Scanner'
withSonarQubeEnv('SonarQube') {
//這里project_module是模塊所在路徑,目的是指定配置文件
sh "${sonarqubeScannerHome}/bin/sonar-scanner -Dproject.settings=./${project_module}/sonar-project.properties"
}
}
// No need to occupy a node
stage("Quality Gate"){
//最多等待1min,如果掃描沒通過,超時失敗
timeout(time: 1, unit: 'MINUTES') { // Just in case something goes wrong, pipeline will be killed after a timeout
def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
由於等待sonar掃描結果需要回調webhook, 因此,需要在sonar配置Jenkins的webhook回調地址。
假設Jenkins地址為: http://jenkins.demo.com
打開sonar-administration-configuration-webhooks, 添加一個webhook
http://jenkins.demo.com/sonarqube-webhook/
這樣掃描結果會通知Jenkins.
Sonar權限管理
Sonar設置權限有admin和普通用戶以及分組和創建者。我們默認創建者和admin有所有權限,
接下來就是我們關注的分組權限。百度大部分文章都沒提到,只是到sonar掃描就結果了。而一個代碼質量檢測平台肯定是給人用的,就必須設置權限問題。我們希望,不同部門的人只能看到部門自己的代碼結果。所以代碼必須和組進行關聯。
集成ldap可以直接使用公司現有的組織架構,也可以手動添加group,然后group添加人。
接下來,如何綁定項目給指定group? 當然可以直接手動設置,但公司那么多項目,肯定需要配置化自動綁定。Sonar提供了permission template來實現這個功能。參見官網
use the "Create" button on Administration > Security >** Permission Templates**. It is possible to provide a Project key pattern.
這里有個問題,就是Project Key Pattern是正則表達式,所以,針對我們前面demo,這個內容應是
my.*
先要配置好permission template, 設置哪些組可以有哪些權限,比如設置組my
可以訪問所有my
為前綴的項目。 然后進行代碼掃描,生成的項目就會綁定這個模板對應的權限了。
Sonar quality Gate通過閾值設置
Sonar通過quality gate規則來決定掃描是否通過,指標有很多種,比如設定bug不能超過10個, 當掃描結果bug大於10就會失敗。