Jenkins实战先看这:Jenkins 集成Junit单元测试+Jacoco统计覆盖率

版本信息:

Jenkins:2.190.1
jdk:1.8.0_231
maven:3.6.2

需求:

通过Jenkins构建 运行单元测试并统计覆盖率

遇到的问题:

流水线语法脚本;(文档学习)
Jenkins mvn not found;(jerkin环境变量添加maven路径)
NullPointerException in Jenkins;(wrap{}语法不支持)
Error cloning remote repo ‘origin’;(脚本书写错误)
There is insufficient memory for the Java Runtime Environment to continue;(整理服务器资源,关停不常用docker容器)
Could not initialize class jackson.databind.deser.std.JdkDeserializers;删除core下 所有_remote文件未果;更换jdk版本为:jdk1.8.0_231未果;修改pom依赖,删除具体版本,添加到spring-cloud-starter-config中,如下
java.lang.ArrayIndexOutOfBoundsException;(本地和QA未复现,@Ignore暂时忽略)
Step ‘Publish JUnit test result report’ failed:No test report files were found. Configuration error?Finished: FAILURE;(target/**/*.xml)
Test reports were found but none of them are new. Did leafNodes run?(sh “touch *.xml”)
忽略单元测试中的错误继续构建工程($ mvn test -Dmaven.test.failure.ignore=true)
Failed to read test report file;(touch+reRun )

主要实施步骤:

  • Jenkins安装Jacoco插件
  • 新建任务
  • 配置参数化构建选项参数、文本参数
  • 添加可配置单选某个单元测试
  • 编写PipeLine script 脚本

参考文章:

Jenkins官方文档:
https://www.jenkins.io/zh/doc/pipeline/tour/tests-and-artifacts/
Jacoco官方文档:https://www.eclemma.org/jacoco/trunk/doc/agent.html
Jenkins单元测试(上):https://cloud.tencent.com/developer/article/1831413
Jenkins单元测试(下):
https://cloud.tencent.com/developer/article/1831414?from=article.detail.1831413
Jenkins 2.X Script Pipeline语法基础:
https://cloud.tencent.com/developer/article/1541807

其他参考:

Jenkins集成JaCoCo和SonarQube检查代码测试覆盖率:
https://www.shangmayuan.com/a/f398d0a4564e4189a9c423b3.html

流水线脚本

    node {
    try {
        stage('Get Code') {
        }
        
        stage('Run Unit Test') {
            script{
                echo "-------------------- begin unitest --------------------"
                
                // sh 'mvn org.jacoco:jacoco-maven-plugin:prepare-agent clean test -Dautoconfig.skip=true'
                // sh "touch target/surefire-reports/*.xml"
                sh 'mvn org.jacoco:jacoco-maven-plugin:prepare-agent clean ${test_name} test -Dautoconfig.skip=true'
                
                junit 'target/surefire-reports/*.xml'

                echo "-------------------- end unitest --------------------"
            }
            
            jacoco(
                //代码覆盖率统计文件位置,Ant风格路径表达式
                execPattern: 'target/**/*.exec', 
                // classes 文件位置,Ant风格路径表达式
                classPattern: 'target/classes',
                //源码文件位置,Ant风格路径表达式
                sourcePattern: 'src/main/java',
                //排除分析的位置,Ant风格路径表达式
                exclusionPattern: 'src/test*',
                //是否禁用每行覆盖率的源文件显示
                skipCopyOfSrcFiles: false,
                //如果为true,则对各维度的覆盖率进行比较。如果任何一个维度的当前覆盖率小于最小覆盖率阈值,则构建状态为失败
                //如果当前覆盖率在最大阈值和最小阈值之间,则当前构建状态为不稳定;如果当前覆盖率大于最大阈值,则构建成功
                changeBuildStatus: true,
                //字节码指令覆盖率
                minimumInstructionCoverage: '30' , maximumInstructionCoverage: '70',
                //行覆盖率
                minimumLineCoverage: '30' ,maximumLineCoverage: '70',
                //圈复杂度覆盖率
                minimumComplexityCoverage: '30' ,maximumComplexityCoverage:'70', 
                //方法覆盖率
                minimumMethodCoverage: '30' , maximumMethodCoverage:'70',
                //类覆盖率
                minimumClassCoverage: '30',maximumClassCoverage: '70',
                //分支覆盖率
                minimumBranchCoverage: '30' ,maximumBranchCoverage:'70',
                //如果为true,则只有所有维度的覆盖率变化量的绝对值小于相应的变化量阈值时,构建结果才为成功
                buildOverBuild: true,
                //以下是各个维度覆盖率的变化量阈值
                deltaInstructionCoverage: '80',deltaLineCoverage: '80',
                deltaMethodCoverage: '0' ,deltaClassCoverage: '80',
                deltaComplexityCoverage: '80',deltaBranchCoverage: '80'
            )
        
        }
    } catch (Exception e) {
        throw e
    }
}

总结:
遇到了很多坑,最后折到了版本不兼容上,前期需要调研好与项目兼容的包,另外解决问题时要多借鉴前人的脚步~

你可能感兴趣的:(JAVA天地,运维,Jenkins,Junit,Jcoco)