如何在 jenkins 管道构建日志中禁用命令输出
How to disable command output in jenkins pipeline build logs
我正在使用 Jenkinsfile 编写管道脚本。
有什么方法可以禁止在构建日志中打印已执行的 shell 命令吗?
这里只是一个简单的 jenkins 管道示例:
node{
stage ("Example") {
sh('echo shellscript.sh arg1 arg2')
sh('echo shellscript.sh arg3 arg4')
}
}
在控制台日志中产生以下输出:
[Pipeline] node
Running on master in /var/lib/jenkins/workspace/testpipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
[testpipeline] Running shell script
+ echo shellscript.sh arg1 arg2
shellscript.sh arg1 arg2
[Pipeline] sh
[testpipeline] Running shell script
+ echo shellscript.sh arg3 arg4
shellscript.sh arg3 arg4
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
基本上我想禁用命令本身的打印。
+ echo shellscript.sh arg1 arg2
+ echo shellscript.sh arg3 arg4
默认情况下,Jenkins 启动 shell 带有标志 -xe
的脚本。 -x
启用额外的日志记录。如果 returns 中的任何命令非零退出状态,-e
使脚本退出。要重置标志,我建议两个选项:
- 在脚本正文中调用
set +x
。
- 在没有
-x
的情况下通过自定义 shebang 行:sh('#!/bin/sh -e\n' + 'echo shellscript.sh arg1 arg2')
至于第二个选项,您可以为方便起见定义一个包装函数,它将在脚本前添加自定义 shebang,然后调用 sh
def mysh(cmd) {
sh('#!/bin/sh -e\n' + cmd)
}
对于需要 post 处理的实例。我扩展了此处提供的原始解决方案。
例如
def output = printWithNoTrace("ping -c 1 $FQDN | grep PING).trim()
包装函数
def printWithNoTrace(cmd) {
steps.sh (script: '#!/bin/sh -e\n'+ cmd,returnStdout: true)
}
shell输出返回到trim()并保存到"output"
我正在使用 Jenkinsfile 编写管道脚本。
有什么方法可以禁止在构建日志中打印已执行的 shell 命令吗?
这里只是一个简单的 jenkins 管道示例:
node{
stage ("Example") {
sh('echo shellscript.sh arg1 arg2')
sh('echo shellscript.sh arg3 arg4')
}
}
在控制台日志中产生以下输出:
[Pipeline] node
Running on master in /var/lib/jenkins/workspace/testpipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
[testpipeline] Running shell script
+ echo shellscript.sh arg1 arg2
shellscript.sh arg1 arg2
[Pipeline] sh
[testpipeline] Running shell script
+ echo shellscript.sh arg3 arg4
shellscript.sh arg3 arg4
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
基本上我想禁用命令本身的打印。
+ echo shellscript.sh arg1 arg2
+ echo shellscript.sh arg3 arg4
默认情况下,Jenkins 启动 shell 带有标志 -xe
的脚本。 -x
启用额外的日志记录。如果 returns 中的任何命令非零退出状态,-e
使脚本退出。要重置标志,我建议两个选项:
- 在脚本正文中调用
set +x
。 - 在没有
-x
的情况下通过自定义 shebang 行:sh('#!/bin/sh -e\n' + 'echo shellscript.sh arg1 arg2')
至于第二个选项,您可以为方便起见定义一个包装函数,它将在脚本前添加自定义 shebang,然后调用 sh
def mysh(cmd) {
sh('#!/bin/sh -e\n' + cmd)
}
对于需要 post 处理的实例。我扩展了此处提供的原始解决方案。
例如
def output = printWithNoTrace("ping -c 1 $FQDN | grep PING).trim()
包装函数
def printWithNoTrace(cmd) {
steps.sh (script: '#!/bin/sh -e\n'+ cmd,returnStdout: true)
}
shell输出返回到trim()并保存到"output"