如何从 Jenkins Pipeline sh 脚本步骤返回 stdout 或 stderr 以及状态 [英] How to return either stdout or stderr together with the status from a Jenkins Pipeline sh script step

查看:111
本文介绍了如何从 Jenkins Pipeline sh 脚本步骤返回 stdout 或 stderr 以及状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个自我回答的问题.它的灵感来自 @codeGeass' 评论:

This is a self-answered question. It was inspired by @codeGeass' comment:

我们可以一起使用它们吗?在一个变量中捕获 returnStdout 并在另一个中捕获 returnStatus ?因为重复两次sh脚本不爽

Can we use them together ? catch returnStdout in a variable and returnStatus in an other ? because it is not cool to repeat the sh script twice

如何在 Jenkins 2.0 流水线作业中执行命令然后返回标准输出的回答.

推荐答案

In plain (Windows Git) Bash 它的工作原理如下(灵感来自 @Ian W 对上述问题的评论):

In plain (Windows Git) Bash it works as follows (inspired by @Ian W's comment to the question above):

stdErrOrOutAndStatus () {
    $1 2>&1 ; echo "status:$?"
}

stdOutAndStatus=$( stdErrOrOutAndStatus 'ls -a' )
echo -e "$stdOutAndStatus\n"
stdErrAndStatus=$( stdErrOrOutAndStatus 'ls notexisting' )
echo -e "$stdErrAndStatus\n"
stdErrAndStatus=$( stdErrOrOutAndStatus 'not_existing_command' )
echo -e "$stdErrAndStatus\n"

输出

$ ./stdErrOrOutAndStatus.sh
.
..
stdErrOrOutAndStatus.sh
status:0

ls: cannot access 'notexisting': No such file or directory
status:2

./stdErrOrOutAndStatus.sh: line 3: not_existing_command: command not found
status:127

注意:我希望 Bash 中没有我不知道的陷阱(目前).我不太经常写脚本.

NB: I hope there's no pitfall of/in Bash I'm not aware of (yet). I don't write scripts too often.

这篇关于如何从 Jenkins Pipeline sh 脚本步骤返回 stdout 或 stderr 以及状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