【问题标题】:Jenkins get list of builds and parametersJenkins 获取构建和参数列表
【发布时间】:2013-05-15 22:04:30
【问题描述】:

我想对 Jenkins 进行 API 调用,以获取构建列表及其参数和状态。我们目前传递一个 git commit sha1 作为参数来构建一个特定的分支。有什么想法可以让我轻松获取这些信息吗?

【问题讨论】:

    标签: api jenkins


    【解决方案1】:

    据我所知,这不能在单个 API 调用中完成。

    首先查询所有构建。

    /job/<jobname>/api/xml
    /job/<jobname>/api/json
    

    这将分别返回 xml 或 json 输出。

    获得内部版本号后,您可以查询每个内部版本号。

    /job/<jobname>/<jobnum>/api/xml?xpath=/freeStyleBuild/action/lastBuiltRevision/SHA
    /job/<jobname>/<jobnum>/api/json?tree=actions[lastBuiltRevision[SHA]]
    

    然后您可以对照您的 SHA 检查结果中的 SHA。

    【讨论】:

    • 或者这样的一个电话.../job/&lt;job-name&gt;/api/json?tree=builds[actions[parameters[name,value]],result,building,number,duration,estimatedDuration]
    【解决方案2】:

    将@user1255162 的评论与答案结合起来。我必须查询构建集并打印其参数以生成报告。这是groovy中的sn-p代码

    import groovy.json.JsonSlurper
    
    
    def root = "<url to job>"
    def options = "/api/json?tree=builds[actions[parameters[name,value]],result,building,number,duration,estimatedDuration]"
    
    def jsonSlurper = new JsonSlurper()
    def text = new URL("${root}/${options}").text
    def data = jsonSlurper.parseText(text)
    
    data["builds"].each { buildsdata ->
        def result = buildsdata["result"]
        def num = buildsdata["number"]
        print("${root}/${num}/parameters  |")
        buildsdata["actions"].each { actions ->
            if (actions["_class"].equals("hudson.model.ParametersAction")) {
                actions["parameters"].sort({it.name}).each { param ->
                        print("${param.name}=${param.value}|")
                }
            }
        }
        println("")
    }
    

    【讨论】:

      【解决方案3】:

      Jenkins 提供了一个不错的 api。

      记录在:

      http://$HOST/jenkins/api
      

      你可能想要这样的东西:

      http://$HOST/jenkins/api/xml?xpath=/hudson/job[1]/build[1]/action[1]/parameter&depth=2
      

      【讨论】:

      • 似乎在 /hudson/job[1] 之后使用与您显示的类似的查询实际上不会产生任何结果。
      • 谢谢你,米查斯。你让我找到了正确的答案!这是我正在寻找的输出。我看到您必须指定确切的内部版本号。 /job/Clinical.Search/15/api/xml
      • 您应该也可以使用/job/Clinical.Search/lastSuccessfulBuild/api/xml 来获得最后一次成功的构建。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 2018-12-19
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多