我需要在Jenkins中的多行shell腳本中訪問Groovy定義的變量(例如var1)。我需要在sh中使用雙引號「」「(我指的是here)如何在Jenkins Groovy中的多行shell腳本中同時使用Groovy定義的和OS系統變量
但是我也需要讀取和更改os系統變量(例如aws_api_key),它需要使用單引號'''在sh和使用\逃離美元$符號。(我指的是here)
我如何使用這兩者?任何幫助將非常感激。
如
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
我試過了。但問題是不能訪問諸如\ $ aws_api_key之類的os系統變量。 – Orionpax
@Orionpax你檢查過嗎?因爲以上這是我得到的: '+ echo groovy data var1 is bin,$ {var1} ,, bin,var1 !!! groovy data var1是bin,$ {var1},bin,var1 !!! aws_api_key的 +回波默認值是aws_api_key的 默認值「在withEnv定義」是「在限定withEnv」 + aws_api_key =在殼 +回聲aws_api_key的新值改變在殼被改變aws_api_key的 新值在改變外殼 + export newvar = newxxx + echo新var值是newxxx 新var值是newxxx' 看起來是什麼 –
是的。你是對的。 – Orionpax