首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从共享库中,切换到Jenkinsfile stage -没有这样的属性:类的value1 : groovy.lang.Binding

从共享库中,切换到Jenkinsfile stage -没有这样的属性:类的value1 : groovy.lang.Binding
EN

Stack Overflow用户
提问于 2020-11-09 23:02:42
回答 3查看 76关注 0票数 0

再次使用旧的类:没有这样的属性: value1 for groovy.lang.MissingPropertyException: groovy.lang.Binding ...

我总是遇到这样的事情,这一次是使用一个共享库运行一条switch语句。

关于如何将库中设置的变量(value1,env)读取到Jenkinsfile (不获取变量)中(它可以很好地回显变量),您有什么想法吗?

我期望:jfile值是"some- dev -value“和dev

Jenkinsfile:

代码语言:javascript
运行
复制
@Library('piplinesharedlib') _
pipeline {
  agent any
    stages {
        stage('env setting by branch') {
            steps {
                branchSelector("${GIT_BRANCH}")
                echo "jfile values are ${value1} and ${env}"
            }
        }
    }
}

共享库:

代码语言:javascript
运行
复制
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

输出:

代码语言:javascript
运行
复制
...
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$6.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$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.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$001(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.access$200(CpsThreadGroup.java:93)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-11-15 18:34:39

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

代码语言:javascript
运行
复制
    stage('env setting by branch') {
        steps {
            branchSelector("${GIT_BRANCH}")
            echo "jfile values are ${branchSelector.value1} and ${branchSelector.env}"
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2020-11-09 23:30:58

尝试使用全局变量:

代码语言:javascript
运行
复制
def value1 = null // or maybe simply `value1 = null` if this doesn't work

@Library('piplinesharedlib') _

pipeline {
票数 1
EN

Stack Overflow用户

发布于 2020-11-11 22:54:12

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

代码语言:javascript
运行
复制
#!/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}"
    }

然后使用以下代码

代码语言:javascript
运行
复制
@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}"
            }
            }
        }
    }
}   
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64754102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档