27

I want to hide jenkins sh execute command in pipeline

pipeline {
    agent any

    stages {
        stage('Load Lib') {
            steps {
                sh "ls -al /"
            }
        }
    }
}

Current result:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Load Lib)
[Pipeline] sh
[Test] Running shell script
+ ls -al /

I want to hide Running shell script ls -al / command in output.

Please help

Binh Pham
  • 411
  • 1
  • 4
  • 7
  • 1
    Possible duplicate of [Echo off in Jenkins Console Output](https://stackoverflow.com/questions/26797219/echo-off-in-jenkins-console-output) – Bono May 15 '18 at 08:28
  • 5 year and sh("set +x\n" + "ls -la") for every single bash line? I think is in this method but I don't see any kind of logger https://github.com/jenkinsci/shell-script-scm-plugin/blob/d35002a554f8e95301342c43f30e9a21a30a5c69/src/main/java/org/jvnet/hudson/plugins/ssscm/ShellScriptSCM.java#L306 – JRichardsz Jan 22 '23 at 23:15

2 Answers2

34

This is definitely related to Echo off in Jenkins Console Output

For pipeline, what this means is:

pipeline {
    agent any

    stages {
        stage('Load Lib') {
            steps {
                sh '''
                    set +x
                    //commands to be echoed off
                    ls -al
                    set -x 
                '''
            }
        }
    }
}

''' indicates a multi line command. set +x turns off command echoing, and set -x turns it back on again.

Barel elbaz
  • 426
  • 3
  • 8
Ben Green
  • 3,953
  • 3
  • 29
  • 49
  • 2
    If you need to interpolate ${VARIABLE}s, you can substitute a triple double quote: """ –  Apr 08 '19 at 15:22
  • 5
    This is just excluding command from console, but If I hover on Jenkins Pipeline stage log from UI, then it still shows command – Alpesh Jikadra Jan 27 '21 at 13:37
0

You can override this behaviour for the whole script by putting the following at the top of the build step:

#!/bin/bash +x
Eric Aya
  • 69,473
  • 35
  • 181
  • 253