如何将 sh 的输出设置为 Groovy 变量?
How to set the output of sh to a Groovy variable?
是否可以将 sh
命令的输出设置为 Groovy 变量?它似乎正在将其设置为命令的状态。
示例输入:
node {
stage "Current Date"
def curDate = sh "date"
echo "The current date is ${curDate}"
}
结果如下:
Entering stage Current Date
Proceeding
[Pipeline] sh
[workspace] Running shell script
+ date
Tue May 10 01:15:05 UTC 2016
[Pipeline] echo
The current date is 0
显示的是The current date is 0
,我想让它显示The current date is Tue May 10 01:15:05 UTC 2016
,你可以看到sh命令已经输出了。我做错了吗?
是的,sh
正在返回退出状态。目前你最好的选择是:
sh 'date > outFile'
curDate = readFile 'outFile'
echo "The current date is ${curDate}"
附录:写完此答案后,sh
步骤中添加了一个新选项,使用 returnStdout: true
从 sh
调用中获取结果字符串。
是否可以将 sh
命令的输出设置为 Groovy 变量?它似乎正在将其设置为命令的状态。
示例输入:
node {
stage "Current Date"
def curDate = sh "date"
echo "The current date is ${curDate}"
}
结果如下:
Entering stage Current Date
Proceeding
[Pipeline] sh
[workspace] Running shell script
+ date
Tue May 10 01:15:05 UTC 2016
[Pipeline] echo
The current date is 0
显示的是The current date is 0
,我想让它显示The current date is Tue May 10 01:15:05 UTC 2016
,你可以看到sh命令已经输出了。我做错了吗?
是的,sh
正在返回退出状态。目前你最好的选择是:
sh 'date > outFile'
curDate = readFile 'outFile'
echo "The current date is ${curDate}"
附录:写完此答案后,sh
步骤中添加了一个新选项,使用 returnStdout: true
从 sh
调用中获取结果字符串。