目录
- 一、前言
- 二、Jenkins中安装Maven Integration插件
- 三、Jenkins中安装Gitlab插件
- 四、Jenkins中安装SonarQube插件
- 五、Jenkins重启
- 六、Jenkins中配置JDK、Maven
- 1.配置JDK
- 2.配置Maven
- 七、GitLab中创建Access Tokens
- 八、Jenkins中配置gitlab插件
- 九、SonarQube中生成token
- 十、Jenkins中配置SonarQube servers
- 十一、Jenkins中配置SonarQube Scanner
- 十二、Jenkins中创建任务
- 1.配置源码管理
- 2.构建触发器
- 3.构建环境
- 4.Build
- 5.Post Steps
- 6.手动验证
- 十三、Gitlab中配置webhook
- 十四、遇坑记
- 1.构建报错“No files nor directories matching...”
- 2.构建报错“Not authorized. Analyzing this project requires authentication...”
一、前言
该文将搭建Jenkins、SonarQube、Gitlab自动化代码质量管理平台中遇到的坑都一一记录了下来,对搭建人员有很大的参考价值。
二、Jenkins中安装Maven Integration插件
进入【系统管理】->【插件管理】
注意:如果没有安装Maven Integration插件,新建任务时会找不到Maven项目
三、Jenkins中安装Gitlab插件
进入【系统管理】->【插件管理】
四、Jenkins中安装SonarQube插件
进入【系统管理】->【插件管理】
五、Jenkins重启
登录后在浏览器地址后加restart:http://192.168.1.35:8000/restart
六、Jenkins中配置JDK、Maven
进入【系统管理】->【全局工具配置】
1.配置JDK
注意:
1)、jdk的路径为容器内的路径,可在容器内执行echo $JAVA_HOME查看
2)、试了自动安装不生效
2.配置Maven
注意:试了自动安装不生效
七、GitLab中创建Access Tokens
在User Setttings 配置Aceess Tokens,输入Name、Expires at,勾选“api”,点击【Create personal access token】创建token。
注意:一定要将生成的token保存下来,后面无法再查看生成的token,如未保存后面只能重新生成
八、Jenkins中配置gitlab插件
进入【系统管理】->【系统配置】
选中类型“GitLab API token”,输入API token,点击【添加】:
添加完成后,测试是否配置成功:
九、SonarQube中生成token
点击右上角“我的账号”->“安全”:
十、Jenkins中配置SonarQube servers
进入【系统管理】->【系统配置】
勾选“Environment variables Enable injection of SonarQube server configuration …”选项,输入Name、Server URL
注意:一定要勾选“Environment variables Enable injection of SonarQube server configuration …”,否则后面的步骤提示sonar授权失败!
Server authentication token 下点击“添加”
注意:谷歌下点击【添加】无响应,更换为360浏览器可以
选中“GitLab API token”,输入第七步生成的 sonar token:
十一、Jenkins中配置SonarQube Scanner
进入【系统管理】->【全局工具配置】
十二、Jenkins中创建任务
1.配置源码管理
选中“Git”
2.构建触发器
勾选“Build when a change is pushed to GitLab. GitLab webhook URL: …”
点击“高级”按钮,点击“Generate”按钮生成 Secret token
3.构建环境
勾选“Prepare SonarQube Scanner environment”
注意:必须在第九步勾选“Environment variables Enable injection of SonarQube server configuration …”才会出现这个选项
4.Build
5.Post Steps
“Analysis properties”内容:
sonar.login=admin
sonar.password=******
sonar.projectKey=user_platform
sonar.projectName=user_platform
sonar.language=java
sonar.java.binaries=$WORKSPACE/user_playform/target/classes/
sonar.sources=$WORKSPACE/user_playform/src
sonar.java.source=1.8
6.手动验证
在sonar可以看到分析结果:
十三、Gitlab中配置webhook
待续
十四、遇坑记
1.构建报错“No files nor directories matching…”
ERROR: Error during SonarScanner execution
java.lang.IllegalStateException: No files nor directories matching '/var/jenkins_home/workspace/user_platform/user_playform/target/classes/'
at org.sonar.java.classpath.AbstractClasspath.getFilesFromProperty(AbstractClasspath.java:129)
at org.sonar.java.classpath.ClasspathForMain.init(ClasspathForMain.java:58)
at org.sonar.java.classpath.AbstractClasspath.getElements(AbstractClasspath.java:317)
at org.sonar.java.SonarComponents.getJavaClasspath(SonarComponents.java:164)
at org.sonar.java.JavaSquid.<init>(JavaSquid.java:78)
at org.sonar.plugins.java.JavaSquidSensor.execute(JavaSquidSensor.java:95)
at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48)
at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85)
at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:59)
at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:77)
at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:59)
at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:389)
at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:385)
at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:354)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:144)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72)
at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66)
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/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)
解决方法:
在/var/jenkins_home/workspace/user_platform/user_playform/下创建目录./target/classes/
2.构建报错“Not authorized. Analyzing this project requires authentication…”
ERROR: Error during SonarScanner execution
ERROR: Not authorized. Analyzing this project requires authentication. Please provide a user token in sonar.login or other credentials in sonar.login and sonar.password.
解决方法:
构建环境中勾选“Prepare SonarQube Scanner environment”
注意:必须在第九步勾选“Environment variables Enable injection of SonarQube server configuration …”才会出现这个选项