【发布时间】:2019-01-14 23:28:38
【问题描述】:
我有一个 groovy 变量,我想将它传递给 shell 块进行进一步处理,但我不断收到错误消息:
stages {
stage('First Stage - echo out available variables'){
steps{
script {
def string_var = "im a groovy variable"
echo "${string_var}" // This will print "im a groovy variable" just fine
sh """
echo """ + string_var + """
""" // This will error
sh """
echo ${string_var}
""" // This will error
sh ''' echo '''+ string_var +''' '''
sh "echo ${string_var}" // This will error
}
}
}
}
我的错误:
an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@1ecb37ba
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@32070c3b
in field com.cloudbees.groovy.cps.Continuable.e
in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@641113f0
【问题讨论】:
-
需要定义为
sh """ echo string_var: ${string_var} """ -
sh方法的参数类型有哪些?您可能需要这样做才能立即评估 GString: sh "echo $string_var".toString()
-
@RohitMahajan 我试过了,同样的错误,在你发布的链接上,我尝试了额外的三倍“但仍然得到同样的错误。我将编辑上面的代码以显示我刚刚尝试过的内容.
-
三双引号对我有用。你能试试
sh ''' echo '''+ string_var +''' '''