无法使用groovy函数中在shell中导出的环境变量
我定义了Jenkins声明性管道,如下所示:无法使用groovy函数中在shell中导出的环境变量,shell,jenkins,groovy,jenkins-pipeline,Shell,Jenkins,Groovy,Jenkins Pipeline,我定义了Jenkins声明性管道,如下所示: def test(String A){ sh """ export tag="20190101" echo "${tag}" echo "${A}" """ } pipeline{ agent any stages{ stage('check'){ steps{ test("aaa") }
def test(String A){
sh """
export tag="20190101"
echo "${tag}"
echo "${A}"
"""
}
pipeline{
agent any
stages{
stage('check'){
steps{
test("aaa")
}
}
}
}
sh '''
export tag="20190101"
echo "${tag}"
echo "${A}"
'''
如果我像下面那样使用双引号
,命令echo“${A}”
将导致错误:
sh """
export tag="20190101"
echo "${tag}"
echo "${A}"
"""
错误如下:
groovy.lang.MissingPropertyException: No such property: tag for class: WorkflowScript
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:39)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.test(WorkflowScript:4)
at WorkflowScript.run(WorkflowScript:13)
at
....
因此,我将双引号
更改为单引号
,这次没有显示错误,但我可以从groovy获得变量pass,如下所示:
def test(String A){
sh """
export tag="20190101"
echo "${tag}"
echo "${A}"
"""
}
pipeline{
agent any
stages{
stage('check'){
steps{
test("aaa")
}
}
}
}
sh '''
export tag="20190101"
echo "${tag}"
echo "${A}"
'''
我认为如果使用单引号
,可以正确解析环境变量,不能解析groovy传递的变量;如果使用双引号
,则可以正确解析groovy传递的变量,但无法解析shell中的环境变量,并将导致错误。
然而,我需要使用我设置的shell环境中的变量和groovy传递的变量,在我的场景中,如何使用shell定义函数?如果你能帮忙,非常感谢
注:
詹金斯2.x声明性管道 这应该起作用:
def test(String A){
sh """
export tag="20190101"
echo \$tag
echo ${A}
"""
}
pipeline{
agent any
stages{
stage('check'){
steps{
test("aaa")
}
}
}
}
抱歉,在
双引号
场景中,错误应该是由echo“${tag}”
引起的。您是否尝试在没有双引号的情况下使用echo${tag}
?在双引号解决方案中,可能需要转义美元符号以延迟扩展:echo“\$tag”