Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jenkins共享库中的全局变量_Jenkins_Groovy_Jenkins Pipeline - Fatal编程技术网

Jenkins共享库中的全局变量

Jenkins共享库中的全局变量,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,我有一个使用共享库的Jenkins文件。我想创建一个全局变量,它可以在共享库的所有函数中使用,类似于对象。然而,我总是以失败告终 groovy.lang.MissingPropertyException: No such property: pipelineParams for class: groovy.lang.Binding 接下来,我在jenkins文件中定义了一个字段: import org.apache.commons.io.FileUtils @groovy.transform.

我有一个使用共享库的Jenkins文件。我想创建一个全局变量,它可以在共享库的所有函数中使用,类似于对象。然而,我总是以失败告终

groovy.lang.MissingPropertyException: No such property: pipelineParams for class: groovy.lang.Binding
接下来,我在
jenkins文件
中定义了一个
字段

import org.apache.commons.io.FileUtils
@groovy.transform.Field
def pipelineParams

library identifier: 'pipeline-helper@master', retriever: modernSCM(
  [$class: 'GitSCMSource',
       remote: 'https://bitbucket/scm/jenkins/pipeline-helper.git',
       credentialsId: 'bitbucket.service.user'
  ])

defaultCiPipelineMSBuild {
    nodes     = 'TEST-NODES' /* label of jenkins nodes*/
    email     = 'example@example.com' /* group mail for notifications */
    msbuild   = 'MSBUILD-DOTNET-4.6' /* ms build tool to use */ 
}
然后在
defaultCiPipelineMSBuild
中设置
pipelineParams

def call(body) {
    // evaluate the body block, and collect configuration into the object
    pipelineParams= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    pipeline {
    ...
稍后,我调用一个函数
buildApplication
,该函数希望使用变量:

def msBuildExe = tool pipelineParams.msbuild

您没有自己创建一个全新的管道参数,而是尝试将变量添加到可以在共享库中使用的现有
env
参数中吗

env.param_name = "As per your requirement"

也可以使用
env.param_name
env[param_name]
跨共享库访问,正如@Dillip所建议的,即使对于非字符串,也可以使用
env
。如果存储为环境变量的对象是列表或映射,则应对其进行反序列化

def pipelineParams = 
// Take the String value between the { and } brackets.
"${env.pipelineParams}"[1..-2]
    .split(', ')
    .collectEntries { entry -> 
        def pair = entry.split('=')
        [(pair.first()): pair.last()]
    }

//use map
pipelineParams.msbuild
所以我稍微修改了管道代码,将map存储为环境变量

def call(body) {
    // evaluate the body block, and collect configuration into the object
    pipelineParams= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    env.pipelineParams = pipelineParams

    pipeline {
    ...
管道被序列化,因此在读取时返回字符串

{ nodes=TEST-NODES,email=example@example.com,msbuild=MSBUILD-DOTNET-4.6 }
因此,为了便于使用,必须对其进行反序列化

def pipelineParams = 
// Take the String value between the { and } brackets.
"${env.pipelineParams}"[1..-2]
    .split(', ')
    .collectEntries { entry -> 
        def pair = entry.split('=')
        [(pair.first()): pair.last()]
    }

//use map
pipelineParams.msbuild

您可以将反序列化添加到函数中,以便在其他地方也可以使用它。

这是一个很好的提示,是的,即使对于非字符串,也可以这样做。我会为thsi写一个单独的答案