且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-04 08:37:46

这应该可以.从 /script 或使用执行系统 ​​Groovy 脚本"在 Jenkins 作业中运行(不是执行 Groovy 脚本").

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

def jobNamePattern ='.*'//根据需要调整到文件夹/作业正则表达式def daysBack = 365//调整到返回报告的天数def timeToDays = 24*60*60*1000//将毫秒转换为天println "Job Name: ( # builds: last ${daysBack} days/total ) Last Status\n Number |触发 |状态 |日期 |持续时间\n"Jenkins.instance.allItems.findAll() {it instanceof hudson.model.FreeStyleProject &&it.fullName.matches(jobNamePattern)}.每个{作业->builds = job.getBuilds().byTimestamp(System.currentTimeMillis() - daysBack*timeToDays, System.currentTimeMillis())println job.fullName + ' ( ' + builds.size() + '/' + job.builds.size() + ' ) ' + job.getLastBuild()?.result//单独的构建细节builds.each { build ->println ' ' + build.number + ' |' + build.getCauses()[0].getShortDescription() + ' |' + build.result + ' |' + build.getTimestampString2() + ' |' + build.getDurationString()}}返回

样本输出

ITSuppt/sampleApplication ( 4/11 ) 成功13 |由用户 Ian W 发起 |成功 |2020-10-22T01:57:58Z |30 秒12 |由用户 Ian W 发起 |失败 |2020-10-22T01:51:36Z |45 秒11 |由用户 Ian W 发起 |成功 |2020-10-15T18:26:22Z |29 秒10 |由用户 Ian W 发起 |失败 |2020-10-15T18:14:13Z |55 秒

如果您有很多作业和构建,可能需要很长时间,因此您可能希望限制跳过详细信息以启动或使用作业模式名称.构建 Javadoc 了解更多信息.

或者,根据此 S/O 答案,您可以从 Jenkins 获取所有作业的所有构建的构建详细信息REST 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?

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

Sample Output

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

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.

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).