我需要在Jenkins的多行shell脚本中访问Groovy定义的变量(例如var1)。我需要在sh中使用双引号“”(我在这里指的是)
但是我也需要读取和更改os系统变量(例如aws_api_key)。它需要在sh中使用单引号''',并使用\来使美元$符号转义。(我指的是这里)
如何同时使用它们?任何帮助都感激不尽。
例如
node ("Jenkins-Test-Slave") {
stage ("hello world") {
echo 'Hello World'
}
def var1="bin"
stage ("test") {
withEnv(["aws_api_key='define in withEnv'","service_url=''"]) {
echo var1
sh '''
echo the groovy data var1 is "${var1}",'\${var1}',\$var1,${var1},var1!!!
echo default value of aws_api_key is \$aws_api_key
aws_api_key='changed in shell'
echo new value of aws_api_key is \$aws_api_key
export newvar='newxxx'
echo the new var value is \$newvar
'''
}
}
}
结果是:
+ echo the groovy data var1 is ,${var1},,,var1!!!
the groovy data var1 is ,${var1},,,var1!!!
+ echo default value of aws_api_key is 'define in withEnv'
default value of aws_api_key is 'define in withEnv'
+ aws_api_key=changed in shell
+ echo new value of aws_api_key is changed in shell
new value of aws_api_key is changed in shell
+ export newvar=newxxx
+ echo the new var value is newxxx
the new var value is newxxx
代替多行String('''
),使用多行GString("""
)使字符串插值有效,然后${var1}
将按预期进行插值:
sh """
echo the groovy data var1 is "${var1}",'\${var1}',\$var1,${var1},var1!!!
echo default value of aws_api_key is \$aws_api_key
aws_api_key='changed in shell'
echo new value of aws_api_key is \$aws_api_key
export newvar='newxxx'
echo the new var value is \$newvar
"""
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句