2017-10-04 84 views
0

我试图去掉我的詹金斯输出。感谢Is it possible to capture the stdout from the sh DSL command in the pipeline,我知道我可以将每个sh命令的输出发送到一个文件。但是,命令本身仍然会写入Jenkins输出而不是文件。例如:是否可以将Jenkins管道中的sh DSL命令的所有输出发送到文件?

sh '''echo "Hello World!" 
    ./helloworld 
    ./seeyoulater 
''' 

原样,这导致詹金斯输出看起来像这样:

echo "Hello World!" 
Hello World! 
./helloworld 
<helloworld output, possibly many lines> 
./seeyoulater 
<seeyoulater output, possibly many lines> 

但是,如果我将输出发送到一个文件,我得到这样詹金斯输出:

echo "Hello World!" > output.log 
./helloworld >> output.log 
./seeyoulater >> output.log 

和output.log看起来像这样:

Hello World! 
<helloworld output> 
<seeyoulater output> 

这会导致我的Jenkins输出不那么混乱,但output.log最终在脚本输出之间没有任何分隔符。我想我可以在每个命令之前有echo <command>,但这只意味着我的詹金斯输出再次变得混乱。

有什么办法可以将sh DSL命令的整个输出发送到文件吗?基本上像sh '''<commands here>''' > output.log是我正在寻找的东西。

回答

0

我无法找到解决方案,但我找到了解决方法。如sh command documentation中所述,缺省值是使用-xe标志运行。 -x标志是为什么命令显示在Jenkins输出中。补救的办法是增加set +x

sh '''set +x 
    echo "Hello World!" > output.log 
    ./helloworld >> output.log 
    ./seeyoulater >> output.log 
''' 

set +x显示了在詹金斯输出,但命令的其余部分没有。从那里开始,只需添加足够的echo语句即可使output.log充分可读。

相关问题