代码质量管理平台
一、checkout和打包功能
1.1 gitlab在新建一个文件
后续在写入内容
1.2 Jenkins新建一个任务
两个参数
1.3 流水线配置
copy仓库地址:
http://172.25.254.131/devops/jenkins.git
保存
1.4 生成流水线脚本
复制给i他地址。配置pipeline syntax
1.6 编写sonar.kenkinsfile文件
#!/usr/bin/groovy node(){ //checkout checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '87684a81-092f-4175-afff-6d9df42f2b14', url: 'http://172.25.254.131/devops/jenkins.git']]]) //build def mvnHome = tool 'M2_HOME' sh "${mvnHome}/bin/mvn ${buildShell}" }
1.7 提交代码,构建
Started by user admin Obtained sonar.jenkinsfile from git http://172.25.254.131/devops/jenkins.git Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /root/.jenkins/workspace/test1-sonar-service_TEST [Pipeline] { [Pipeline] checkout using credential 87684a81-092f-4175-afff-6d9df42f2b14 Cloning the remote Git repository Cloning repository http://172.25.254.131/devops/jenkins.git > git init /root/.jenkins/workspace/test1-sonar-service_TEST # timeout=10 Fetching upstream changes from http://172.25.254.131/devops/jenkins.git > git --version # timeout=10 using GIT_ASKPASS to set credentials > git fetch --tags --progress http://172.25.254.131/devops/jenkins.git +refs/heads/*:refs/remotes/origin/* > git config remote.origin.url http://172.25.254.131/devops/jenkins.git # timeout=10 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10 > git config remote.origin.url http://172.25.254.131/devops/jenkins.git # timeout=10 Fetching upstream changes from http://172.25.254.131/devops/jenkins.git using GIT_ASKPASS to set credentials > git fetch --tags --progress http://172.25.254.131/devops/jenkins.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision e844e8870b03a721578cf029494ce02ce311c7fb (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f e844e8870b03a721578cf029494ce02ce311c7fb Commit message: "Update sonar.jenkinsfile" First time build. Skipping changelog. [Pipeline] tool [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline groovy.lang.MissingPropertyException: No such property: buildShell for class: groovy.lang.Binding #没有这个参数 at groovy.lang.Binding.getVariable(Binding.java:63) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:264) at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:289) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:293) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:269) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) at WorkflowScript.run(WorkflowScript:13) at ___cps.transform___(Native Method) at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74) at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30) at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66) 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 com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72) at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21) at com.cloudbees.groovy.cps.Next.step(Next.java:83) at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174) at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163) at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129) at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268) at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:136) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58) at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:347) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:259) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:247) at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131) at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Finished: FAILURE
1.8 添加一个参数,配置
失败,没有安装mvn
1.9 安装maven
[root@jenkins-master ~]# mkdir /usr/local/maven
[root@jenkins-master ~]# cd /usr/local/maven
[root@jenkins-master maven]# wget http://apache.communilink.net/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz
再次构建
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/root/.jenkins/workspace/test1-sonar-service_TEST). Please verify you invoked Maven from the correct directory. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException
1.10 选择另一个的gitlab的关于maven的project
http://172.25.254.131/tester/mvn-test01.git
重新配置流水线
流水线语法
生成流水线脚本
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://172.25.254.131/tester/mvn-test01.git']]])
新建snoar.jenkinsfile
#!/usr/bin/groovy node(){ //checkout checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://172.25.254.131/tester/mvn-test01.git']]]) //build def mvnHome = tool 'M2_HOME' sh "${mvnHome}/bin/mvn ${buildShell}" }
1.11 再次构建
输出台信息
Started by user admin Obtained sonar.jenkinsfile from git http://172.25.254.131/tester/mvn-test01.git Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /root/.jenkins/workspace/test1-sonar-service_TEST [Pipeline] { [Pipeline] checkout No credentials specified > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://172.25.254.131/tester/mvn-test01.git # timeout=10 Fetching upstream changes from http://172.25.254.131/tester/mvn-test01.git > git --version # timeout=10 > git fetch --tags --progress http://172.25.254.131/tester/mvn-test01.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision 7060102f73c77cbfbf9dc1d02ff0c905d17328a5 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 7060102f73c77cbfbf9dc1d02ff0c905d17328a5 Commit message: "Update jaja.jenkinsfile" > git rev-list --no-walk 7060102f73c77cbfbf9dc1d02ff0c905d17328a5 # timeout=10 [Pipeline] tool [Pipeline] sh + /usr/local/maven/apache-maven-3.6.1/bin/mvn clean package -DskipTests [INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.example.app:maven-app >---------------------- [INFO] Building maven-app 1.3-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-app --- [INFO] Deleting /root/.jenkins/workspace/test1-sonar-service_TEST/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/.jenkins/workspace/test1-sonar-service_TEST/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/.jenkins/workspace/test1-sonar-service_TEST/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/.jenkins/workspace/test1-sonar-service_TEST/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/.jenkins/workspace/test1-sonar-service_TEST/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-app --- [INFO] Building jar: /root/.jenkins/workspace/test1-sonar-service_TEST/target/maven-app-1.3-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.386 s [INFO] Finished at: 2019-04-17T16:18:51+08:00 [INFO] ------------------------------------------------------------------------ [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
成功!!!
二 、结合sonar
2.1 安装sonar-scanner
[root@jenkins-master jenkins]# cd /usr/local/
[root@jenkins-master local]# mv sonar-scanner-3.3.0.1492-linux sonar-scanner
[root@jenkins-master ~]# vim /etc/profile
export SONAR_SCANNER=/usr/local/sonar-scanner
export PATH=$PATH:$JAVA_HOME/bin:$SONAR_SCANNER/bin
[root@jenkins-master ~]# source /etc/profile
这里使用sonnar命令完成
6f7a4c06ef06a8be9799097024c8f2f6fe2fafe7
2.2 修改sonar.kenjinsfile文件
#!/usr/bin/groovy node(){ //checkout checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://172.25.254.131/tester/mvn-test01.git']]]) //build def mvnHome = tool 'M2_HOME' sh "${mvnHome}/bin/mvn ${buildShell}" //Sonar sh """ sonar-scanner -Dsonar.projectKey=${serviceName} -Dsonar.projectName=${serviceName} -Dsonar.login=6f7a4c06ef06a8be9799097024c8f2f6fe2fafe7 -Dsonar.source=src -Dsonar.host.url=${sonarServer} """ }
2.3 尝试构建修改
开始构建
结果信息
[Pipeline] sh + sonar-scanner -Dsonar.projectKey=test1-sonar-service -Dsonar.projectName=test1-sonar-service -Dsonar.login=6f7a4c06ef06a8be9799097024c8f2f6fe2fafe7 -Dsonar.host.url=src /root/.jenkins/workspace/test1-sonar-service_TEST@tmp/durable-f84d04fc/script.sh: line 2: sonar-scanner: command not found [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: script returned exit code 127 Finished: FAILURE
修改
#!/usr/bin/groovy node(){ //checkout checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://172.25.254.131/tester/mvn-test01.git']]]) //build def mvnHome = tool 'M2_HOME' sh "${mvnHome}/bin/mvn ${buildShell}" //Sonar sh """ /usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=${serviceName} -Dsonar.projectName=${serviceName} -Dsonar.login=6f7a4c06ef06a8be9799097024c8f2f6fe2fafe7 -Dsonar.source=src -Dsonar.host.url=${sonarServer}
-Dsonar.java.binaries=/root/.jenkins/workspace/test1-sonar-service_TEST/target/classes """ }
构建
INFO: ------------------------------------------------------------------------ ERROR: Error during SonarQube Scanner execution ERROR: You must define the following mandatory properties for 'test1-sonar-service': sonar.sources ERROR: ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging. [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: script returned exit code 1 Finished: FAILURE
修改:
#!/usr/bin/groovy node(){ //checkout checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://172.25.254.131/tester/mvn-test01.git']]]) //build def mvnHome = tool 'M2_HOME' sh "${mvnHome}/bin/mvn ${buildShell}" //Sonar sh """ /usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=${serviceName} -Dsonar.projectName=${serviceName} -Dsonar.login=6f7a4c06ef06a8be9799097024c8f2f6fe2fafe7 -Dsonar.sources=src -Dsonar.host.url=${sonarServer} """ }
再次构建
Started by user admin Obtained sonar.jenkinsfile from git http://172.25.254.131/tester/mvn-test01.git Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /root/.jenkins/workspace/test1-sonar-service_TEST [Pipeline] { [Pipeline] checkout No credentials specified > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://172.25.254.131/tester/mvn-test01.git # timeout=10 Fetching upstream changes from http://172.25.254.131/tester/mvn-test01.git > git --version # timeout=10 > git fetch --tags --progress http://172.25.254.131/tester/mvn-test01.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision ad3e34fe85103fdf2229159cf14e8f17493b799a (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f ad3e34fe85103fdf2229159cf14e8f17493b799a Commit message: "Update sonar.jenkinsfile" > git rev-list --no-walk d57ef66ee0b2a069a63da5fe6afc2b1c7d276e67 # timeout=10 [Pipeline] tool [Pipeline] sh + /usr/local/maven/apache-maven-3.6.1/bin/mvn clean package -DskipTests [INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.example.app:maven-app >---------------------- [INFO] Building maven-app 1.3-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-app --- [INFO] Deleting /root/.jenkins/workspace/test1-sonar-service_TEST/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/.jenkins/workspace/test1-sonar-service_TEST/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/.jenkins/workspace/test1-sonar-service_TEST/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /root/.jenkins/workspace/test1-sonar-service_TEST/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /root/.jenkins/workspace/test1-sonar-service_TEST/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-app --- [INFO] Building jar: /root/.jenkins/workspace/test1-sonar-service_TEST/target/maven-app-1.3-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.356 s [INFO] Finished at: 2019-04-20T02:09:51+08:00 [INFO] ------------------------------------------------------------------------ [Pipeline] sh + /usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=test1-sonar-service -Dsonar.projectName=test1-sonar-service -Dsonar.login=6f7a4c06ef06a8be9799097024c8f2f6fe2fafe7 -Dsonar.sources=src -Dsonar.host.url=http://172.25.254.133:9000 INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties INFO: Project root configuration file: NONE INFO: SonarQube Scanner 3.3.0.1492 INFO: Java 1.8.0_121 Oracle Corporation (64-bit) INFO: Linux 3.10.0-693.el7.x86_64 amd64 INFO: User cache: /root/.sonar/cache INFO: SonarQube server 6.7.7 INFO: Default locale: "en_US", source code encoding: "UTF-8" INFO: Publish mode INFO: Load global settings INFO: Load global settings (done) | time=57ms INFO: Server id: A623D34D-AWoVn6_8P1KovjAYWYot INFO: User cache: /root/.sonar/cache INFO: Load plugins index INFO: Load plugins index (done) | time=136ms INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2. INFO: Process project properties INFO: Load project repositories INFO: Load project repositories (done) | time=13ms INFO: Load quality profiles INFO: Load quality profiles (done) | time=36ms INFO: Load active rules INFO: Load active rules (done) | time=290ms INFO: Load metrics repository INFO: Load metrics repository (done) | time=39ms INFO: Project key: test1-sonar-service INFO: ------------- Scan test1-sonar-service INFO: Load server rules INFO: Load server rules (done) | time=23ms INFO: Base dir: /root/.jenkins/workspace/test1-sonar-service_TEST INFO: Working dir: /root/.jenkins/workspace/test1-sonar-service_TEST/.scannerwork INFO: Source paths: src INFO: Source encoding: UTF-8, default locale: en_US INFO: Index files INFO: 2 files indexed INFO: Sensor Zero Coverage Sensor INFO: Sensor Zero Coverage Sensor (done) | time=9ms INFO: Sensor CPD Block Indexer INFO: Sensor CPD Block Indexer (done) | time=0ms INFO: SCM Publisher is disabled INFO: Calculating CPD for 0 files INFO: CPD calculation finished INFO: Analysis report generated in 49ms, dir size=1 KB INFO: Analysis reports compressed in 6ms, zip size=1 KB INFO: Analysis report uploaded in 442ms INFO: ANALYSIS SUCCESSFUL, you can browse http://172.25.254.133:9000/dashboard/index/test1-sonar-service 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://172.25.254.133:9000/api/ce/task?id=AWpPClxumO3e1qpxfNZm INFO: Task total time: 1.776 s INFO: ------------------------------------------------------------------------ INFO: EXECUTION SUCCESS INFO: ------------------------------------------------------------------------ INFO: Total time: 3.104s INFO: Final Memory: 8M/129M INFO: ------------------------------------------------------------------------ [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
完成
三、使用slave
3.1 在任务配置加一个选项参数
sonar.jenkinsfile文件
#!/usr/bin/groovy node("${nodeSlave}"){ //checkout checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://172.25.254.131/tester/mvn-test01.git']]]) //build def mvnHome = tool 'M2_HOME' sh "${mvnHome}/bin/mvn ${buildShell}"}
[root@jenkins-master ~]# scp -r /usr/local/sonar-scanner 172.25.254.134:/usr/local/
[root@node5 ~]# vim /etc/profile
export SONAR_SCANNER=/usr/local/sonar-scanner
export PATH=$PATH:$SONAR_SCANNER/bin
[root@node5 ~]# source /etc/profile
执行构建的时候,会一直报错
[Pipeline] tool [Pipeline] sh + /usr/local/maven/apache-maven-3.6.1/bin/mvn clean package -DskipTests The JAVA_HOME environment variable is not defined correctly This environment variable is needed to run this program NB: JAVA_HOME should point to a JDK not a JRE [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: script returned exit code 1 Finished: FAILURE
然后在salve1节点一直排查,jdk的环境变量没有问题,mvn -v执行正常,
[root@node5 test1-sonar-service_TEST]# /usr/local/maven/apache-maven-3.6.1/bin/mvn clean package -DskipTests也执行成功
[INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.example.app:maven-app >---------------------- [INFO] Building maven-app 1.3-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-app --- [INFO] Deleting /var/jenkins/workspace/test1-sonar-service_TEST/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /var/jenkins/workspace/test1-sonar-service_TEST/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /var/jenkins/workspace/test1-sonar-service_TEST/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /var/jenkins/workspace/test1-sonar-service_TEST/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /var/jenkins/workspace/test1-sonar-service_TEST/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-app --- [INFO] Building jar: /var/jenkins/workspace/test1-sonar-service_TEST/target/maven-app-1.3-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.030 s [INFO] Finished at: 2019-04-24T23:59:46+08:00
但是构建一直失败,卡了三四个小时,后来重启了master和slave节点,再一次构建执行成功,是个大坑
成功输出结果
Started by user admin Obtained sonar.jenkinsfile from git http://172.25.254.131/tester/mvn-test01.git Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on slave1 in /var/jenkins/workspace/test1-sonar-service_TEST@2 [Pipeline] { [Pipeline] checkout No credentials specified Fetching changes from the remote Git repository > git rev-parse --is-inside-work-tree # timeout=10 > git config remote.origin.url http://172.25.254.131/tester/mvn-test01.git # timeout=10 Fetching upstream changes from http://172.25.254.131/tester/mvn-test01.git > git --version # timeout=10 > git fetch --tags --progress http://172.25.254.131/tester/mvn-test01.git +refs/heads/*:refs/remotes/origin/* Checking out Revision 401249b8852695b6c9e1ed1cfea359101daffab4 (refs/remotes/origin/master) > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 > git config core.sparsecheckout # timeout=10 > git checkout -f 401249b8852695b6c9e1ed1cfea359101daffab4 Commit message: "Update sonar.jenkinsfile" [Pipeline] tool [Pipeline] sh > git rev-list --no-walk 401249b8852695b6c9e1ed1cfea359101daffab4 # timeout=10 + /usr/local/maven/apache-maven-3.6.1/bin/mvn clean package -DskipTests [INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.example.app:maven-app >---------------------- [INFO] Building maven-app 1.3-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-app --- [INFO] Deleting /var/jenkins/workspace/test1-sonar-service_TEST@2/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /var/jenkins/workspace/test1-sonar-service_TEST@2/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /var/jenkins/workspace/test1-sonar-service_TEST@2/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /var/jenkins/workspace/test1-sonar-service_TEST@2/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /var/jenkins/workspace/test1-sonar-service_TEST@2/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-app --- [INFO] Building jar: /var/jenkins/workspace/test1-sonar-service_TEST@2/target/maven-app-1.3-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.972 s [INFO] Finished at: 2019-04-24T23:58:36+08:00 [INFO] ------------------------------------------------------------------------ [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
3.2 结合sonar
修改sonar.jenkinsfile
#!/usr/bin/groovy node("${nodeSlave}"){ //checkout checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://172.25.254.131/tester/mvn-test01.git']]]) //build def mvnHome = tool 'M2_HOME' sh "${mvnHome}/bin/mvn ${buildShell}" //Sonar sh """ /usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=${serviceName} -Dsonar.projectName=${serviceName} -Dsonar.login=6f7a4c06ef06a8be9799097024c8f2f6fe2fafe7 -Dsonar.sources=src -Dsonar.host.url=${sonarServer} """ }
构建也顺利完成
Started by user admin Obtained sonar.jenkinsfile from git http://172.25.254.131/tester/mvn-test01.git Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on slave1 in /var/jenkins/workspace/test1-sonar-service_TEST [Pipeline] { [Pipeline] checkout No credentials specified Fetching changes from the remote Git repository Checking out Revision da77bf327b6d4c057ba469fdd5189f78ab6d850c (refs/remotes/origin/master) > git rev-parse --is-inside-work-tree # timeout=10 > git config remote.origin.url http://172.25.254.131/tester/mvn-test01.git # timeout=10 Fetching upstream changes from http://172.25.254.131/tester/mvn-test01.git > git --version # timeout=10 > git fetch --tags --progress http://172.25.254.131/tester/mvn-test01.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Commit message: "Update sonar.jenkinsfile" [Pipeline] tool [Pipeline] sh + /usr/local/maven/apache-maven-3.6.1/bin/mvn clean package -DskipTests > git config core.sparsecheckout # timeout=10 > git checkout -f da77bf327b6d4c057ba469fdd5189f78ab6d850c > git rev-list --no-walk 401249b8852695b6c9e1ed1cfea359101daffab4 # timeout=10 [INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.example.app:maven-app >---------------------- [INFO] Building maven-app 1.3-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-app --- [INFO] Deleting /var/jenkins/workspace/test1-sonar-service_TEST/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /var/jenkins/workspace/test1-sonar-service_TEST/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /var/jenkins/workspace/test1-sonar-service_TEST/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /var/jenkins/workspace/test1-sonar-service_TEST/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /var/jenkins/workspace/test1-sonar-service_TEST/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-app --- [INFO] Building jar: /var/jenkins/workspace/test1-sonar-service_TEST/target/maven-app-1.3-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.938 s [INFO] Finished at: 2019-04-25T00:04:51+08:00 [INFO] ------------------------------------------------------------------------ [Pipeline] sh + /usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=test1-sonar-service -Dsonar.projectName=test1-sonar-service -Dsonar.login=6f7a4c06ef06a8be9799097024c8f2f6fe2fafe7 -Dsonar.sources=src -Dsonar.host.url=http://172.25.254.133:9000 INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties INFO: Project root configuration file: NONE INFO: SonarQube Scanner 3.3.0.1492 INFO: Java 1.8.0_121 Oracle Corporation (64-bit) INFO: Linux 3.10.0-693.el7.x86_64 amd64 INFO: User cache: /root/.sonar/cache INFO: SonarQube server 6.7.7 INFO: Default locale: "en_US", source code encoding: "UTF-8" INFO: Publish mode INFO: Load global settings INFO: Load global settings (done) | time=172ms INFO: Server id: A623D34D-AWoVn6_8P1KovjAYWYot INFO: User cache: /root/.sonar/cache INFO: Load plugins index INFO: Load plugins index (done) | time=340ms INFO: Download sonar-l10n-zh-plugin-1.19.jar INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2. INFO: Download sonar-python-plugin-1.13.0.2922.jar INFO: Process project properties INFO: Load project repositories INFO: Load project repositories (done) | time=59ms INFO: Load quality profiles INFO: Load quality profiles (done) | time=21ms INFO: Load active rules INFO: Load active rules (done) | time=81ms INFO: Load metrics repository INFO: Load metrics repository (done) | time=27ms INFO: Project key: test1-sonar-service INFO: ------------- Scan test1-sonar-service INFO: Load server rules INFO: Load server rules (done) | time=62ms INFO: Base dir: /var/jenkins/workspace/test1-sonar-service_TEST INFO: Working dir: /var/jenkins/workspace/test1-sonar-service_TEST/.scannerwork INFO: Source paths: src INFO: Source encoding: UTF-8, default locale: en_US INFO: Index files INFO: 2 files indexed INFO: Sensor Zero Coverage Sensor INFO: Sensor Zero Coverage Sensor (done) | time=11ms INFO: Sensor CPD Block Indexer INFO: Sensor CPD Block Indexer (done) | time=0ms INFO: SCM Publisher is disabled INFO: Calculating CPD for 0 files INFO: CPD calculation finished INFO: Analysis report generated in 46ms, dir size=1 KB INFO: Analysis reports compressed in 4ms, zip size=1 KB INFO: Analysis report uploaded in 40ms INFO: ANALYSIS SUCCESSFUL, you can browse http://172.25.254.133:9000/dashboard/index/test1-sonar-service 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://172.25.254.133:9000/api/ce/task?id=AWpQF7VgmO3e1qpxfNZn INFO: Task total time: 1.585 s INFO: ------------------------------------------------------------------------ INFO: EXECUTION SUCCESS INFO: ------------------------------------------------------------------------ INFO: Total time: 3.742s INFO: Final Memory: 12M/90M INFO: ------------------------------------------------------------------------ [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
四、现有优化
4.1 修改sonar.jenkinsfile
#!/usr/bin/groovy String skipSonar = "${env.skipSonar}" String skipTset = "${env.skipTest}" node("${nodeSlave}"){ //checkout stage 'checkout' checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://172.25.254.131/tester/mvn-test01.git']]]) //build stage 'Build' if ("${skipTest}" == 'true'){ mvnHome = tool 'M2_HOME' sh "${mvnHome}/bin/mvn ${buildShell} -DskipTests" } else{ sh "${mvnHome}/bin/mvn ${buildShell}" } //Sonar stage 'Sonar' sh """ /usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=${serviceName} -Dsonar.projectName=${serviceName} -Dsonar.login=6f7a4c06ef06a8be9799097024c8f2f6fe2fafe7 -Dsonar.sources=src -Dsonar.host.url=${sonarServer} """ }
4.2 jenkins加几个开关
任务配置
4.3 开始构建
4.4 执行结果
Started by user admin Obtained sonar.jenkinsfile from git http://172.25.254.131/tester/mvn-test01.git Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on slave1 in /var/jenkins/workspace/test1-sonar-service_TEST [Pipeline] { [Pipeline] stage (checkout) Using the ‘stage’ step without a block argument is deprecated Entering stage checkout Proceeding [Pipeline] checkout No credentials specified Fetching changes from the remote Git repository Checking out Revision ae3a97f638dd6827893927b958f0996c810a5b5c (refs/remotes/origin/master) Commit message: "Update sonar.jenkinsfile" [Pipeline] stage (Build) Using the ‘stage’ step without a block argument is deprecated Entering stage Build Proceeding [Pipeline] tool [Pipeline] sh + /usr/local/maven/apache-maven-3.6.1/bin/mvn clean package -DskipTests > git rev-parse --is-inside-work-tree # timeout=10 > git config remote.origin.url http://172.25.254.131/tester/mvn-test01.git # timeout=10 Fetching upstream changes from http://172.25.254.131/tester/mvn-test01.git > git --version # timeout=10 > git fetch --tags --progress http://172.25.254.131/tester/mvn-test01.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 > git config core.sparsecheckout # timeout=10 > git checkout -f ae3a97f638dd6827893927b958f0996c810a5b5c > git rev-list --no-walk 08386a6b4836d2e277eca119c1651df0158ed964 # timeout=10 [INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.example.app:maven-app >---------------------- [INFO] Building maven-app 1.3-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-app --- [INFO] Deleting /var/jenkins/workspace/test1-sonar-service_TEST/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /var/jenkins/workspace/test1-sonar-service_TEST/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /var/jenkins/workspace/test1-sonar-service_TEST/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /var/jenkins/workspace/test1-sonar-service_TEST/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-app --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /var/jenkins/workspace/test1-sonar-service_TEST/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-app --- [INFO] Building jar: /var/jenkins/workspace/test1-sonar-service_TEST/target/maven-app-1.3-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.321 s [INFO] Finished at: 2019-04-25T09:16:36+08:00 [INFO] ------------------------------------------------------------------------ [Pipeline] stage (Sonar) Using the ‘stage’ step without a block argument is deprecated Entering stage Sonar Proceeding [Pipeline] sh + /usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=test1-sonar-service -Dsonar.projectName=test1-sonar-service -Dsonar.login=6f7a4c06ef06a8be9799097024c8f2f6fe2fafe7 -Dsonar.sources=src -Dsonar.host.url=http://172.25.254.133:9000 INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties INFO: Project root configuration file: NONE INFO: SonarQube Scanner 3.3.0.1492 INFO: Java 1.8.0_121 Oracle Corporation (64-bit) INFO: Linux 3.10.0-693.el7.x86_64 amd64 INFO: User cache: /root/.sonar/cache INFO: SonarQube server 6.7.7 INFO: Default locale: "en_US", source code encoding: "UTF-8" INFO: Publish mode INFO: Load global settings INFO: Load global settings (done) | time=61ms INFO: Server id: A623D34D-AWoVn6_8P1KovjAYWYot INFO: User cache: /root/.sonar/cache INFO: Load plugins index INFO: Load plugins index (done) | time=40ms INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2. INFO: Process project properties INFO: Load project repositories INFO: Load project repositories (done) | time=77ms INFO: Load quality profiles INFO: Load quality profiles (done) | time=25ms INFO: Load active rules INFO: Load active rules (done) | time=111ms INFO: Load metrics repository INFO: Load metrics repository (done) | time=42ms INFO: Project key: test1-sonar-service INFO: ------------- Scan test1-sonar-service INFO: Load server rules INFO: Load server rules (done) | time=83ms INFO: Base dir: /var/jenkins/workspace/test1-sonar-service_TEST INFO: Working dir: /var/jenkins/workspace/test1-sonar-service_TEST/.scannerwork INFO: Source paths: src INFO: Source encoding: UTF-8, default locale: en_US INFO: Index files INFO: 2 files indexed INFO: Sensor Zero Coverage Sensor INFO: Sensor Zero Coverage Sensor (done) | time=23ms INFO: Sensor CPD Block Indexer INFO: Sensor CPD Block Indexer (done) | time=0ms INFO: SCM Publisher is disabled INFO: Calculating CPD for 0 files INFO: CPD calculation finished INFO: Analysis report generated in 54ms, dir size=1 KB INFO: Analysis reports compressed in 7ms, zip size=1 KB INFO: Analysis report uploaded in 128ms INFO: ANALYSIS SUCCESSFUL, you can browse http://172.25.254.133:9000/dashboard/index/test1-sonar-service 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://172.25.254.133:9000/api/ce/task?id=AWpSENV2mO3e1qpxfNZo INFO: Task total time: 1.702 s INFO: ------------------------------------------------------------------------ INFO: EXECUTION SUCCESS INFO: ------------------------------------------------------------------------ INFO: Total time: 3.006s INFO: Final Memory: 8M/92M INFO: ------------------------------------------------------------------------ [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS