![](/img/trans.png)
[英]How to execute shell script in remote server using Jenkins Active choice parameter groovy script?
[英]Groovy parameter to shell script
我一直在嘗試將我的代碼分成兩個不同的文件: callTheFunction.groovy
和theFunction.groovy
。
從文件名可以看出:
callTheFunction.groovy
調用定義在Function.groovy中的theFunction.groovy
,將隨機值作為參數傳入。theFunction
是一個 shell 腳本 - 在 groovy function 內 - 它應該使用從callTheFunction
傳遞的參數。 問題:
shell 腳本無法識別/理解 arguments,變量為空,沒有值。
theFunction.groovy
def call(var1, var2) {
sh '''
echo "MY values $var1 and $var2"
'''
}
調用TheFunction.groovy
def call {
pipeline {
stages {
stage ('myscript') {
steps {
theFunction("Value1", "Value2")
}
}
}
}
}
OUTPUT 來自管道:
MY values and
我知道那里有類似的問題:
更新
您可以在沒有environment {}
的情況下使用環境變量
使用像我在這里使用的環境變量(我稍微重構了你的代碼)。 對 shell 腳本使用三重單引號進行循環並向其中添加 grrovy 變量:
def callfunc() {
sh '''
export s="key"
echo $s
for i in $VARENV1
do
echo "Looping ... i is set to $i"
done
'''
}
pipeline {
agent { label 'agent_1' }
stages {
stage ('Run script') {
steps {
script {
env.VARENV1 = "Peace"
}
callfunc()
}
}
}
}
OUTPUT:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.