How to retrieve Jenkins build parameters using the Groovy API?

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

    To get the parameterized build params from the current build from your GroovyScript (using Pipeline), all you need to do is: Say you had a variable called VARNAME.

    def myVariable = env.VARNAME

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

    Regarding parameters:

    See this answer first. To get a list of all the builds for a project (obtained as per that answer):

    project.builds
    

    When you find your particular build, you need to get all actions of type ParametersAction with build.getActions(hudson.model.ParametersAction). You then query the returned object for your specific parameters.

    Regarding p4.change: I suspect that it is also stored as an action. In Jenkins Groovy console get all actions for a build that contains p4.change and examine them - it will give you an idea what to look for in your code.

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

    Update: Jenkins 2.x solution:

    With Jenkins 2 pipeline dsl, you can directly access any parameter with the trivial syntax based on the params (Map) built-in:

    echo " FOOBAR value: ${params.'FOOBAR'}"

    The returned value will be a String or a boolean depending on the Parameter type itself. The syntax is the same for scripted or declarative syntax. More info at: https://jenkins.io/doc/book/pipeline/jenkinsfile/#handling-parameters

    Original Answer for Jenkins 1.x:

    For Jenkins 1.x, the syntax is based on the build.buildVariableResolver built-ins:

    // ... or if you want the parameter by name ...
    def hardcoded_param = "FOOBAR"
    def resolver = build.buildVariableResolver
    def hardcoded_param_value = resolver.resolve(hardcoded_param)
    

    Please note the official Jenkins Wiki page covers this in more details as well, especially how to iterate upon the build parameters: https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script

    The salient part is reproduced below:

    // get parameters
    def parameters = build?.actions.find{ it instanceof ParametersAction }?.parameters
    parameters.each {
       println "parameter ${it.name}:"
       println it.dump()
    }
    
    0 讨论(0)
  • 2020-12-09 08:18

    In cases when a parameter name cannot be hardcoded I found this would be the simplest and best way to access parameters:

    def myParam = env.getProperty(dynamicParamName)
    

    In cases, when a parameter name is known and can be hardcoded the following 3 lines are equivalent:

    def myParam = env.getProperty("myParamName")
    def myParam = env.myParamName
    def myParam = myParamName
    
    0 讨论(0)
  • 2020-12-09 08:18

    If you are trying to get all parameters passed to Jenkins job you can use the global variable params in your groovy pipeline to fetch it.

    http://jenkins_host:8080/pipeline-syntax/globals

    params

    Exposes all parameters defined in the build as a read-only map with variously typed values. Example:

    if (params.BOOLEAN_PARAM_NAME) {doSomething()} or to supply a nontrivial default value:

    if (params.get('BOOLEAN_PARAM_NAME', true)) {doSomething()} Note for multibranch (Jenkinsfile) usage: the properties step allows you to define job properties, but these take effect when the step is run, whereas build parameter definitions are generally consulted before the build begins. As a convenience, any parameters currently defined in the job which have default values will also be listed in this map. That allows you to write, for example:

    properties([parameters([string(name: 'BRANCH', defaultValue: 'master')])]) git url: '…', branch: params.BRANCH and be assured that the master branch will be checked out even in the initial build of a branch project, or if the previous build did not specify parameters or used a different parameter name.

    Use something like below.

    def dumpParameter()
    {
      params.each {
        println it.key + " = " + it.value
      }
    }
    
    0 讨论(0)
  • 2020-12-09 08:21

    Get all of the parameters:

    System.getenv().each{
      println it
    }
    

    Or more sophisticated:

    def myvariables = getBinding().getVariables()
    for (v in myvariables) {
       echo "${v} " + myvariables.get(v)
    }
    

    You will need to disable "Use Groovy Sandbox" for both.

    0 讨论(0)
提交回复
热议问题