简体   繁体   English

导出 jenkins 作业的构建历史

[英]Export build history of jenkins job

Can I export, in any conventional file format, a history of builds, with their time/date and success.我可以以任何传统文件格式导出构建历史,以及它们的时间/日期和成功。 And hopefully even promotion status.并希望甚至是促销状态。

You can make use of Jenkins rest api :您可以使用 Jenkins rest api :

  1. Start at : Traverse all jobs on your Jenkins Server using :开始于:使用以下命令遍历 Jenkins 服务器上的所有作业:
    http://JENKINS_URl/api/json?tree=jobs[name,url]
    This will give json response with all jobs with job name and job url.这将为所有具有作业名称和作业 url 的作业提供 json 响应。
  2. Then for each job access its builds using api :然后对于每个作业使用 api 访问其构建:
    http://JENKINS_URL/job/JOB_NAME/api/json?tree=allBuilds[number,url]
    This will give all the builds for job JOB_NAME and return json response with build number and build url.这将为作业 JOB_NAME 提供所有构建,并返回带有构建号和构建 url 的 json 响应。
  3. Now Traverse each build using api :现在使用 api 遍历每个构建:
    http://JENKINS_URL/job/JOB_NAME/BUILD_NUMBER/api/json
    This will give everything related to the build as json response.这将提供与构建相关的所有内容作为 json 响应。 Like Build status, how build was triggered, time etc.像构建状态,构建是如何触发的,时间等。

For automation, you can use bash, curl and jq to achieve this.对于自动化,您可以使用 bash、curl 和 jq 来实现这一点。

Have written small bash script to retrieve build status and timestamp for each job on Jenkins server :已经编写了小的 bash 脚本来检索 Jenkins 服务器上每个作业的构建状态和时间戳:

#!/bin/bash
JENKINS_URL=<YOUR JENKINS URL HERE>
for job in `curl -sg "$JENKINS_URL/api/json?tree=jobs[name,url]" | jq '.jobs[].name' -r`; 
do 
    echo "Job Name : $job"
    echo -e "Build Number\tBuild Status\tTimestamp"
    for build in `curl -sg "$JENKINS_URL/job/$job/api/json?tree=allBuilds[number]" | jq '.allBuilds[].number' -r`; 
    do 
        curl -sg "$JENKINS_URL/job/$job/$build/api/json" | jq '(.number|tostring) + "\t\t" + .result + "\t\t" + (.timestamp|tostring)' -r
    done 
    echo "================"
done

Note : Above script assumes that Jenkins server does not have any authentication.注意:以上脚本假设 Jenkins 服务器没有任何身份验证。 For authentication, add below parameter to each curl call :对于身份验证,将以下参数添加到每个 curl 调用:
-u username:API_TOKEN
Where :在哪里 :
username:API_TOKEN with your username and password/API_Token

Similar way you can export all build history in any format you want.类似的方式,您可以以您想要的任何格式导出所有构建历史记录。

Parvez' suggestion to use the REST API is perfectly fine. Parvez 的使用 REST API 的建议非常好。

However, the REST API is awkward to use if it does not directly provide the data you're looking for, leading to convoluted and multiple invocations of the REST API.但是,如果 REST API 不直接提供您要查找的数据,则它使用起来很尴尬,从而导致对 REST API 的复杂和多次调用。 This is slow and it makes you depend on stability of that API.这很慢,它使您依赖该 API 的稳定性。

For anything but the most basic queries, I usually prefer to run a small groovy script that will extract the required data from Jenkins' internal structures.除了最基本的查询之外,我通常更喜欢运行一个小的 groovy 脚本,该脚本将从 Jenkins 的内部结构中提取所需的数据。 This is way faster, and often it's also more simple to use.这更快,而且通常使用起来也更简单。 Here's a small script that will fetch the data that you're looking for:这是一个小脚本,它将获取您要查找的数据:

import jenkins.model.*
import hudson.plugins.promoted_builds.*
import groovy.json.JsonOutput

def job = Jenkins.instance.getItemByFullName( 'TESTJOB' )

def buildInfos = []
for ( build in job.getBuilds() ) {
  def promotionList = []
  for ( promotion in build.getAction(PromotedBuildAction.class).getPromotions() ) {
    promotionList += promotion.getName()
  }
  buildInfos += [
    result    : build.getResult().toString(),
      number    : build.getNumber(),
      time      : build.getTime().toString(),
      promotions: promotionList
  ]
}
println( JsonOutput.toJson( buildInfos ) )

The script will produce the result in JSON format, like this (prettified):该脚本将以 JSON 格式生成结果,如下所示(美化):

[
    {
        "number": 2, 
        "promotions": [
            "promotionA"
        ], 
        "result": "SUCCESS", 
        "time": "Thu Oct 18 11:50:37 EEST 2018"
    }, 
    {
        "number": 1, 
        "promotions": [], 
        "result": "SUCCESS", 
        "time": "Thu Oct 18 11:50:34 EEST 2018"
    }
]

You can run such a script via the Jenkins "Script Console" GUI, or via the REST API for running groovy scripts (sic).您可以通过 Jenkins“脚本控制台”GUI 或通过 REST API 运行此类脚本以运行 groovy 脚本(原文如此)。 There's also a CLI interface command for doing that.还有一个用于执行此操作的 CLI 界面命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM