Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable command output in jenkins pipeline build logs

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 
like image 297
Vasyl Keretsman Avatar asked Oct 06 '16 09:10

Vasyl Keretsman


People also ask

How do I hide console output in Jenkins?

You can hide your output away from the script by redirecting the output to /dev/null but I think this the most you can do – the commands that you write will still be displayed. You received this message because you are subscribed to the Google Groups "Jenkins Users" group.

Where is Jenkins console output stored?

On Windows, Jenkins log files are stored as jenkins. out (console output logs) and jenkins. err (error logs) in the Jenkins home folder. Note: Jenkins logs may be stored in the Jenkins installation folder in Program Files on some Windows systems.

What is SH in Jenkins pipeline?

On Linux, BSD, and Mac OS (Unix-like) systems, the sh step is used to execute a shell command in a Pipeline. Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Build') { steps { sh 'echo "Hello World"' sh ''' echo "Multiline shell steps works too" ls -lah ''' } } } }


1 Answers

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) } 
like image 97
izzekil Avatar answered Sep 22 '22 22:09

izzekil