ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
### 理论知识 #### SonarQube 与 Jenkins 简 介 SonarQube 是一个开源的代码质量分析平台,便于管理代码的质量,可检查出项目代码的漏洞和潜在的逻辑问题。同时,它提供了丰富的插件,支持多种语言的检测, 如 Java、Python、Groovy、C、C++等几十种编程语言的检测。它主要的核心价值体现在如下几个方面: - 检查代码是否遵循编程标准:如命名规范,编写的规范等。 - 检查设计存在的潜在缺陷:SonarQube 通过插件 Findbugs、Checkstyle 等工具检测代码存在的缺陷。 - 检测代码的重复代码量:SonarQube 可以展示项目中存在大量复制粘贴的代码。 - 检测代码中注释的程度:源码注释过多或者太少都不好,影响程序的可读可理解性。 - 检测代码中包、类之间的关系:分析类之间的关系是否合理,复杂度情况。 ** SonarQube 平台是由4个部分组成:** - SonarQube Server - SonarQube Database - SonarQube Plugins - SonarQube Scanner 参考https://www.ibm.com/developerworks/cn/devops/1612_qusm_jenkins/index.html 感谢《IBM的曲世明和陈计云》 #### SonarQube Scanner原理 SonarQube Scanner,作为代码扫描的工具,通过它,将项目的代码读取并发送至SonarQube服务器中,才能让SonarQube进行代码分析。 可以认为SonarQube Scanner就是SonarQube的客户端。SonarQube Scanner很方便和不同类型的构建工具进行整合 参考http://aoyouzi.iteye.com/blog/2294992 感谢《aoyouzi》 #### 功能介绍(个人理解) - Jenkins - SonarQube Server(接收代码,进行分析) - SonarQube Scanner for Jenkins(Jenkins用来调用SonarQube Scanner的插件) - SonarQube Scanner(SonarQube的客户端,将读取的代码发送到SonarQube Server) ### SonarQube Server服务部署 #### 下载SonarQube Server(5.6.6版本) https://www.SonarQube.org/downloads/ #### 安装SonarQube Server 1. 部署Centos 7.3 x86_64 2. 部署JDK1.8(略) 3. 部署MysQL5.7(略) 4. 创建MySQL用户 ```shell create user 'sonar'@'192.168.0.231' identified by 'sonar2017'; CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; grant all privileges on sonar.* to 'sonar'@'192.168.0.231'; ``` 4. 安装SonarQube(以前叫sonar)上传sonar到/opt目录下 ```shell unzip SonarQube-5.6.6.zip mv SonarQube-5.6.6 /app/ ln -s SonarQube-5.6.6 SonarQube ``` 5.编辑配置文件(/app/SonarQube/conf/sonar.properties) ```shell #grep -vE '#|^.$' /app/SonarQube/conf/sonar.properties sonar.jdbc.username=sonar sonar.jdbc.password=sonar2017 sonar.jdbc.url=jdbc:mysql://192.168.0.231:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.web.host=192.168.0.231 sonar.web.context=/sonar sonar.web.port=9000 sonar.log.level=INFO sonar.path.logs=logs ``` 备注:sonar.web.context=/sonar 是访问地址后缀,配置后访问地址变为,http://192.168.0.231:9000/sonar #### SonarQube Server汉化(注意:不同版本,汉化包不同) [SonarQube 5.6.6中文包地址](https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/download/sonar-l10n-zh-plugin-1.15/sonar-l10n-zh-plugin-1.15.jar "Sonar 5.6.6中文包地址") 上传到/app/SonarQube/extensions/plugins #### 启动SonarQube - 启动 ```shell /app/SonarQube/bin/linux-x86-64/sonar.sh restart ``` - 看日志 ```shell tail -f /app/SonarQube/logs/sonar.log ``` - console模式 ```shell /app/SonarQube/bin/linux-x86-64/sonar.sh console ``` #### 登录,并修改密码,创建token(后面jenkins要用) - 登录地址:http://192.168.0.231:9000/sonar 默认用户名密码admin/admin - 修改管理员密码 login-->配置-->权限-->用户-->点击administraotr后面的锁图标 - 创建一个用户ding login-->配置-->权限-->用户-->token-->update token-->Generate Tokens(输入ding)-->generate 记录下token 参考文章:http://www.cnblogs.com/westfly/p/6098100.html 感谢《westfly》 ### Jenkins和SonarQube Server集成 #### 安装SonarQube Scanner for Jenkins插件 系统管理-->管理插件-->SonarQube Scanner for Jenkins-->安装 #### 配置SonarQube Server的连接 系统管理-->系统设置-->配置SonarQube servers ```shell Environment variables 勾选 SonarQube installations name:随便写 Server URL:http://192.168.0.231:9000/sonar Server version:5.3 or higher 注:5.1 or higher不支持token方式,如果写了数据库信息,在输出中会提示这个特性已被忽略,不建议使用。 Server authentication token:你懂的。 ``` #### 配置项目构建过程中的选项,增加SonarQube Scanner 系统管理-->Global Tool Configuration SonarQube Scanner for MSBuild(Microsoft 和 Visual Studio的生成系统,不配置) SonarQube Scanner(我们配置这个) ```shell name:随便填 自动安装:勾选 选择版本:最新(3.0.3.778) ``` #### 项目中配置SonarQube Scanner参数(读哪的代码、什么语言等) 项目-->增加构建步骤-->Execute SonarQube Scanner JDK:选jdk1.8(提前配置) Analysis Properties: ```shell sonar.language=java sonar.sources=$WORKSPACE/ sonar.projectName=ding sonar.projectKey=ding sonar.projectVersion=1.0 sonar.sourceEncoding=UTF-8 ``` ```shell sonar.language=java 开发语言(匹配Server上的语言规则) sonar.sources=$WORKSPACE/ RPM包安装后,位置是/var/lib/jenkins/workspace/ 项目都会下载到这里 sonar.projectName=ding 项目名称,回去里面读src sonar.projectKey=ding 唯一的项目Key,SonarQube Server上作为唯一标识,生成链接 sonar.projectVersion=1.0 版本:不太理解,强制要求 sonar.sourceEncoding=UTF-8 开发代码字符集 ``` 参考链接:https://docs.SonarQube.org/display/SCAN/Analyzing+with+SonarQube+Scanner ### 故障处理 #### 故障1:项目中配置SonarQube Scanner的Task to run为ding,就报这个错,去掉就可以了 ```shell 13:53:22.236 ERROR: Error during SonarQube Scanner execution Task 'ding' does not exist. Please use 'list' task to see all available tasks. 13:53:22.236 DEBUG: Execution getVersion 13:53:22.236 DEBUG: Execution stop ERROR: SonarQube scanner exited with non-zero code: 1 Sending e-mails to: dinghe@ding.com Finished: FAILURE ``` **解决** 去掉SonarQube Scanner的Task to run中的内容 #### 故障2: E170001 **报错** ```shell Caused by: org.tmatesoft.svn.core.SVNAuthenticationException: svn: E170001: Authentication required for '<http://192.168.0.250:80> VisualSVN Server' ``` **解决:** 登录SonarQube Server-->配置-->SCM-->设置为是(关闭SCM) #### 故障2:SonarQube安装完后出现SonarQube is under maintenance. Please check back later. ```shell SonarQube在安装完后运行:192.168.0.235:9000出现如下提示 SonarQube is under maintenance. Please check back later. Whilst waiting, you might want to check new plugins to extend the current functionality. If you are an administrator and have no idea why this message is showing, you should read the upgrade guide 这个需要安装完后先运行http://192.168.0.235:9000/setup 更新下 SonarQube ```