您可能知道,Jenkins是高度可配置的CI服务器。 我们可以设置不同的自定义构建过程。 我将分享一些我用来设置Jenkins工作层次的方法。 这是用例:
- 我们有一个主入口作业被调用以启动整个构建过程。
- 这项工作可以有一个到多个子工作。 (第2级)
- 每个子工作也可以有一个到多个子工作。 (第3级)
- 主要思想是将所有儿童工作的身份收集/汇总到主要入境工作中。
- Main_Job是单入口作业,必须包含所有同级作业状态
- Job_Set_1和Job_Set_2是两个子作业
- Build_ [1-4]是Job_Set_1的子作业
- Tests_ [1-3]是Job_Set_2的子作业
现在,我们应该使用一些Jenkins插件来构建这样的层次结构。 我一直在使用Multijob Plugin 。 请参阅以下配置的屏幕截图:
Main_Job /配置
Job_Set_1 /配置
Job_Set_2 / configure
但是,这还不够。 我们必须将所有兄弟作业的状态汇总到Main_Job中 。 我一直在使用修补的Groovy Postbuild插件 。 修改后的插件位于https://github.com/halyph/groovy-postbuild-plugin Groovy Postbuild插件允许我们为作业的Post-build Actions编写Groovy脚本。 该脚本可以访问整个Jenkins环境,并且可以使用所有已安装的Jenkins插件的功能:
这是此脚本的来源:
import hudson.model.*
import com.tikal.jenkins.plugins.multijob.*;
void log(msg) {
manager.listener.logger.println(msg)
}
threshold = Result.SUCCESS
void aggregate_results() {
failed = false
mainJob = manager.build.getProject().getName()
job = hudson.model.Hudson.instance.getItem(mainJob)
log '-------------------------------------------------------------------------------------'
log 'Aggregated status report'
log '-------------------------------------------------------------------------------------'
log('${mainJob} #${manager.build.getNumber()} - ${manager.build.getResult()}')
job.getLastBuild().getSubBuilds().each { subBuild->
subJob = subBuild.getJobName()
subJobNumber = subBuild.getBuildNumber()
job = hudson.model.Hudson.instance.getItem(subBuild.getJobName())
log '${subJob} #${subJobNumber} - ${job.getLastCompletedBuild().getResult()}'
log job.getLastCompletedBuild().getLog()
//println subBuild
dePhaseJob = hudson.model.Hudson.instance.getItem(subBuild.getJobName())
dePhaseJobBuild = dePhaseJob.getBuildByNumber(subBuild.getBuildNumber())
dePhaseJobBuild.getSubBuilds().each { childSubBuild ->
try {
log ' ${childSubBuild.jobName}'
job = hudson.model.Hudson.instance.getItem(childSubBuild.getJobName())
build = job.getBuildByNumber(childSubBuild.getBuildNumber())
indent = ' '
log '${indent} #${build.getNumber()} - ${build.getResult()}'
log build.getLog()
if(!failed && build.getResult().isWorseThan(threshold) ) {
failed = true
}
} catch (Exception e) {
log('ERROR: ${e.getMessage()}')
failed = true
}
}
}
if(failed) {manager.build.setResult(hudson.model.Result.FAILURE)}
}
try {
aggregate_results()
} catch(Exception e) {
log('ERROR: ${e.message}')
log('ERROR: Failed Status report aggregation')
manager.build.setResult(hudson.model.Result.FAILURE)
}
您也可以在GitHub Gist上找到它: https : //gist.github.com/4610274 。
这个脚本很傻。 它只是为每个子作业提取(在Main_Job控制台中打印)下一个信息:
- 工作名称
- 建立数字
- 生成结果(状态)
- 建立日志
此外,它还可以根据子作业的状态来设置Main_Job状态。 请参阅脚本中的threshold
变量。 汇总报告如下所示:
Started by user anonymous
Building in workspace C:\Users\oivasiv\.jenkins\jobs\Main_Job\workspace
Starting build job Job_Set_1.
Finished Build : #8 of Job : Job_Set_1 with status :SUCCESS
Starting build job Job_Set_2.
Finished Build : #8 of Job : Job_Set_2 with status :SUCCESS
-------------------------------------------------------------------------------------
Aggregated status report
-------------------------------------------------------------------------------------
Main_Job #8 - SUCCESS
Job_Set_1 #8 - SUCCESS
Started by upstream project 'Main_Job' build number 8
Building in workspace C:\Users\oivasiv\.jenkins\jobs\Job_Set_1\workspace
Starting build job Build_1.
Starting build job Build_2.
Finished Build : #8 of Job : Build_1 with status :FAILURE
Finished Build : #8 of Job : Build_2 with status :SUCCESS
Starting build job Build_3.
Starting build job Build_4.
Finished Build : #6 of Job : Build_3 with status :UNSTABLE
Finished Build : #6 of Job : Build_4 with status :SUCCESS
Notifying upstream projects of job completion
Finished: SUCCESS
Build_1
#8 - FAILURE
Started by upstream project 'Job_Set_1' build number 8
Building in workspace C:\Users\oivasiv\.jenkins\jobs\Build_1\workspace
Setting result to [Failed]
Build step 'Set the build result' changed build result to FAILURE
Build step 'Set the build result' marked build as failure
Notifying upstream projects of job completion
Finished: FAILURE
Build_2
#8 - SUCCESS
Started by upstream project 'Job_Set_1' build number 8
Building in workspace C:\Users\oivasiv\.jenkins\jobs\Build_2\workspace
Setting result to [Success]
Build step 'Set the build result' changed build result to SUCCESS
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
null
Notifying upstream projects of job completion
Finished: SUCCESS
Build_3
#6 - UNSTABLE
Started by upstream project 'Job_Set_1' build number 8
Building in workspace C:\Users\oivasiv\.jenkins\jobs\Build_3\workspace
Setting result to [Unstable]
Build step 'Set the build result' changed build result to UNSTABLE
Notifying upstream projects of job completion
Finished: UNSTABLE
Build_4
#6 - SUCCESS
Started by upstream project 'Job_Set_1' build number 8
Building in workspace C:\Users\oivasiv\.jenkins\jobs\Build_4\workspace
Setting result to [Success]
Build step 'Set the build result' changed build result to SUCCESS
Notifying upstream projects of job completion
Finished: SUCCESS
Job_Set_2 #8 - SUCCESS
Started by upstream project 'Main_Job' build number 8
Building in workspace C:\Users\oivasiv\.jenkins\jobs\Job_Set_2\workspace
Starting build job Test_1.
Finished Build : #8 of Job : Test_1 with status :SUCCESS
Starting build job Test_2.
Finished Build : #8 of Job : Test_2 with status :UNSTABLE
Starting build job Test_3.
Finished Build : #8 of Job : Test_3 with status :SUCCESS
Notifying upstream projects of job completion
Finished: SUCCESS
Test_1
#8 - SUCCESS
Started by upstream project 'Job_Set_2' build number 8
Building in workspace C:\Users\oivasiv\.jenkins\jobs\Test_1\workspace
Setting result to [Success]
Build step 'Set the build result' changed build result to SUCCESS
Notifying upstream projects of job completion
Finished: SUCCESS
Test_2
#8 - UNSTABLE
Started by upstream project 'Job_Set_2' build number 8
Building in workspace C:\Users\oivasiv\.jenkins\jobs\Test_2\workspace
Setting result to [Unstable]
Build step 'Set the build result' changed build result to UNSTABLE
Notifying upstream projects of job completion
Finished: UNSTABLE
Test_3
#8 - SUCCESS
Started by upstream project 'Job_Set_2' build number 8
Building in workspace C:\Users\oivasiv\.jenkins\jobs\Test_3\workspace
Setting result to [Success]
Build step 'Set the build result' changed build result to SUCCESS
Notifying upstream projects of job completion
Finished: SUCCESS
Build step 'Groovy Postbuild' changed build result to FAILURE
Build step 'Groovy Postbuild' marked build as failure
Notifying upstream projects of job completion
Finished: FAILURE
因此,此类作业状态聚合和Groovy后处理有什么好处:
- 我们在一个地方拥有工作状态
- 从Main_Job发送的电子邮件通知包含此状态
- 实现任何自定义后处理逻辑的能力
- 可以通过Groovy Scrip重用其他插件功能
参考:来自JCG合作伙伴 Orest Ivasiv的Jenkins分层作业和作业状态汇总 ,在Knowledge Is Everything博客上。
翻译自: https://www.javacodegeeks.com/2013/02/jenkins-hierarchical-jobs-and-jobs-status-aggregation.html