114

For example:

var output=sh "echo foo";
echo "output=$output";

I will get:

output=0

So, apparently I get the exit code rather than the stdout. Is it possible to capture the stdout into a pipeline variable, such that I could get: output=foo as my result?

StephenKing
  • 36,187
  • 11
  • 83
  • 112
Jesse S
  • 1,595
  • 2
  • 11
  • 10

7 Answers7

266

Now, the sh step supports returning stdout by supplying the parameter returnStdout.

// These should all be performed at the point where you've
// checked out your sources on the slave. A 'git' executable
// must be available.
// Most typical, if you're not cloning into a sub directory
gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
// short SHA, possibly better for chat notifications, etc.
shortCommit = gitCommit.take(6)

See this example.

Pang
  • 9,564
  • 146
  • 81
  • 122
iloahz
  • 4,491
  • 8
  • 23
  • 31
  • 14
    note the `.trim()` part of this answer, otherwise you may get a newline character at the end of the line – Will Munn Dec 05 '16 at 11:22
  • 2
    append `--short` to `rev-parse` can just directly obtain a short hash – Leon Mar 19 '17 at 14:13
  • 2
    not sure what causing failure but I had to convert output to string also like this `gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').toString().trim()` – Balkrishna Nov 29 '17 at 23:58
  • 1
    hi, what '.take(6)' stands for? – Vano Nov 29 '18 at 08:07
  • 2
    @Vano that refers to the Groovy method take(), which will get the first 6 characters in this case. http://docs.groovy-lang.org/docs/groovy-2.3.2/html/api/org/codehaus/groovy/runtime/StringGroovyMethods.html#take(java.lang.CharSequence,%20int) – ahillman3 Nov 30 '18 at 21:15
  • In my case I'm getting `java.lang.NullPointerException: Cannot invoke method trim() on null object` and it's only when I'm using `myvar.take()` in some other function, while it works with simple `sh 'echo'+myvar.take()` – holms Jul 16 '19 at 09:33
49

Note: The linked Jenkins issue has since been solved.

As mention in JENKINS-26133 it was not possible to get shell output as a variable. As a workaround suggested using of writ-read from temporary file. So, your example would have looked like:

sh "echo foo > result";
def output=readFile('result').trim()
echo "output=$output";
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • 22
    For newcomers, please see the answer http://stackoverflow.com/a/38912813/345845 below, this was since made easier with the new `returnStdout` parameter passed to the `sh` step. – Baptiste Mathus Mar 12 '17 at 13:28
  • 2
    "it's not possible to get shell output as a variable" - not true. This is a hack, the correct answer is returnStdout. – Graham Jul 07 '17 at 13:02
  • 4
    The only time this is actually a good answer is if you need **both** the `stdout` and the `exit status` from the shell command. Other times, use the `returnStdout` parameter. – Simon Forsberg Dec 03 '17 at 22:20
5

Try this:

def get_git_sha(git_dir='') {
    dir(git_dir) {
        return sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
    }
}

node(BUILD_NODE) {
    ...
    repo_SHA = get_git_sha('src/FooBar.git')
    echo repo_SHA
    ...
}

Tested on:

  • Jenkins ver. 2.19.1
  • Pipeline 2.4
METAJIJI
  • 361
  • 2
  • 11
5

You can try to use as well this functions to capture StdErr StdOut and return code.

def runShell(String command){
    def responseCode = sh returnStatus: true, script: "${command} &> tmp.txt" 
    def output =  readFile(file: "tmp.txt")

    if (responseCode != 0){
      println "[ERROR] ${output}"
      throw new Exception("${output}")
    }else{
      return "${output}"
    }
}

Notice:

&>name means 1>name 2>name -- redirect stdout and stderr to the file name
GalloCedrone
  • 4,869
  • 3
  • 25
  • 41
3

I had the same issue and tried almost everything then found after I came to know I was trying it in the wrong block. I was trying it in steps block whereas it needs to be in the environment block.

        stage('Release') {
                    environment {
                            my_var = sh(script: "/bin/bash ${assign_version} || ls ", , returnStdout: true).trim()
                                }
                    steps {                                 
                            println my_var
                            }
                }
1

A short version would be:

echo sh(script: 'ls -al', returnStdout: true).result
A.Hab
  • 47
  • 2
0
def listing = sh script: 'ls -la /', returnStdout:true

Reference : http://shop.oreilly.com/product/0636920064602.do Page 433

Pang
  • 9,564
  • 146
  • 81
  • 122
Rohit Salecha
  • 893
  • 11
  • 9