jenkins代码管理sonar
Jenkins搭建与gitlab代理自动构建请参考https://blog.csdn.net/weixin_45636702/article/details/103097104
Sonar 介绍:
Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工 具,代码分析工具,以及持续集成工具。与持续集成工具(例如 Hudson/Jenkins 等)不同,Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,而 是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方 便地对不同规模和种类的工程进行代码质量管理
在对其他工具的支持方面,Sonar 不仅提供了对 IDE 的支持,可以在 Eclipse 和 IntelliJ IDEA 这些工具里联机查看结果;同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方 便地在持续集成中使用 Sonar
此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化 也有良好的支持
Sonar 部署:
上篇文章我们已经可以成功的使用 git 进行拉去,Sonar 的功能就是来检查代码是否有 BUG。除了 检查代码是否有 bug 还有其他的功能,比如说:你的代码注释率是多少,代码有一些建议,编写语 法的建议。所以我们叫质量管理
Sonar 还可以给代码打分,并且引用了技术宅的功能(告诉你有很多地方没改)
安装sonar:
[root@localhost src]# unzip sonarqube-5.6.zip
[root@localhost src]# mv sonarqube-5.6 /usr/local/sonarqube
[root@localhost src]# ln -s /usr/local/sonarqube/bin/linux-x86-64/sonar.sh /usr/local/bin/
安装sonar数据库:(需要5.6版本或者更高)
[root@localhost src]# yum localinstall mysql-community-*
查看mysql密码:
[root@localhost src]# systemctl start mysqld
[root@localhost src]# grep password /var/log/mysqld.log
2019-11-18T01:32:05.900308Z 1 [Note] A temporary password is generated for root@localhost: +ApeUqPbA41t
[root@localhost src]# mysql -u root -p+ApeUqPbA41t
修改mysql密码,创建数据库,授权:
mysql> alter user 'root'@'localhost' identified by '[email protected]';
mysql> create database sonar character set utf8 collate utf8_general_ci;
mysql> grant all on sonar.* to 'sonar'@'%' identified by '[email protected]';
mysql> flush privileges;
修改sonar的配置文件:
[root@localhost src]# cd /usr/local/sonarqube/conf/
[root@localhost conf]# vim sonar.properties
修改:
sonar.jdbc.username=sonar //数据库用户名
[email protected] //数据库密码
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonaruseUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.host=0.0.0.0 //web监听地址
sonar.web.port=9000 //运行端口
启动:
[root@localhost conf]# sonar.sh start
Starting SonarQube...
Started SonarQube.
[root@localhost conf]# netstat -anput | grep 9000
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 12401/java
现在登录web界面是英文的:
浏览器访问:192.168.1.1.0:9000
sonar 跟 jenkins 类似,也是以插件为主
sonar 安装插件有 2 种方式:
第一种将插件下载完存放在 sonar 的插件目录
第二种使用 web 界面 来使用安装
现在使用第一种:
[root@localhost plugins]# cd /usr/local/sonarqube/extensions/plugins/ 插件存放路径
[root@localhost plugins]# mv /usr/src/sonar-l10n-zh-plugin-1.11.jar ./
[root@localhost plugins]# mv /usr/src/sonar-php-plugin-2.9-RC1.jar ./
重启sonar就行:
[root@localhost plugins]# sonar.sh restart
点击右上角登录:
默认账户名密码都是admin:
代码分析,sonar扫描:
安装sonar-scanner
[root@localhost src]# unzip sonar-scanner-cli-3.3.0.1492-linux.zip
[root@localhost src]# mv sonar-scanner-3.3.0.1492-linux/ /usr/local/sonar-scanner
[root@localhost src]# ln -s /usr/local/sonar-scanner//bin/sonar-scanner /usr/local/bin/
[root@localhost src]# ln -s /usr/local/sonar-scanner/bin/sonar-scanner /usr/bin/
将安装好的扫描仪关联到sonarqube:
[root@localhost src]# cd /usr/local/sonar-scanner/conf/
[root@localhost conf]# vim sonar-scanner.properties
sonar.host.url=http://localhost:9000 //去掉注释
sonar.jdbc.username=sonar
[email protected]
sonar.sourceEncoding=UTF-8 //去掉注释
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance //这一行和sonarqube的配置文件中jdbc.url相同
配置完成后下载测试的代码包:
[root@localhost src]# unzip testalyzer-master.zip
[root@localhost src]# cd testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests/
[root@localhost php-sonar-runner-unit-tests]# ls
README.md reports sonar-project.properties src tests
[root@localhost php-sonar-runner-unit-tests]# cat sonar-project.properties
sonar.projectKey=org.sonarqube:php-ut-sq-scanner //密钥
sonar.projectName=PHP :: PHPUnit :: SonarQube Scanner //web界面显示名称
sonar.projectVersion=1.0 //版本
sonar.sources=src //软件包存放路径
sonar.tests=tests //测试路径
sonar.language=php //语言
sonar.sourceEncoding=UTF-8 //编码格式
# Reusing PHPUnit reports
sonar.php.coverage.reportPath=reports/phpunit.coverage.xml
sonar.php.tests.reportPath=reports/phpunit.xml
也就是说在项目里面必须有这个配置文件才可以进行扫描
进行扫描并在web界面查看:
提示:需要在项目文件里面进行执行
我们什么都不指定就会在当面目录下扫描 sonar-project.properties 文件,根据配置文件进 行扫描工作
[root@localhost php-sonar-runner-unit-tests]# sonar-scanner //扫描php
这里的名字,版本 都是在 sonar-project.properties 文件中定义的
质量阈帮我们设定好一个阈值,超过相应的阈值就算有 bug
[root@localhost src]# cd testalyzer-master/projects/languages/javascript/javascript-sonar-runner[root@localhost javascript-sonar-runner]# ls
README.md sonar-project.properties src validation.txt
[root@localhost javascript-sonar-runner]# sonar-scanner //扫描java
sonar与Jenkins进行关联:
为了让 jenkins 可以在构建项目的时候执行 sonar,所以我们需要在 jenkins 上安装插件
上传插件:
系统管理----插件管理----高级---上传插件
系统管理----系统设置----add sonarqube server----name 随意写----url sonarqube的路径:http:192.168.1.10:9000
系统管理----全局工具配置---新增SonarQube Scanner----取消勾选自动安装----name 随意些写----SONAR_RUNNER_HOME:/usr/local/sonar-scanner/ ##这个是sonar-scanner的安装路径
配置上一次的web-demo项目---构建----Exexuter SonarQube Scabber----Analysis proerties 内容如下:
sonar.projectKey=web-demo
sonar.projectName=web-demo
sonar.projectVersion=1.0
sonar.sources=src
sonar.tests=tests
sonar.language=php
sonar.sourceEncoding=UTF-8
sonar.php.coverage.reportPath=reports/phpunit.coverage.xml
sonar.php.test.reportPath=reports/phpunit.xml
回到终端:
[root@localhost src]# cd
[root@localhost ~]# cd /usr/src/testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests/
[root@localhost php-sonar-runner-unit-tests]# cp -r * /root/web-demo/
[root@localhost php-sonar-runner-unit-tests]# cd /root/web-demo/
[root@localhost web-demo]# git add *
[root@localhost web-demo]# git commit -m "test sonar"
[root@localhost web-demo]# git push origin master
构建完成后,点击 SonarQube 就会链接到 192.168.1.10:9000 就是代码查看器的地址
现在我们已经做到了可以在 git 上进行拉取代码。并进行检测
Jenkins邮件告警:
首先生成qq的SMTP的授权码:
登录qq邮箱---设置---帐户----POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务---开启POP3/SMTP服务 :
系统管理---系统设置---Jenkins Location----系统管理员邮件地址
邮件通知---SMTP服务器:smtp.qq.cpm
用户默认邮件后缀:@qq.com
勾选使用smtp认证------用户名:邮箱账号-----密码:刚刚生成的授权码
候选使用ssl
smtp端口:465
勾选通过发送邮件测试配置
配置web-demo项目---构建后操作---添加E-mail Notification---Recipient:收件人地址
----Editable Email Notification----Project From填写项目名称—Project Recipient List:收件人地址----点击Advanced Settings------Triggers----add Triggers----Always----Send To只保留Recipient List
模仿构建失败:
关闭gitlab-----点击立即构建:
[root@localhost ~]# gitlab-ctl stop
然后启动gitlab:
[root@localhost ~]# gitlab-ctl start
再次构建测试:
点击这个连接就会跳转到构建的这个项目: