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

问题描述

我正在使用 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?

这里只是一个简单的 jenkins 管道示例:

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' + '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' + '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
' + cmd)
}

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

08-16 04:54