How to disable command output in jenkins pipeline build logs

穿精又带淫゛_ 提交于 2019-12-03 14:45:18

问题


I am using Jenkinsfile for scripting of a pipeline.

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

回答1:


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. 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')

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)
}



回答2:


For instances where post processing is required. I extended the original solution provided here.

For example

def output = printWithNoTrace("ping -c 1 $FQDN | grep PING).trim()

wrapper function

def printWithNoTrace(cmd) {
steps.sh (script: '#!/bin/sh -e\n'+ cmd,returnStdout: true)
        }

The shell output is returned to trim() and saved to "output"



来源:https://stackoverflow.com/questions/39891926/how-to-disable-command-output-in-jenkins-pipeline-build-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!