jenkins - Jenkins 共享库中的全局变量

标签 jenkins groovy jenkins-pipeline

我有一个使用共享库的 Jenkinsfile。我想创建一个可用于共享库所有函数的全局变量,类似于 params目的。但是我总是以

groovy.lang.MissingPropertyException: No such property: pipelineParams for class: groovy.lang.Binding

正在关注 this guideline ,我在 Jenkinsfile 中定义了一个 Field:

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_nameenv[param_name] 跨共享库访问

关于jenkins - Jenkins 共享库中的全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57688110/

相关文章:

jenkins - 如何使用管道脚本检查jenkins中特定节点的在线或离线状态?

jenkins - 作为 Jenkins 管理员,如何在不以用户身份登录的情况下获取用户的 API token ?

continuous-integration - 如何以不同用户身份运行 jenkins

groovy - Groovy 中是否存在肯定断言消息?

java - 将对象属性复制到 Groovy 中的另一个对象

Jenkins 超时/中止异常

jenkins - 管道中的 channel 关闭异常

linux - 多分支管道 github webhook

java - 如何基于 XML 节点构建 xpath 表达式

jenkins - 未知阶段部分 "withSonarQubeEnv"