How to retrieve Jenkins build parameters using the Groovy API?

前端 未结 10 2174
忘掉有多难
忘掉有多难 2020-12-09 07:55

I have a parameterized job that uses the Perforce plugin and would like to retrieve the build parameters/properties as well as the p4.change property that\'s set by the Perf

10条回答
  •  佛祖请我去吃肉
    2020-12-09 08:30

    thanks patrice-n! this code worked to get both queued and running jobs and their parameters:

    import hudson.model.Job
    import hudson.model.ParametersAction
    import hudson.model.Queue
    import jenkins.model.Jenkins
    
    println("================================================")
    for (Job job : Jenkins.instanceOrNull.getAllItems(Job.class)) {
        if (job.isInQueue()) {
            println("------------------------------------------------")
            println("InQueue " + job.name)
    
            Queue.Item queue = job.getQueueItem()
            if (queue != null) {
                println(queue.params)
            }
        }
        if (job.isBuilding()) {
            println("------------------------------------------------")
            println("Building " + job.name)
    
            def build = job.getBuilds().getLastBuild()
            def parameters = build?.getAllActions().find{ it instanceof ParametersAction }?.parameters
            parameters.each {
                def dump = it.dump()
                println "parameter ${it.name}: ${dump}"
            }
        }
    }
    println("================================================")
    

提交回复
热议问题