来自共享库的 var,切换到 Jenkinsfile 阶段 - No such 属性: value1 for class: groovy.lang.Binding

var from shared libraries, switch to Jenkinsfile stage - No such property: value1 for class: groovy.lang.Binding

再次使用旧的 groovy.lang.MissingPropertyException:没有这样的 属性:class 的值 1:groovy.lang.Binding ...

我将 运行 放入这些东西中,这次使用共享库 运行 switch 语句。

关于如何将库中设置的变量 (value1, env) 读取到 Jenkinsfile(未获取变量)的任何想法?

我期待:jfile 值是“some-dev-value”和 dev

詹金斯文件:

@Library('piplinesharedlib') _
pipeline {
  agent any
    stages {
        stage('env setting by branch') {
            steps {
                branchSelector("${GIT_BRANCH}")
                echo "jfile values are ${value1} and ${env}"
            }
        }
    }
}

共享库:

def call(String GIT_BRANCH) {
    echo "gitbranch is ${GIT_BRANCH}"
    def version = "1.0"

    def values = readProperties file: "${env.WORKSPACE}\values.groovy"
    //def value1 = ''

    switch(GIT_BRANCH) {
        case "develop":
            env = "dev"
            echo "setting env to ${env}"
            value1 = values['DevValueloadedFromReasources']
            break
        case ["master", "Feature*".toString()]:
            env = "prod"
            echo "setting env to ${env}"
            value1 = values['ProdValueloadedFromReasources']
            break
        case "Feature*":
            env = "dev"
            echo "setting env to ${env}"
            value1 = values['FeatureValueloadedFromReasources']
            break
        default:
            value1 = "nothin"
            break
    }
        echo "The values for this env are ${value1} and ${env}"
    }
return this

输出:

...
gitbranch is develop
[Pipeline] readProperties
[Pipeline] echo
setting env to dev
[Pipeline] echo
The values for this env are "some-dev-value" and dev
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
[Bitbucket] Notifying commit build result
[Bitbucket] Build result notified
groovy.lang.MissingPropertyException: No such property: value1 for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
    at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:289)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:293)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:269)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:269)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.run(WorkflowScript:32)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:137)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:682)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:417)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:415)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:681)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:280)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:566)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:565)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:270)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:465)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:464)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:269)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(ModelInterpreter.groovy:503)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(ModelInterpreter.groovy:502)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:268)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:315)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inDeclarativeAgent(ModelInterpreter.groovy:607)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inDeclarativeAgent(ModelInterpreter.groovy:606)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:312)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.stageInput(ModelInterpreter.groovy:378)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.stageInput(ModelInterpreter.groovy:377)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:311)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inWrappers(ModelInterpreter.groovy:634)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inWrappers(ModelInterpreter.groovy:633)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:251)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
    at sun.reflect.GeneratedMethodAccessor378.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=15=]1(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:93)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:282)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:270)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:59)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

尝试使用全局:

def value1 = null // or maybe simply `value1 = null` if this doesn't work

@Library('piplinesharedlib') _

pipeline {

为了解决你的问题,首先创建一个名为sharedLib.groovy的groovy文件,并定义两个全局变量“value1”和“env”,然后用@Field注解它们的默认值。在您的方法中更改这两个变量的值。

#!/usr/bin/env groovy

import groovy.transform.Field

@Field value1 = ''
@Field env = ''
def myMethod(String GIT_BRANCH) {
    echo "gitbranch is ${GIT_BRANCH}"
    def version = "1.0"

    //def values = readProperties file: "${env.WORKSPACE}\values.groovy"

    //def value1 = ''

    switch(GIT_BRANCH) {
        case "develop":
            env = "dev"
            echo "setting env to ${env}"
            value1 ="1"
            break
        case ["master", "Feature*".toString()]:
            env = "prod"
            echo "setting env to ${env}"
            value1 ="2"
            break
        case "Feature*":
            env = "dev"
            echo "setting env to ${env}"
            value1 = "3"
            break
        default:
            value1 = "nothin"
            break
    }
        echo "The values for this env are ${value1} and ${env}"
    }

然后使用下面的代码

@Library("piplinesharedlib") _
pipeline {
  agent {label "coreop-pasargad-246"}
    stages {
        stage('env setting by branch') {
            steps {
            script{
                sharedLib.myMethod("${GIT_BRANCH}");

                //branchSelector("${GIT_BRANCH}")
                echo "jfile values are ${sharedLib.value1} and ${sharedLib.env}"
            }
            }
        }
    }
}   

把我的头撞在墙上……事实证明,只需调用 ${branchSelector.value1} 就可以了。

    stage('env setting by branch') {
        steps {
            branchSelector("${GIT_BRANCH}")
            echo "jfile values are ${branchSelector.value1} and ${branchSelector.env}"
        }
    }