本文介绍了如何在Jenkins管道构建日志中禁用命令输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jenkinsfile编写管道脚本.

I am using Jenkinsfile for scripting of a pipeline.

有什么方法可以禁止在构建日志中打印执行的shell命令吗?

Is there any way to disable printing of executed shell commands in build logs?

这只是詹金斯管道的一个简单示例:

Here is just a simple example of a jenkins pipeline:

node{
  stage ("Example") {
    sh('echo shellscript.sh arg1 arg2')
    sh('echo shellscript.sh arg3 arg4')
  }
}

会在控制台日志中产生以下输出:

which produces the following output in console log:

[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

基本上我想禁用命令本身的打印.

Basically I would like to disable printing of the commands itself.

+ echo shellscript.sh arg1 arg2
+ echo shellscript.sh arg3 arg4

推荐答案

默认情况下,Jenkins使用标志-xe启动shell脚本. -x启用其他日志记录.如果内部的任何命令返回非零退出状态,则-e会使脚本退出.要重设标志,我建议两个选项:

By default Jenkins starts shell scripts with flags -xe. -x enables additional logging. -e makes the script exit if any command inside returns non-zero exit status. To reset a flag I'd suggest two options:

  1. 在脚本正文中调用set +x.
  2. 通过不带-x的自定义shebang行:sh('#!/bin/sh -e\n' + 'echo shellscript.sh arg1 arg2')
  1. Call set +x in the body of your script.
  2. Pass custom shebang line without -x: sh('#!/bin/sh -e\n' + 'echo shellscript.sh arg1 arg2')

对于第二个选项,您可以定义一个包装函数以方便使用,该函数将在脚本前添加自定义shebang,然后调用sh

As for the second option you can define a wrapper function for convenience which will prepend the script with custom shebang and then call sh

def mysh(cmd) {
    sh('#!/bin/sh -e\n' + cmd)
}

这篇关于如何在Jenkins管道构建日志中禁用命令输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:11