Jenkins分层作业和作业状态汇总

本文介绍了如何使用Jenkins创建分层作业,并通过Multijob Plugin和修改过的Groovy Postbuild Plugin来汇总和展示各个子作业的状态。主要目标是使主作业能够收集所有子作业的状态,并提供统一的视图,方便管理和邮件通知。
摘要由CSDN通过智能技术生成

您可能知道,Jenkins是高度可配置的CI服务器。 我们可以设置不同的自定义构建过程。 我将分享一些我用来设置Jenkins工作层次的方法。 这是用例:

  1. 我们有一个主入口作业被调用以启动整个构建过程。
  2. 这项工作可以有一个到多个子工作。 (第2级)
  3. 每个子工作也可以有一个到多个子工作。 (第3级)
  4. 主要思想是将所有儿童工作的身份收集/汇总到主要入境工作中。



请参见下面的示例屏幕快照,该示例可视化了总体思路。 这里:

  • Main_Job是单入口作业,必须包含所有同级作业状态
  • Job_Set_1Job_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值