devops系列二:windows下用Quarqube安装及Jenkins集成

前言

由于开发人员不断增加和任务的繁重而忽略代码质量,最终会导致bug的增多和代码债务的堆积,而公司又没有专人去做review,所以现要引入自动review代码工具:SonarQube

本文主要阐述安装及集成Jenkins分析.net代码遇到的问题

安装

1.安装准备

jdk:1.8

Mysql:4.7

SonarQube :6.7.7 下载地址: https://www.sonarqube.org/downloads/

2.安装SonarQube

2.1配置数据库

create database sonar default charset=utf8; 
create user 'sonar' identified by '123456';
grant all on sonar.* to 'sonar'@'%' identified by '123456';
grant all on sonar.* to 'sonar'@'localhost' identified by '123456';
flush privileges;

2.2 修改 sonarque配置

1.解压文件到对应的安装目录中去

2.\sonarqube-6.7.7\sonarqube-6.7.7\conf中sonar.properties中修改mysql 的连接及默认用户名密码

sonar.jdbc.url=jdbc:mysql://localhost:3356/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.username=sonar
sonar.jdbc.password=123456
sonar.login=admin
sonar.password=admin

3.\sonarqube-6.7.7\sonarqube-6.7.7\bin\windows-x86-64

对应目录中以管理员身份运行启动

4.如果在日志es.log中出现

org.elasticsearch.transport.BindTransportException: Failed to bind to [9001]
	at org.elasticsearch.transport.TcpTransport.bindToPort(TcpTransport.java:780) ~[elasticsearch-5.6.3.jar:5.6.3]
	at org.elasticsearch.transport.TcpTransport.bindServer(TcpTransport.java:745) ~[elasticsearch-5.6.3.jar:5.6.3]
	at org.elasticsearch.transport.netty4.Netty4Transport.doStart(Netty4Transport.java:174) ~[?:?]
	at 

等内容

需要将配置文件中的ES的端口改为0即

sonar.search.port=0

他就不会再扫描了

2.3汉化

用历史版本的Sonarqube在应用市场貌似不支持汉化,所以只能用过插件的方式去汉化了

github地址:
https://github.com/SonarQubeCommunity/sonar-l10n-zh
参照说明,对应的SonarQube版本下载对应的兼容插件即可。

参考地址:https://blog.csdn.net/xinluke/article/details/52174026

我的版本对应的插件是1.1.6为如下地址

https://codechina.csdn.net/mirrors/sonarqubecommunity/sonar-l10n-zh/-/releases#sonar-l10n-zh-plugin-1.16

直接下载对应jar包然后放到下面这个目录上去重启sonarqube就可以了

extensions\plugins

集成Jenkins

1.安装插件

1.1插件管理-搜索 SonarQube Scanner for Jenkins

image

1.2 Global Tool Configuration全局工具配置中就会出现

image

如下两个选项配置如下

image

image

记得选择Install from GIThub

1.3全局配置会出现 SonarQube servers

配置如下

image

2集成Jenkins

2.1创建sonarqube密钥

image

在上图二的位置填写上密钥如下图

image

记得复制下来密钥后面会游泳,因为生成之后页面就不会显示了

2.2集成jenkins

构建步骤的时候新建两个构建步骤

  1. SonarScanner for MSBuild - Begin Analysis

  2. SonarScanner for MSBuild - End Analysis

    用这两个步骤中间夹需要编译发布的工程

image

Project key :第一步复制的登录的key

Project name :显示在sonarqube中的项目的名称

Project version :显示在sonarqube中的项目的版本

然后保存-应用-立即构建

2.3集成常见问题

1.第一次构建的时候会下载对应的sonar-scanner-msbuild包从github上面,辞职国外镜像地址太慢了

Unpacking https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/5.2.1.31210/sonar-scanner-msbuild-5.2.1.31210-net46.zip to D:\Jenkins\work\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\sonar-Msbuild-.netframework on Jenkins
FATAL: Failed to install https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/5.2.1.31210/sonar-scanner-msbuild-5.2.1.31210-net46.zip to D:\Jenkins\work\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\sonar-Msbuild-.netframework

可以切换未国内地址就可以解决

2.访问svn地址服务失败, 原因是我的项目是从SVN检出的 所以当sonar去检测的时候没有SVN支持 sonar-runner发现了.svn文件,于是启动了自己的SVN插件,去访问SVN,但是又没有对应SVN路径的授权所以就报错。

