gpt4 book ai didi

jenkins - 如何将 sh 的输出设置为 Groovy 变量?

转载 作者:行者123 更新时间:2023-12-02 17:53:47 25 4
gpt4 key购买 nike

是否可以得到 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}"

ADDENDUM:编写此答案后,在 sh 步骤中添加了一个新选项,使用 returnStdout: truesh 调用。

关于jenkins - 如何将 sh 的输出设置为 Groovy 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37127815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com