Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture the output of sh command in the declarative Jenkins pipeline

In the following code I'm trying to assign the 'ls -l /' result to the b global variable but, when I try to print what's inside it, the result is null.

How can I set a global variable?

def b = [:]
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script{
                    b = sh 'ls -l /'
                    println "b:"+b
                }
            }
        }
    }
}

This is the result:

[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ ls -l /
total 24
drwxr-xr-x   2 root root 4096 Jan 18 11:49 bin
drwxr-xr-x   2 root root    6 Oct 20 10:40 boot
drwxr-xr-x   5 root root  360 Jan 21 10:00 dev
drwxr-xr-x   1 root root   77 Jan 21 10:00 etc
drwxr-xr-x   2 root root    6 Oct 20 10:40 home
drwxr-xr-x   8 root root   96 Jan 18 11:49 lib
drwxr-xr-x   2 root root   34 Jan 18 11:49 lib64
drwxr-xr-x   2 root root    6 Dec 26 00:00 media
drwxr-xr-x   2 root root    6 Dec 26 00:00 mnt
drwxr-xr-x   2 root root    6 Dec 26 00:00 opt
dr-xr-xr-x 276 root root    0 Jan 21 10:00 proc
drwx------   1 root root   76 Feb 12 17:32 root
drwxr-xr-x   1 root root   21 Jan 21 10:00 run
drwxr-xr-x   2 root root 4096 Jan 18 11:49 sbin
drwxr-xr-x   2 root root    6 Dec 26 00:00 srv
dr-xr-xr-x  13 root root    0 Feb  6 02:34 sys
drwxrwxrwt   1 root root 4096 Feb 13 15:18 tmp
drwxr-xr-x   1 root root   32 Dec 26 00:00 usr
drwxr-xr-x   1 root root   39 Jan 21 10:00 var
[Pipeline] echo
b:null
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

As you can see the b variable is always set to null.

like image 511
carlo.zermo Avatar asked Feb 14 '19 10:02

carlo.zermo


People also ask

What is sh command in Jenkins pipeline?

On Linux, BSD, and Mac OS (Unix-like) systems, the sh step is used to execute a shell command in a Pipeline. Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Build') { steps { sh 'echo "Hello World"' sh ''' echo "Multiline shell steps works too" ls -lah ''' } } } }

What is BAT command in Jenkins pipeline?

From within a Jenkins pipeline you can any external program. If your pipeline will run on Unix/Linux you need to use the sh command. If your pipeline will run on MS Windows you'll need to use the bat command. Naturally the commands you pass to these will also need to make sense on the specific operating system.

How do I set environment variable in Jenkins pipeline stage?

Setting Stage Level Environment Variable It is by using the env variable directly in the script block. We can define, let us say, USER_GROUP and display it. You will see that the underlying shell also has access to this environment variable. You can also set an environment variable using withEnv block.


1 Answers

If you want to capture the output of sh step correctly then you need to replace

b = sh 'ls -l /'

with

b = sh script: 'ls -l /', returnStdout: true

The default behavior of sh step is that it prints the result to the console, so if you want to change its behavior you need to explicitly set returnStdout parameter to true.

like image 127
Szymon Stepniak Avatar answered Sep 23 '22 22:09

Szymon Stepniak