groovy 列出过去一年的Jenkins作业构建详细信息沿着触发构建的用户

vtwuwzda  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(182)

是否有简单的方法使用API或脚本来获取过去一年在所有作业上执行的所有构建的列表,沿着触发构建的用户作为报告?

643ylb08

643ylb081#

这应该可以。从<JENKINS_URL>/script或在Jenkins作业中使用“执行系统Groovy脚本”(而不是“执行Groovy脚本”)运行。

**更新:**包括主题行的详细信息。

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 Job && 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

如果您有很多作业和构建,这可能需要很长时间,因此您可能希望限制跳过详细信息以开始或使用作业模式名称. Build Javadoc以获取其他信息。
或者,根据这个S/O answer,你可以从Jenkins REST API获得所有作业的所有构建的构建细节(附加示例elsewhere)。

相关问题