sonar是一款靜態代碼質量分析工具,支持包括JAVA、JS、CSS等20多種語言,可以集成到IDE、Jenkins等服務中,方便查看代碼質量分析報告。javascript
centos 7 docker 19.0 + docker-compose 1.26 +
version: "3" services: sonarqube: image: sonarqube:7.4-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.1 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:
docker-compose up -d
查看插件目錄php
# 查看插件目錄命令 docker volume inspect sonarqube_sonarqube_extensions # 返回 [ { "CreatedAt": "2020-06-28T11:21:57+08:00", "Driver": "local", "Labels": { "com.docker.compose.project": "sonarqube", "com.docker.compose.version": "1.24.1", "com.docker.compose.volume": "sonarqube_extensions" }, "Mountpoint": "/data/docker/volumes/sonarqube_sonarqube_extensions/_data", "Name": "sonarqube_sonarqube_extensions", "Options": null, "Scope": "local" } ] # Mountpoint下目錄 /data/docker/volumes/sonarqube_sonarqube_extensions/_data/plugins
上傳插件css
# plugins . ├── README.txt ├── sonar-csharp-plugin-7.7.0.7192.jar ├── sonar-css-plugin-1.0.2.611.jar ├── sonar-flex-plugin-2.4.0.1222.jar ├── sonar-go-plugin-1.1.0.1612.jar ├── sonar-jacoco-plugin-1.0.1.143.jar ├── sonar-java-plugin-5.8.0.15699.jar ├── sonar-javascript-plugin-5.0.0.6962.jar ├── sonar-kotlin-plugin-1.2.1.2009.jar ├── sonar-l10n-zh-plugin-1.19.jar ├── sonar-ldap-plugin-2.2.0.608.jar ├── sonar-php-plugin-2.14.0.3569.jar ├── sonar-python-plugin-1.10.0.2131.jar ├── sonar-ruby-plugin-1.2.1.2009.jar ├── sonar-scm-git-plugin-1.6.0.1349.jar ├── sonar-scm-svn-plugin-1.9.0.1295.jar ├── sonar-typescript-plugin-1.8.0.3332.jar ├── sonar-vbnet-plugin-7.7.0.7192.jar └── sonar-xml-plugin-1.5.1.1452.jar
查看配置文件目錄java
# 查看配置文件目錄 docker volume inspect sonarqube_sonarqube_conf # 返回信息 [ { "CreatedAt": "2020-06-28T13:37:30+08:00", "Driver": "local", "Labels": { "com.docker.compose.project": "sonarqube", "com.docker.compose.version": "1.24.1", "com.docker.compose.volume": "sonarqube_conf" }, "Mountpoint": "/data/docker/volumes/sonarqube_sonarqube_conf/_data", "Name": "sonarqube_sonarqube_conf", "Options": null, "Scope": "local" } ] # Mountpoint下目錄 /data/docker/volumes/sonarqube_sonarqube_conf/_data
配置文件python
vim /data/docker/volumes/sonarqube_sonarqube_conf/_data/sonar.properties sonar.web.context=/sonar sonar.web.port=9000
web訪問git
http://localhost:9000/sonar
# 檢查結果通知 配置- 配置 – webhooks # 建立 # 名稱 jenkins # URL http://jenkins:8080/sonarqube-webhook/ #備註: #Jenkins服務器地址
插件安裝github
SonarQube Scanner插件安裝
系統配置web
# 系統管理 – 系統配置 – SonarQube servers # Name sonar # Server URL http://sonar:9000/sonar/ # 備註: sonar服務器地址 # Server authentication token 選擇sonar認證
maven-global-settings配置sql
# 系統設置-文件管理 配置全局maven源 # 文件ID maven-global-settings
流水線步驟docker
stage('Sonar階段') { steps{ configFileProvider([configFile(fileId: 'maven-global-settings', variable: 'MAVEN_GLOBAL_ENV')]) { withSonarQubeEnv('sonar') { echo "開始進行代碼審計" sh """mvn -s $MAVEN_GLOBAL_ENV sonar:sonar \ -Dsonar.projectKey=${APPLICATION_NAME} \ -Dsonar.projectName=${APPLICATION_NAME} \ -Dsonar.java.coveragePlugin=jacoco \ -Dsonar.surefire.reportsPath=target/surefire-reports \ -Dsonar.analysis.mode= \ -Dsonar.java.binaries=target/sonar """ } script { //設置超時時間1分鐘 timeout(1) { //利用sonar webhook功能通知pipeline代碼檢測結果,未經過質量閾,pipeline將會fail def qg = waitForQualityGate('sonar') if (qg.status != 'OK') { error "未經過Sonarqube的代碼質量閾檢查,請及時修改!failure: ${qg.status}" } } } } } }
新增
# 質量閾 – 建立 # 名稱 test # 條件 度量 Bugs > 0 # 設爲默認
上傳插件
# https://github.com/jensgerdes/sonar-pmd/releases sonar-pmd-plugin-3.3.0.jar
激活規則
# 新增質量配置 # 激活p3c規則 # 應用規則 # 設置成默認配置