代码质量集成管理

代码质量集成管理 - Jenkins+Gitlab+SonarQube

一、前期准备

1.1 Jenkins安装

参考安装教程:1.Jenkins安装教程-RPM;2.jenkins自动部署-Gitlab

1.2 Gitlab安装

参考安装教程:版本管控工具-gitlab

1.3 SonarQube安装

参考安装教程:代码质量管理工具-SonarQube

二、集成管理配置

2.1 gitlab新建测试项目

参考安装教程:Gitlab-项目创建

2.2 Jenkins配置

2.2.1 安装Sonarqube Scanner插件

image-20210220224516545

2.2.2 配置服务

2.2.2.1 系统配置中添加SonarQube Scanner

image-20210220152717962

2.2.2.2 添加SonarQube Scanner凭证

image-20210220152534081

2.2.2.3 在SonarQube生成令牌

image-20210220225136811

2.2.2.3 添加SonarQube全局配置

image-20210220225629187

2.2.3 安装JDK11.0

2.2.3.1 Jenkins安装所在系统安装JDK11

image-20210220225957451

2.2.3.2 Jenkins全局配置JDK11

image-20210220230214125

2.3 新建项目测试

2.3.1 通用设置

image-20210220230514354

2.3.2 源码管理

image-20210220230627065

2.3.3 构建触发器

image-20210220230856617

2.3.4 Gitlab创建webhook

image-20210220231053128

2.3.5 构建环境

image-20210220231219126

重启脚本

 #!/bin/bash
 source /etc/profile
 chmod 755 /app/gitlab/jenkins-gitlab.jar
 count=`ps -ef|grep java|grep jenkins-gitlab.jar |awk '{print $2}'`
 if [ $count -gt 0 ]; then
   echo '原进程:'$count
   echo '重新启动中...'
   kill -9 $count
   sleep 1
   export BUILD_ID=dontKillMe
  (java -jar /app/gitlab/jenkins-gitlab.jar > /app/gitlab/log/log.log 2>&1 &)
 else
   echo '启动中...'
   sleep 1
   export BUILD_ID=dontKillMe
  (java -jar /app/gitlab/jenkins-gitlab.jar > /app/gitlab/log/log.log 2>&1 &)
 fi
 echo '现进程:' `ps -ef|grep java|grep jenkins-gitlab.jar |awk '{print $2}'`

2.3.6 构建(build)

image-20210220231409631

2.3.7 Post Steps

image-20210220231733132

2.3.8 项目启动

image-20210220232044991

