sorin sorin - 11 months ago 678
Groovy Question

How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?

I have something like this on a Jenkinsfile (Groovy) and I want to record the stdout and the exit code in a variable in order to use the information later.

sh "ls -l"

How can I do this, especially as it seems that you cannot really run any kind of groovy code inside the


The latest version of the pipeline sh step allows you to do the following;

// Git committer email
vars.GIT_COMMIT_EMAIL = sh (
    script: 'git --no-pager show -s --format=\'%ae\'',
    returnStdout: true
echo "Git committer email: ${vars.GIT_COMMIT_EMAIL}"

Another undocumented (for now) feature is the returnStatus option.

// Test commit message for flags
vars.BUILD_FULL = sh (
    script: "git log -1 --pretty=%B | grep '\\[jenkins-full]'",
    returnStatus: true
) == 0
echo "Build full flag: ${vars.BUILD_FULL}"

These options where added based on this issue.