How to retrieve Jenkins build parameters using the Groovy API?

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

提交回复
热议问题