首页 » docker » docker 实现jenkins+sonarqube+sonar-scanner自动实现代码质量测试

docker 实现jenkins+sonarqube+sonar-scanner自动实现代码质量测试

 
文章目录

docker 实现jenkins+sonarqube+sonar-scanner自动实现代码质量测试

jenkins与gitlab的结合,当gitlab有push操作的时候,触发Jenkins自动构建,Jenkins需要安装以下插件 :gitlab hook plugin,gitlab plugin,build authorization token root plugin,其中最后一个最重要,参考自己的博文https://www.sacus.top/?p=624

版本说明

jenkins是2.32,官方的版本:docker pull jenkins就可以。 sonarqube是6.2最新的版本,也是官方的版本,docker pull sonarqube就可以。 sonar-scanner是直接下载下来,是二进行的,放在jenkins的容器里,版本是2.8的。

运行说明

sonarqube有自带的数据库H2,这里我没有用,我用容器启动的一个数据库db,数据库中的数据库账号和数据库已经创建好了,先启动db,在启动sonarqube,sonarqube 要link db ,让其直接在docker内部通信,应该效率更高,最后启动jenkins,因为jenkins是link sonarqube

运行命令:

映射的目录一般是数据目录,比较重要的,当docker down了,我们可以去另外的机器,启动起来就可以用了。 
Jenkins, jenkins安装最后不要选as admin,不然退出来之后登录不进去了。

docker run -d  --name jenkins-clc \
-p 8090:8080 -p 50000:50000 \
--link db:db --link sonarqube:sonarqube \
-v /home/chenlc/docker/jenkins/data:/var/jenkins_home \
 jenkins

Sonarqube运行命令如下:

docker run  -d --name sonarqube -p 9000:9000 -p 9092:9092 \
--link db:db  -e SONARQUBE_JDBC_USERNAME="sonar"   \
-e SONARQUBE_JDBC_PASSWORD="sonar@clc" \
-e SONARQUBE_JDBC_URL="jdbc:mysql://db:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance"  \
-v /home/chenlc/docker/sonarqube/conf:/opt/sonarqube/conf \
-v /home/chenlc/docker/sonarqube/data:/opt/sonarqube/data  \
-v /home/chenlc/docker/sonarqube/extensions:/opt/sonarqube/extensions \
-v /home/chenlc/docker/sonarqube/bundled-plugins:/opt/sonarqube/lib/bundled-plugins  \
-v /home/chenlc/docker/sonarqube/logs:/opt/sonarqube/logs  \
sonarqube

Sonar-scanner是直接放在jenkins的data目录下面,/home/chenlc/docker/jenkins/data/sonar-scanner-2.8。

插件安装

jenkins需要安装sonarqube plugin 、gitlab hook及gitlab相关的插件。 
sonarqube需要安装你检测语言的插件,如php,还可以安装中文包,把web界面弄成中文。

jenkins与sonarqube结合配置

  1. 插件需要安装sonarqube plugin
  2. 系统管理—系统设置中配置sonarqube,如下:qq%e5%9b%be%e7%89%8720170208173515
  3. 系统管理–Global Tool Configuration中配置,如下: qq%e5%9b%be%e7%89%8720170208173557
  4. 在jobs项目,对需要检查的项目进行设置,
    前提是源码设置里要设置,不然没有代码拉取下来,就没在办法测试,源码配置了gitlab源的话,构建就会去gitlab上拉取代码,拉取的代码jenkins在放在data目录下的workspace下对应的你的jobs项目名称, qq%e5%9b%be%e7%89%8720170208173246

安装过程中遇到的问题

在jenkins容器中执行测试代码扫描,会报以下错误, 但是在sonarqube容器又不会,然后查找问题,发现:jenkins是从openjdk:8-jdk来build的,sonarqube是从openjdk:8来build的,正想测试是不是这个的问题,在下载image的时候,我直接在jenkins上设置测试了一下,发现没有问题,所以没有试,如果不在docker里面,在vm或物理机上测试都是可以的。 
错误如下:

INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 6.038s
INFO: Final Memory: 40M/153M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
java.lang.IllegalStateException: Unable to load component class org.sonar.scanner.scan.ProjectLock
	at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:69)
	at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:678)
	at org.sonar.core.platform.ComponentContainer.getComponentByType(ComponentContainer.java:272)
	at org.sonar.scanner.scan.ProjectScanContainer.doBeforeStart(ProjectScanContainer.java:110)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:140)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
	at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:47)
	at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:86)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
	at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:115)
	at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:116)
	at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:62)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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:233)
	at org.sonarsource.scanner.api.EmbeddedScanner.runAnalysis(EmbeddedScanner.java:151)
	at org.sonarsource.scanner.cli.Main.runAnalysis(Main.java:110)
	at org.sonarsource.scanner.cli.Main.execute(Main.java:74)
	at org.sonarsource.scanner.cli.Main.main(Main.java:61)
Caused by: java.lang.IllegalStateException: Failed to create work directory
	at org.sonar.scanner.scan.ProjectLock.(ProjectLock.java:42)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.picocontainer.injectors.AbstractInjector.newInstance(AbstractInjector.java:145)
	at org.picocontainer.injectors.ConstructorInjector$1.run(ConstructorInjector.java:342)
	at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
	at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)
	at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.getComponentInstance(AbstractInjectionFactory.java:56)
	at org.picocontainer.behaviors.AbstractBehavior.getComponentInstance(AbstractBehavior.java:64)
	at org.picocontainer.behaviors.Stored.getComponentInstance(Stored.java:91)
	at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
	at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
	at org.sonar.core.platform.ComponentContainer$ExtendedDefaultPicoContainer.getComponent(ComponentContainer.java:67)
	... 23 more
Caused by: java.nio.file.AccessDeniedException: /var/jenkins_home/sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests/.sonar
	at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
	at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384)
	at java.nio.file.Files.createDirectory(Files.java:674)
	at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781)
	at java.nio.file.Files.createDirectories(Files.java:767)
	at org.sonar.scanner.scan.ProjectLock.(ProjectLock.java:39)
	... 37 more
ERROR: 
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

后续有时间可以用docker-compose来启动,把db,jenkins,sonarqube三个启动命令写在docker-compose.yml中,直接docker-compose up就启动了。

 

原文链接:docker 实现jenkins+sonarqube+sonar-scanner自动实现代码质量测试,转载请注明来源!

2