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

I have something like this in a Jenkins (Groovy) file, and I want to write stdout and the exit code in a variable in order to use the information later.

sh "ls -l" 

How can I do this, especially since it seems like you really can't run any groovy code inside Jenkinsfile ?

+170
groovy jenkins-pipeline jenkins-workflow jenkinsfile
Apr 11 '16 at 11:39 on
source share
6 answers

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

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

Another function is the returnStatus option.

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

These options have been added depending on this issue.

See the official documentation for the sh command.

+304
Aug 05 '16 at 7:32
source share

Quick response:

 sh "ls -l > commandResult" result = readFile('commandResult').trim() 

I think there is a function request to get the result of step sh, but as far as I know, there is currently no other option.

EDIT: JENKINS-26133

EDIT2: not quite sure what version it is, but sh / bat steps can now return std output, simply:

 def output = sh returnStdout: true, script: 'ls -l' 
+38
Apr 11 '16 at 13:41
source share

The Current Pipeline version initially supports returnStdout and returnStatus , which allows you to get output or status from sh / bat steps.

Example:

 def ret = sh(script: 'uname', returnStdout: true) println ret 

The official documentation .

+37
Oct 12 '16 at 20:30
source share

This is an example that will make sense, I believe!

 node('master'){ stage('stage1'){ def commit = sh (returnStdout: true, script: '''echo hi echo bye | grep -o "e" date echo lol''').split() echo "${commit[-1]} " } } 
+11
Jul 20 '17 at 11:29
source share

If you want to get standard output and know if the command was successful, just use returnStdout and wrap it in an exception handler:

script pipeline

 try { // Fails with non-zero exit if dir1 does not exist def dir1 = sh(script:'ls -la dir1', returnStdout:true).trim() } catch (Exception ex) { println("Unable to read dir1: ${ex}") } 

exit:

 [Pipeline] sh [Test-Pipeline] Running shell script + ls -la dir1 ls: cannot access dir1: No such file or directory [Pipeline] echo unable to read dir1: hudson.AbortException: script returned exit code 2 

Unfortunately, hudson.AbortException does not have any useful method to get this exit status, so if the actual value is needed, you need to parse it in the message (pah!)

Unlike Javadoc, https://javadoc.jenkins-ci.org/hudson/AbortException.html the assembly did not fail when this exception was detected. It does not work when not caught!

Update: If you also want to get STDERR output from a shell command, several approaches are possible:

a) Redirect STDERR to STDOUT 2>&1 - but then you need to parse this from the main output, and you will not get the output if the command is not executed - because you are in the exception handler.

b) redirect STDERR to a temporary file (whose name you are preparing earlier) 2>filename (but do not forget to clear the file later) - i.e. main code becomes:

 def stderrfile = 'stderr.out' try { def dir1 = sh(script:"ls -la dir1 2>${stderrfile}", returnStdout:true).trim() } catch (Exception ex) { def errmsg = readFile(stderrfile) println("Unable to read dir1: ${ex} - ${errmsg}") } 

c) Go the other way, set returnStatus=true instead, do without an exception handler and always write the output to a file, that is:

 def outfile = 'stdout.out' def status = sh(script:"ls -la dir1 >${outfile} 2>&1", returnStatus:true) def output = readFile(outfile).trim() if (status == 0) { // output is directory listing from stdout } else { // output is error message from stderr } 

Warning: the above code applies only to Unix / Linux - Windows requires completely different shell commands.

+10
Nov 12 '18 at 0:07
source share

The easiest way is to use this method.

my_var='echo 2' echo $my_var output: 2

note that this is not a simple single quote - the back quote (').

-5
Nov 16 '18 at 6:46
source share



All Articles