Is it possible to capture the stdout from the sh DSL command in the pipeline

JenkinsJenkins WorkflowJenkins Pipeline

Jenkins Problem Overview


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?

Jenkins Solutions


Solution 1 - Jenkins

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.

Solution 2 - Jenkins

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";

Solution 3 - Jenkins

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

Solution 4 - Jenkins

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

Solution 5 - Jenkins

A short version would be:

echo sh(script: 'ls -al', returnStdout: true).result

Solution 6 - Jenkins

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

Solution 7 - Jenkins

def listing = sh script: 'ls -la /', returnStdout:true

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJesse SView Question on Stackoverflow
Solution 1 - JenkinsiloahzView Answer on Stackoverflow
Solution 2 - JenkinsAndrey ProkopievView Answer on Stackoverflow
Solution 3 - JenkinsMETAJIJIView Answer on Stackoverflow
Solution 4 - JenkinsGalloCedroneView Answer on Stackoverflow
Solution 5 - JenkinsA.HabView Answer on Stackoverflow
Solution 6 - JenkinsNusrat ul HasanView Answer on Stackoverflow
Solution 7 - JenkinsRohit SalechaView Answer on Stackoverflow