本文在 Jenkins集成Gitlab、Pipeline实现自动化部署(高级篇) 一文基础进一步集成 SonarQube 搭建自动化代码质量管理平台,也是对
Jenkins集成SonarQube和Gitlab搭建自动化代码质量管理平台 一文的优化升级。
进入【系统管理】->【插件管理】,安装 SonarQube Scanner 插件:
登录后在浏览器地址后加restart:http://192.168.1.35:8000/restart
打开 “Disable the SCM Sensor”,关闭审查结果上传 SCM 功能:
进入【系统管理】->【系统配置】
勾选“Environment variables Enable injection of SonarQube server configuration …”选项,输入Name、Server URL:
注意:一定要勾选“Environment variables Enable injection of SonarQube server configuration …”,否则后面的步骤提示sonar授权失败!
Server authentication token 下点击“添加”,
注意:谷歌下点击【添加】无响应,更换为360浏览器、Edge可以
选中“Secret text”,输入第四步生成的 sonar token:
注意:类型选 “Secret text”
进入【系统管理】->【全局工具配置】,配置 SonarQube Scanner:
Jenkins 中配置流水线,脚本中增加 stage “sonar analysis”:
stage('sonar analysis') {
steps{
script {
scannerHome = tool 'sonarqube-scanner'
}
withSonarQubeEnv('sonar-server') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
项目下增加 sonar-project.properties 配置文件,内容如下:
sonar.projectKey=test-code-review
sonar.projectName=test-code-review
sonar.projectVersion=1.0
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
sonar.sourceEncoding=UTF-8
# sonar.java.binaries=**/target/classes
注意:配置文件一定要命名为 sonar-project.properties
提交代码后,Jenkins 控制台显示如下:
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (sonar analysis)
[Pipeline] script
[Pipeline] {
[Pipeline] tool
[Pipeline] }
[Pipeline] // script
[Pipeline] withSonarQubeEnv
Injecting SonarQube environment variables using the configuration: sonar-server
......
INFO: Analysis report generated in 214ms, dir size=93 KB
INFO: Analysis report compressed in 123ms, zip size=13 KB
INFO: Analysis report uploaded in 854ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://192.168.1.35:9200/dashboard?id=test-code-review
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://192.168.1.35:9200/api/ce/task?id=AX-tX4R3McYjyqt9nHOT
INFO: Analysis total time: 14.099 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 17.860s
INFO: Final Memory: 9M/40M
INFO: ------------------------------------------------------------------------