46

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
Vasyl Keretsman
  • 2,688
  • 2
  • 18
  • 15

2 Answers2

64

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.

    sh 'set +x'
    
  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)
}
not2savvy
  • 2,902
  • 3
  • 22
  • 37
izzekil
  • 5,781
  • 2
  • 36
  • 38
  • 2
    This is for anyone who is working for windows => output = bat(returnStdout: true, script: '') – Girish Nair Mar 07 '18 at 11:42
  • The 'alias' list was being printed for every sh in a pipeline script. I didn't figure out where/how 'alias' was called for every sh command. But using just '-e' and not '-x' caused the alias list not to show anymore. – onebeartoe Jun 29 '18 at 16:51
  • 3
    Beware that in the new "Blue Ocean" view that Jenkins gives you, these environment variables will still be visible in the UI. We got round this by writing the commands to a file (using a writefile pipeline step) and then executing the file. – BretC Aug 28 '18 at 13:21
  • 1
    @Bretc many thanks for your suggestion. That is indeed the way if you want to hide from both blue ocean and console output, most part of the answers around just mention how to hide from classic log output. – nandilov Mar 25 '22 at 18:42
5

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"

upHiller
  • 936
  • 2
  • 9
  • 14