How to retrieve Jenkins build parameters using the Groovy API?

前端 未结 10 2124
忘掉有多难
忘掉有多难 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:22

    I've just got this working, so specifically, using the Groovy Postbuild plugin, you can do the following:

    def paramText
    def actionList = manager.build.getActions(hudson.model.ParametersAction)
    if (actionList.size() != 0)
    {
      def pA = actionList.get(0)
      paramText = pA.createVariableResolver(manager.build).resolve("MY_PARAM_NAME")
    }
    
    0 讨论(0)
  • 2020-12-09 08:23

    For resolving a single parameter (I guess what's most commonly needed), this is the simplest I found:

    build.buildVariableResolver.resolve("myparameter")

    in your Groovy System script build step.

    0 讨论(0)
  • 2020-12-09 08:28

    The following can be used to retreive an environment parameter:

    println System.getenv("MY_PARAM") 
    
    0 讨论(0)
  • 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("================================================")
    
    0 讨论(0)
提交回复
热议问题