Jenkins编写脚本的管道。两个问题:
我想念什么?(请参见下面的脚本)
node {
var=10
stage('HelloWorld') {
sh '''
echo "Hello World. Var=$var" ===> Prints nothing for var
var=20'''
}
stage('git clone') {
echo "Cloning git. Var = $var" ==> Prints 20, and not 10
sh '''
echo "Var in second stage is = $var" ===> Doesnt print anything here. I need 20.
'''
}
}
您的示例不起作用,因为您使用的是带单引号的字符串文字。从Groovy手册(重点是我的):
任何Groovy的表达可以在所有字符串文字从被内插,除了单和三单引号的字符串。
尝试这个:
sh "echo 'Hello World. Var=$var'"
或这个:
sh """
echo 'Hello World. Var=$var'
echo 'More stuff'
"""
您不能从Shell步骤直接设置Groovy变量。从Groovy到Shell,这仅在一个方向上起作用。相反,您可以设置退出代码或将数据写入Groovy可以读取的stdout。
传递true
参数returnStatus
并在shell脚本中设置退出代码,该代码将作为sh
步骤的返回值。
var = sh script: 'exit 42', returnStatus: true
echo "$var" // prints 42
传递true
参数returnStdout
并echo
从shell脚本使用以输出字符串数据。
var = sh script: "echo 'the answer is 42'", returnStdout: true
echo "$var" // prints "the answer is 42"
传递true
参数returnStdout
并echo
从shell脚本使用以JSON格式输出字符串数据。
使用解析Groovy代码中的JSON数据JsonSlurper
。现在,您可以查询一个常规的Groovy对象。
def jsonStr = sh returnStdout: true, script: """
echo '{
"answer": 42,
"question": "what is 6 times 7"
}'
"""
def jsonData = new groovy.json.JsonSlurper().parseText( jsonStr )
echo "answer: $jsonData.answer"
echo "question: $jsonData.question"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句