写点什么

【DevOps】Jenkins 持续集成流水线(中)

用户头像
Man
关注
发布于: 2020 年 08 月 06 日
【DevOps】Jenkins持续集成流水线(中)

一、前言



承接上一篇: Jenkins持续集成流水线(上)



当我们的持续构建流水线的基本骨架构建完毕后,接下来我们要集成单元测试覆盖率(Jacoco)、静态代码扫描工具,通过一键构建并生成覆盖率报告、代码扫描报告。

二、集成静态代码扫描工具(FindBugs)



具体细节请参考我的另外一篇文章:Jenkins集成FindBugs

三、集成自动化单元测试(Jacoco)



1、前置条件



第一步:要在本地IDE的maven项目中集成Jacoco,具体请参考我的另外一篇文章:Maven项目与Jacoco覆盖率插件的集成



第二步:安装以下插件:JaCoCo plugin。

2、配置构建任务



第一步:在【构建设置】这里的Post-build Actions选择Record JaCoCo coverage report



第二步:然后有以下参数需要配置。

Path to exec files:用于指定jacoco.exec的路径(有时候是jacoco-unit.exec,这个取决于你当时在IDE上配置Jacoco时候在POM文件中如何配置jacoco这个plugin)

Inclusions:要包括的package

Exclusions:要排除的package

Path to class directories:编译后的二进制文件存放的地方,一般是target/classes

Path to source directories:源代码存放的地方 





3、触发构建



我在下面的构建日志中尽量把关键日志挑出来(Jacoco+Findbug),方便大家阅读(当然也省略很多其他日志)。

由用户 justyman 启动
Running as SYSTEM
构建中 在工作空间 D:\Tools\Jenkins\workspace\callcenter 中
using credential 75629e7a-7d86-4fc9-a5b9-8795e5234a97
> C:\Program Files (x86)\Git\cmd\git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> ......
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building callcenter 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] --- jacoco-maven-plugin:0.8.2:prepare-agent (jacoco-initialize) @ callcenter ---
[INFO] argLine set to -javaagent:D:\\software\\mvnRepository\\org\\jacoco\\org.jacoco.agent\\0.8.2\\org.jacoco.agent-0.8.2-runtime.jar=destfile=D:\\Tools\\Jenkins\\workspace\\callcenter\\target\\coverage-reports\\jacoco-unit.exec,includes=**/service/utils/**:**/controller/**
[INFO]......
[INFO] --- jacoco-maven-plugin:0.8.2:report (jacoco-site) @ callcenter ---
[INFO] Loading execution data file D:\Tools\Jenkins\workspace\callcenter\target\coverage-reports\jacoco-unit.exec
[INFO] Analyzed bundle 'callcenter' with 4 classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 40.226 s
[INFO] Finished at: 2020-08-01T15:49:41+08:00
[INFO] Final Memory: 54M/305M
[INFO] ------------------------------------------------------------------------
....
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ callcenter ---
[INFO] Deleting D:\Tools\Jenkins\workspace\callcenter\target
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.2:prepare-agent (jacoco-initialize) @ callcenter ---
[INFO] argLine set to -javaagent:D:\\software\\mvnRepository\\org\\jacoco\\org.jacoco.agent\\0.8.2\\org.jacoco.agent-0.8.2-runtime.jar=destfile=D:\\Tools\\Jenkins\\workspace\\callcenter\\target\\coverage-reports\\jacoco-unit.exec,includes=**/service/utils/**:**/controller/**
[INFO]
....
[INFO] --- findbugs-maven-plugin:3.0.5:findbugs (default-cli) @ callcenter ---
[INFO] Fork Value is true
[java] Warnings generated: 4
[INFO] Done FindBugs Analysis....
[WARNING] Attempt to (de-)serialize anonymous class hudson.plugins.analysis.core.HealthAwareReporter$2; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[WARNING] Attempt to (de-)serialize anonymous class hudson.plugins.analysis.core.HealthAwareReporter$3; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[FINDBUGS] Searching for all files in D:\Tools\Jenkins\workspace\callcenter\target that match the pattern findbugsXml.xml
[FINDBUGS] Parsing 1 file in D:\Tools\Jenkins\workspace\callcenter\target
[FINDBUGS] Successfully parsed file D:\Tools\Jenkins\workspace\callcenter\target\findbugsXml.xml of module callcenter with 4 unique warnings and 0 duplicates.
[WARNING] Attempt to (de-)serialize anonymous class hudson.plugins.analysis.core.HealthAwareReporter$1; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[FINDBUGS] Computing warning deltas based on reference build #16
......
[JaCoCo plugin] Collecting JaCoCo coverage data...
[JaCoCo plugin] **/target/coverage-reports/jacoco-unit.exec;**/target/classes;**/src/main/java; locations are configured
[JaCoCo plugin] Number of found exec files for pattern **/target/coverage-reports/jacoco-unit.exec: 0
[JaCoCo plugin] Saving matched execfiles:
[JaCoCo plugin] Saving matched class directories for class-pattern: **/target/classes:
[JaCoCo plugin] - D:\Tools\Jenkins\workspace\callcenter\target\classes 14 files
[JaCoCo plugin] Saving matched source directories for source-pattern: **/src/main/java:
[JaCoCo plugin] Source Inclusions: **/*.java
[JaCoCo plugin] Source Exclusions:
[JaCoCo plugin] - D:\Tools\Jenkins\workspace\callcenter\src\main\java 14 files
[JaCoCo plugin] Loading inclusions files..
[JaCoCo plugin] inclusions: [**/*.class]
[JaCoCo plugin] exclusions: []
[JaCoCo plugin] Thresholds: JacocoHealthReportThresholds [minClass=0, maxClass=0, minMethod=0, maxMethod=0, minLine=0, maxLine=0, minBranch=0, maxBranch=0, minInstruction=0, maxInstruction=0, minComplexity=0, maxComplexity=0]
[JaCoCo plugin] Publishing the results..
[JaCoCo plugin] Loading packages..
[JaCoCo plugin] Done.
[JaCoCo plugin] Overall coverage: class: 0, method: 0, line: 0, branch: 0, instruction: 0
SSH: Connecting from host [DESKTOP-7GNOQPJ]
SSH: Connecting with configuration [SIT_Server] ...



当上面的构建完成后,就可以有Findbug报告和Jacoco单测覆盖率报告出来了(如下面三个图)。



  • Jacoco覆盖率报告会展示不同维度的覆盖率,如语句覆盖率、分支覆盖率、方法覆盖率、类覆盖率(具体不同覆盖率的含义可以参考我的另外一篇文章《SonarQube 指标定义》

  • Findbug报告则可以展示具体每个类的不合乎Findbug要求的不合规项出来。









四、后话



这一节主要展现了在代码构建环节中执行自动化单元测试及静态代码扫描这两项检测工作,也是属于我的DevOps流水线中的质量关卡的其中一环(后面我会专题讲一下质量关卡这个事情)。



帮自己先挖个坑吧,后面会探讨一下如何把基于YAPI的自动化接口测试集成到Jenkins(我的理解也算是质量关卡的一部分),然后如何把所有的构建元数据整合到一个Portal用于做类似管理驾驶舱之类的用途。

发布于: 2020 年 08 月 06 日阅读数: 88
用户头像

Man

关注

尘世间一名迷途小码农 2020.06.24 加入

1、致力于成为一名DevOps Geek,热衷于用技术方式去解决问题,厌恶低效,热衷自动化和智能化,释放人的创造性。 2、CSDN博客:https://blog.csdn.net/justyman

评论

发布
暂无评论
【DevOps】Jenkins持续集成流水线(中)