列出Jenkins过去一年的工作构建细节以及触发构建的用户 [英] List Jenkins job build detials for last one year along with the user who triggered the build

查看:65
本文介绍了列出Jenkins过去一年的工作构建细节以及触发构建的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何简单的方法可以使用API​​或脚本来获得过去一年在所有作业上执行的所有构建以及作为报告触发该构建的用户的列表?

Is there any simple way to work with APIs or with scripting to get list of all builds performed on all jobs for last one year along with the user who triggered the build as a report?

推荐答案

这应该可以.从<JENKINS_URL>/script或在具有"执行系统Groovy脚本"的Jenkins作业中运行; (不是"Execute Groovy脚本").

This should do. Run from <JENKINS_URL>/script or in a Jenkins job with an "Execute System Groovy Script" (not an "Execute Groovy script").

已更新:包含主题行中的详细信息.

Updated: to include details from the subject line.

def jobNamePattern ='.*'   // adjust to folder/job regex as needed
def daysBack = 365   // adjust to how many days back to report on
def timeToDays = 24*60*60*1000  // converts msec to days

println "Job Name: ( # builds: last ${daysBack} days / overall )  Last Status\n   Number | Trigger | Status | Date | Duration\n"

Jenkins.instance.allItems.findAll() {
  it instanceof hudson.model.FreeStyleProject && it.fullName.matches(jobNamePattern)
}.each { job ->
  builds = job.getBuilds().byTimestamp(System.currentTimeMillis() - daysBack*timeToDays, System.currentTimeMillis())
  println job.fullName + ' ( ' + builds.size() + ' / ' + job.builds.size() + ' )  ' + job.getLastBuild()?.result
  
  // individual build details
  builds.each { build ->
    println '   ' + build.number + ' | ' + build.getCauses()[0].getShortDescription() + ' | ' + build.result + ' | ' + build.getTimestampString2() + ' | ' + build.getDurationString()
  }
}
return

样本输出

ITSuppt/sampleApplication ( 4 / 11 )  SUCCESS
   13 | Started by user Ian W | SUCCESS | 2020-10-22T01:57:58Z | 30 sec
   12 | Started by user Ian W | FAILURE | 2020-10-22T01:51:36Z | 45 sec
   11 | Started by user Ian W | SUCCESS | 2020-10-15T18:26:22Z | 29 sec
   10 | Started by user Ian W | FAILURE | 2020-10-15T18:14:13Z | 55 sec

如果您有大量的工作和构建,可能会花费很长时间,因此您可能希望限制跳过详细信息以开始或使用工作模式名称. 构建Javadoc 以获取更多信息.

It could take a long time if you have a lot of jobs and builds, so you might want to restrict to skip the details to start or use a job pattern name. Build Javadoc for additional info.

或者,根据此 S/O答案,您可以从Jenkins获取所有作业的所有构建的构建详细信息REST API(其他示例其他地方).

Or, according to this S/O answer, you can Get build details for all builds of all jobs from Jenkins REST API (additional examples elsewhere).

这篇关于列出Jenkins过去一年的工作构建细节以及触发构建的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