managejenkinspipeline整合jacoco统计单元测试覆盖率⼀ 前提条件
⾸先需要在jenkins中安装jacoco插件
在Manage plugins配置项⾥⾯搜索jacoco,安装 插件
在maven的pom⽂件中加⼊插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
harbin weather
<!--表⽰执⾏任何⼦⽬录下所有命名以Test结尾的java类 -->gentleness
<includes>
<include>**/*Test*.java</include>
</includes>
</configuration>
</plugin>
maven的这个插件会在target下⽣成测试报告
注意:java的单元测试注解必须是org.junit.Test,否则将⽣成不了报告,maven识别不了
最后配置流⽔线
agent any
tools {
jdk 'jdk1.8'
}
stages {
stage('Checkout'){
steps{
echo "starting fetchCode from ${poUrl}......"
// Get some code from a GitHub repository
// git branch: '${GIT_BRANCH}', credentialsId:'d82aaf65-3389-4c65-aa6f-1e98be7c31a4', url: '${GIT_URL}' }
}
stage('单元测试') {
steps {
memory意思echo ""
//注⼊jacoco插件配置,clean test执⾏单元测试代码. All tests should pass.
bat "mvn org.jacoco:jacoco-maven-plugin:prepare-agent -f ${pomPath} clean test -Dautoconfig.skip=true -st.skip=fal -st.failure.ignore=true"
junit '**/target/surefire-reports/*.xml'
//配置单元测试覆盖率要求,未达到要求pipeline将会fail,code coverage.LineCoverage>20%.
jacoco changeBuildStatus: true, maximumLineCoverage:"20"
}
}
stage('部署测试环境') {
steps {
欧姆接触
echo ""
//编译和打包
bat "mvn -f ${pomPath} clean package -Dautoconfig.skip=true -st.skip=true"
echo "end of building ......"
}
}
}
}
脚本中的⼀些参数是在流⽔线的参数中配置好的,可以参考之前的⽂章。
实际应⽤中,我们可能会吧脚本在程序后端动态⽣成,这样就可以动态的控制流⽔线了,
这需要通过http远程加载脚本
需要在jenkins安装插件,Http Request Plugin
安装后在具体的pipeline的jenkinsfile中写⼊
def respon = httpRequest contentType:'APPLICATION_JSON',
httpMode:"GET",
url:"localhost:9001/jenkins/project/getScript?jobName=allen-jenkins",
outputFile: "Jenkinsfile"
println "Write jenkins file done"
load "Jenkinsfile"
}
这个就是从远端加载脚本并执⾏,
远端返回的就是⼀个字符串
bossanovapipeline {
agent any
tools {
jdk 'jdk1.8'
}
stages {
stage('Checkout'){
steps{
echo "starting fetchCode from ${poUrl}......"
// Get some code from a GitHub repository
// git branch: 'master', credentialsId:'d82aaf65-3389-4c65-aa6f-1e98be7c31a4', url:
'git.synnex/scm/onetool/monitortool.git'
}
}
肯塔基大学排名stage('单元测试') {
steps {
echo"work place is ${WORKSPACE}"
echo ""
//注⼊jacoco插件配置,clean test执⾏单元测试代码. All tests should pass.
bat "mvn org.jacoco:jacoco-maven-plugin:prepare-agent -f l clean test -
Dautoconfig.skip=true -st.skip=fal -st.failure.ignore=true"
junit '**/target/surefire-reports/*.xml'
//配置单元测试覆盖率要求,未达到要求pipeline将会fail,code coverage.LineCoverage>20%.
jacoco changeBuildStatus: true, maximumLineCoverage:"20"
echo "jar path is ${WORKSPACE}/target/"
}
}
开衣服店}
}
中间将⼀些参数进⾏了替换:
遇到的问题:
有时候通过远程加载脚本和直接运⾏脚本,他们的WORKPLACE可能不⼀样,这样容易导致mvn构建的时候找不到pom⽂件,这个问题当我们在脚本中加⼊重新拉去代码的逻辑后,⼜正常了
伊尔维萨克>ver