2.3.9 控制台日志

 Started by GitLab push by Tom Smith
 Running as SYSTEM
 Building in workspace /var/lib/jenkins/workspace/Jenkins-SonarQube
 The recommended git tool is: NONE
 using credential Gilab-User
  > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
 Fetching changes from the remote Git repository
  > /usr/bin/git config remote.origin.url http://192.168.61.72/zoops/sonar-test.git # timeout=10
 Fetching upstream changes from http://192.168.61.72/zoops/sonar-test.git
  > /usr/bin/git --version # timeout=10
  > git --version # 'git version 1.8.3.1'
 using GIT_ASKPASS to set credentials Gilab-User
  > /usr/bin/git fetch --tags --progress http://192.168.61.72/zoops/sonar-test.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 skipping resolution of commit remotes/origin/master, since it originates from another repository
  > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 Checking out Revision 6c690f78575233f95c5bf55a4aa023e674bdecbd (refs/remotes/origin/master)
  > /usr/bin/git config core.sparsecheckout # timeout=10
  > /usr/bin/git checkout -f 6c690f78575233f95c5bf55a4aa023e674bdecbd # timeout=10
 Commit message: "--SonarQube-Scanner扫描"
  > /usr/bin/git rev-list --no-walk d825e8d1cdeca95df59ffee021faef3004ef4466 # timeout=10
 Parsing POMs
 Established TCP socket on 35137
 [Jenkins-SonarQube] $ /usr/local/java/bin/java -cp /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven35-agent-1.13.jar:/usr/local/maven/boot/plexus-classworlds-2.6.0.jar:/usr/local/maven/conf/logging jenkins.maven3.agent.Maven35Main /usr/local/maven/ /var/cache/jenkins/war/WEB-INF/lib/remoting-4.6.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven35-interceptor-1.13.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.13.jar 35137
 <===[JENKINS REMOTING CAPACITY]===>channel started
 Executing Maven:  -B -f /var/lib/jenkins/workspace/Jenkins-SonarQube/pom.xml -s /usr/local/maven/conf/settings.xml -gs /usr/local/maven/conf/settings.xml -DskipTests=true clean compile package
 [INFO] Scanning for projects...
 [INFO]
 [INFO] -----------------------< com.jenkins:sonar-test >-----------------------
 [INFO] Building sonar-test 0.0.1-SNAPSHOT
 [INFO] --------------------------------[ jar ]---------------------------------
 [INFO]
 [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ sonar-test ---
 [INFO] Deleting /var/lib/jenkins/workspace/Jenkins-SonarQube/target
 [INFO]
 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sonar-test ---
 [INFO] Using 'UTF-8' encoding to copy filtered resources.
 [INFO] Copying 2 resources
 [INFO]
 [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ sonar-test ---
 [INFO] Changes detected - recompiling the module!
 [INFO] Compiling 1 source file to /var/lib/jenkins/workspace/Jenkins-SonarQube/target/classes
 [INFO]
 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sonar-test ---
 [INFO] Using 'UTF-8' encoding to copy filtered resources.
 [INFO] Copying 2 resources
 [INFO]
 [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ sonar-test ---
 [INFO] Changes detected - recompiling the module!
 [INFO] Compiling 1 source file to /var/lib/jenkins/workspace/Jenkins-SonarQube/target/classes
 [INFO]
 [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ sonar-test ---
 [INFO] Using 'UTF-8' encoding to copy filtered resources.
 [INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/Jenkins-SonarQube/src/test/resources
 [INFO]
 [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ sonar-test ---
 [INFO] Changes detected - recompiling the module!
 [INFO] Compiling 1 source file to /var/lib/jenkins/workspace/Jenkins-SonarQube/target/test-classes
 [INFO]
 [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ sonar-test ---
 [INFO] Tests are skipped.
 [INFO]
 [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ sonar-test ---
 [INFO] Building jar: /var/lib/jenkins/workspace/Jenkins-SonarQube/target/sonar-test.jar
 [INFO]
 [INFO] --- spring-boot-maven-plugin:2.3.7.RELEASE:repackage (repackage) @ sonar-test ---
 [INFO] Replacing main artifact with repackaged archive
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time:  3.878 s
 [INFO] Finished at: 2021-02-20T22:31:53+08:00
 [INFO] ------------------------------------------------------------------------
 Waiting for Jenkins to finish collecting data
 [JENKINS] Archiving /var/lib/jenkins/workspace/Jenkins-SonarQube/pom.xml to com.jenkins/sonar-test/0.0.1-SNAPSHOT/sonar-test-0.0.1-SNAPSHOT.pom
 [JENKINS] Archiving /var/lib/jenkins/workspace/Jenkins-SonarQube/target/sonar-test.jar to com.jenkins/sonar-test/0.0.1-SNAPSHOT/sonar-test-0.0.1-SNAPSHOT.jar
 channel stopped
 [Jenkins-SonarQube] $ /var/lib/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube-Scanner/bin/sonar-scanner -Dsonar.host.url=http://192.168.61.81:9000 ******** -Dproject.settings=/var/lib/jenkins/workspace/Jenkins-SonarQube/sonar-scanner.properties -Dsonar.java.binaries=/var/lib/jenkins/workspace/Jenkins-SonarQube/target/classes -Dsonar.sources=/var/lib/jenkins/workspace/Jenkins-SonarQube -Dsonar.projectBaseDir=/var/lib/jenkins/workspace/Jenkins-SonarQube
 INFO: Scanner configuration file: /var/lib/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube-Scanner/conf/sonar-scanner.properties
 INFO: Project root configuration file: /var/lib/jenkins/workspace/Jenkins-SonarQube/sonar-scanner.properties
 INFO: SonarQube Scanner 4.2.0.1873
 INFO: Java 11.0.10 Oracle Corporation (64-bit)
 INFO: Linux 3.10.0-1160.11.1.el7.x86_64 amd64
 INFO: User cache: /root/.sonar/cache
 INFO: SonarQube server 7.9.5
 INFO: Default locale: "zh_CN", source code encoding: "UTF-8" (analysis is platform dependent)
 INFO: Load global settings
 INFO: Load global settings (done) | time=54ms
 INFO: Server id: 48F3CEBB-AXe5bCYaWLFD_1L1uUGJ
 INFO: User cache: /root/.sonar/cache
 INFO: Load/download plugins
 INFO: Load plugins index
 INFO: Load plugins index (done) | time=34ms
 INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
 INFO: Load/download plugins (done) | time=74ms
 INFO: Process project properties
 INFO: Execute project builders
 INFO: Execute project builders (done) | time=2ms
 INFO: Project key: sonar-test
 INFO: Base dir: /var/lib/jenkins/workspace/Jenkins-SonarQube
 INFO: Working dir: /var/lib/jenkins/workspace/Jenkins-SonarQube/.scannerwork
 INFO: Load project settings for component key: 'sonar-test'
 INFO: Load project settings for component key: 'sonar-test' (done) | time=17ms
 INFO: Load quality profiles
 INFO: Load quality profiles (done) | time=35ms
 INFO: Detected Jenkins
 INFO: Load active rules
 INFO: Load active rules (done) | time=394ms
 INFO: Indexing files...
 INFO: Project configuration:
 INFO: 20 files indexed
 INFO: Quality profile for java: Sonar way
 INFO: Quality profile for web: Sonar way
 INFO: Quality profile for xml: Sonar way
 INFO: ------------- Run sensors on module sonar-test
 INFO: Load metrics repository
 INFO: Load metrics repository (done) | time=20ms
 WARNING: An illegal reflective access operation has occurred
 WARNING: Illegal reflective access by net.sf.cglib.core.ReflectUtils$1 (file:/root/.sonar/cache/866bb1adbf016ea515620f1aaa15ec53/sonar-javascript-plugin.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
 WARNING: Please consider reporting this to the maintainers of net.sf.cglib.core.ReflectUtils$1
 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
 WARNING: All illegal access operations will be denied in a future release
 INFO: Sensor JavaSquidSensor [java]
 INFO: Configured Java source version (sonar.java.source): none
 INFO: JavaClasspath initialization
 WARN: Bytecode of dependencies was not provided for analysis of source files, you might end up with less precise results. Bytecode can be provided using sonar.java.libraries property.
 INFO: JavaClasspath initialization (done) | time=7ms
 INFO: JavaTestClasspath initialization
 INFO: JavaTestClasspath initialization (done) | time=0ms
 INFO: Java Main Files AST scan
 INFO: 2 source files to be analyzed
 INFO: Load project repositories
 INFO: Load project repositories (done) | time=15ms
 INFO: 2/2 source files have been analyzed
 INFO: Java Main Files AST scan (done) | time=323ms
 INFO: Java Test Files AST scan
 INFO: 0 source files to be analyzed
 INFO: Java Test Files AST scan (done) | time=0ms
 INFO: 0/0 source files have been analyzed
 INFO: Sensor JavaSquidSensor [java] (done) | time=757ms
 INFO: Sensor JaCoCo XML Report Importer [jacoco]
 INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=2ms
 INFO: Sensor SurefireSensor [java]
 INFO: parsing [/var/lib/jenkins/workspace/Jenkins-SonarQube/target/surefire-reports]
 INFO: Sensor SurefireSensor [java] (done) | time=1ms
 INFO: Sensor JaCoCoSensor [java]
 INFO: Sensor JaCoCoSensor [java] (done) | time=0ms
 INFO: Sensor JavaXmlSensor [java]
 INFO: 1 source files to be analyzed
 INFO: Sensor JavaXmlSensor [java] (done) | time=110ms
 INFO: Sensor HTML [web]
 INFO: 1/1 source files have been analyzed
 INFO: Sensor HTML [web] (done) | time=56ms
 INFO: Sensor XML Sensor [xml]
 INFO: 1 source files to be analyzed
 INFO: Sensor XML Sensor [xml] (done) | time=84ms
 INFO: ------------- Run sensors on project
 INFO: 1/1 source files have been analyzed
 INFO: Sensor Zero Coverage Sensor
 INFO: Sensor Zero Coverage Sensor (done) | time=5ms
 INFO: Sensor Java CPD Block Indexer
 INFO: Sensor Java CPD Block Indexer (done) | time=7ms
 INFO: SCM Publisher is disabled
 INFO: 2 files had no CPD blocks
 INFO: Calculating CPD for 2 files
 INFO: CPD calculation finished
 INFO: Analysis report generated in 43ms, dir size=81 KB
 INFO: Analysis report compressed in 10ms, zip size=17 KB
 INFO: Analysis report uploaded in 21ms
 INFO: ANALYSIS SUCCESSFUL, you can browse http://192.168.61.81:9000/dashboard?id=sonar-test
 INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
 INFO: More about the report processing at http://192.168.61.81:9000/api/ce/task?id=AXe_2ryRgf7k25Y1CzAM
 INFO: Analysis total time: 2.984 s
 INFO: ------------------------------------------------------------------------
 INFO: EXECUTION SUCCESS
 INFO: ------------------------------------------------------------------------
 INFO: Total time: 3.707s
 INFO: Final Memory: 7M/27M
 INFO: ------------------------------------------------------------------------
 SSH: Connecting from host [localhost.localdomain]
 SSH: Connecting with configuration [idea-maven-test] ...
 SSH: EXEC: completed after 1,204 ms
 SSH: Disconnecting configuration [idea-maven-test] ...
 SSH: Transferred 1 file(s)
 Finished: SUCCESS

2.3.10 项目构建历史

image-20210220232612096

2.3.11 SonarQube扫描查看

image-20210220232800431

以上,请参考!


参考链接

  1. https://blog.csdn.net/weixin_38657051/article/details/102519042

原文地址:https://www.cnblogs.com/shuangxinyuan/p/15506456.html