ERROR: Error during SonarScanner execution
java.lang.IllegalStateException: Error when executing blame for file Controllers/BaseController.cs
	at org.sonar.plugins.scm.svn.SvnBlameCommand.blame(SvnBlameCommand.java:85)
	at org.sonar.plugins.scm.svn.SvnBlameCommand.blame(SvnBlameCommand.java:58)
	at org.sonar.scanner.scm.ScmPublisher.publish(ScmPublisher.java:85)
	at org.sonar.scanner.phases.PublishPhaseExecutor.afterSensors(PublishPhaseExecutor.java:60)
	at org.sonar.scanner.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:90)
	at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:177)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
	at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:291)
	at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:286)
	at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:284)
	at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:264)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
	at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:48)
	at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:84)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
	at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:121)
	at org.sonar.batch.bootstrapper.Batch.doExecuteTask(Batch.java:116)
	at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:71)
	at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
	at com.sun.proxy.$Proxy0.execute(Unknown Source)
	at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
	at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
	at org.sonarsource.scanner.cli.Main.execute(Main.java:112)
	at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
	at org.sonarsource.scanner.cli.Main.main(Main.java:61)
Caused by: org.tmatesoft.svn.core.SVNAuthenticationException: svn: E170001: Authentication required for '<http://> VisualSVN Server'

此时如下设置

image

将 Disabled the SCM Sensor设置为true 或者在svn页面,设置svn的用户名和密码。

3.终于成功了但是发现没有分析.net的代码如下图

image

最终的解决方案时在extensions\plugins中安装了重新放进去一个sonar-csharp-plugin-5.9.0.1001.jar

c#的扫描扩展工具下载地址如下https://binaries.sonarsource.com/Distribution/sonar-csharp-plugin/

重启,解决

4.终于构建成功了但是提示 Failed to upload report - 500: An error has occurred. Please contact your administrator

INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 2:26.110s
INFO: Final Memory: 18M/719M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarScanner execution
ERROR: Failed to upload report - 500: An error has occurred. Please contact your administrator
ERROR: 
The SonarScanner did not complete successfully

此时如果你去sonqube日志web.log中找找你会看到这么一个错误日志

ava.lang.IllegalStateException: Fail to insert data of CE task AXl-uWTF2T51LgfwJ_Et
	at org.sonar.db.ce.CeTaskInputDao.insert(CeTaskInputDao.java:56)
	at org.sonar.server.computation.queue.ReportSubmitter.submitReport(ReportSubmitter.java:178)
	at org.sonar.server.computation.queue.ReportSubmitter.submit(ReportSubmitter.java:97)
	at org.sonar.server.ce.ws.SubmitAction.handle(SubmitAction.java:114)
	at org.sonar.server.ws.WebServiceEngine.execute(WebServiceEngine.java:103)
	at org.sonar.server.ws.WebServiceFilter.doFilter(WebServiceFilter.java:86)
	at org.sonar.server.platform.web.MasterServletFilter$GodFilterChain.doFilter(MasterServletFilter.java:126)
	at org.sonar.server.platform.web.MasterServletFilter.doFilter(MasterServletFilter.java:95)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.sonar.server.user.UserSessionFilter.doFilter(UserSessionFilter.java:87)
	at org.sonar.server.user.UserSessionFilter.doFilter(UserSessionFilter.java:71)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.sonar.server.platform.web.SecurityServletFilter.doHttpFilter(SecurityServletFilter.java:72)
	at org.sonar.server.platform.web.SecurityServletFilter.doFilter(SecurityServletFilter.java:48)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.sonar.server.platform.web.RedirectFilter.doFilter(RedirectFilter.java:61)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.sonar.server.platform.web.requestid.RequestIdFilter.doFilter(RequestIdFilter.java:63)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.sonar.server.platform.web.RootFilter.doFilter(RootFilter.java:62)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:109)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at ch.qos.logback.access.tomcat.LogbackValve.invoke(LogbackValve.java:256)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (6266894 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.
	at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3678)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2509)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2490)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
	at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)
	at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
	at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
	at org.sonar.db.ce.CeTaskInputDao.insert(CeTaskInputDao.java:53)
	... 46 common frames omitted

Packet for query is too large (6266894 > 4194304). You can change this value on the server by setting the max_allowed_packet'

说明分析报告太大了,如下图

image

我的分析报告时4M但是mysql配置中只支持4M

参考文章:https://www.cnblogs.com/demingblog/p/6270187.html

解决办法:修改mysql中的全局变量max_allowed_packet 记得一定要在sonar这个库中修改

show VARIABLES like '%max_allowed_packet%';

  可以看到max_allowed_packet的大小,用这个值 max_allowed_packet / 1024 /1024 = ? M

所以把这个设置大点就好了。在mysql的命令行窗口执行如下命令:

set global max_allowed_packet=6*1024*1024

如下图终于构建成功

image

点击上图中的sonarqube图标可以连接到sonarqube中查看报告

image

这.....

posted @ 2021-05-21 09:32  zhao56  阅读(161)  评论(0编辑  收藏  举报