Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the output of a shell script in a Jenkinsfile?

In a Jenkinsfile Groovy script stage, say I want to issue a linux command that outputs rows and columns of strings, and want to get the nth column in the output of a certain row. An example of such command is "ls -al." So I do this correct?

stage("Get dir size") {
  sh returnStatus: true, script: '''
    LINE=`ls -al | grep some_dir`
    IFS=" " read -ra COLS <<< $LINE
    echo ${COLS[4]}
  '''
  /* I want to use the value of ${COL[4]} after above block */
}

But how do I get the value of essentially ${COL[4]}, which is the fifth column in an "ls -al" command, which is the directory size?

Thanks!

like image 950
Chris F Avatar asked May 31 '18 02:05

Chris F


1 Answers

This bash script you have shown in your example won't return correct directory size. It will return a size of a file, usually 4096 bytes and not a total size of all files and subdirectories recursively. If you want to get the total directory size you can try something like this:

#!groovy

node('master') {

  stage("Get dir size") {
    script {
      DIR_SIZE = sh(returnStdout: true, script: 'du -sb /var/jenkins_home/war/jsbundles | cut -f1')
    }
    echo "dir size = ${DIR_SIZE}"
  }
}

Crucial part is to use sh step with returnStdout enabled so you can capture inside a variable what the script prints out to the console. In this example I am calculating a total size of /var/jenkins_home/war/jsbundles folder and when I run this pipeline script I get:

dir size = 653136

You can then use DIR_SIZE variable as an input in later pipeline steps.

Alternative approach: using Groovy's File.directorySize()

Instead of using a bash script you could consider using Groovy's built-in method File.directorySize(), something like:

#!groovy

node('master') {

  stage("Get dir size") {
    script {
      DIR_SIZE = new File('/var/jenkins_home/war/jsbundles').directorySize()
    }
    echo "dir size = ${DIR_SIZE}"
  }
}

However, this approach will give you a different result comparing to the use case with bash command:

dir size = 649040

This is because Groovy's File.directorySize() method calculates the result as a sum of all file sizes recursively and it does not take into account size of directory file. In this example the difference is 4096 - a size of a directory file /var/jenkins_home/war/jsbundles (this path does not contain any subfolders, only bunch of files).

Update: extracting data from column-like output

You can extract any information from column-like output by piping commands like grep and cut together. For example, you can replace above examples with:

#!groovy

node('master') {
  stage("Get dir size") {
    script {
      DIR_SIZE = sh(returnStdout: true, script: 'ls -la /var | grep jenkins_home | cut -d " " -f5')
    }
    echo "dir size = ${DIR_SIZE}"
  }
}

And for following output:

total 60
drwxr-xr-x  1 root    root    4096 Nov  4  2017 .
drwxr-xr-x  1 root    root    4096 May 31 03:27 ..
drwxr-xr-x  1 root    root    4096 Nov  4  2017 cache
dr-xr-xr-x  2 root    root    4096 May  9  2017 empty
drwxr-xr-x  2 root    root    4096 Nov  4  2017 git
drwxrwxr-x 20 jenkins jenkins 4096 May 31 12:26 jenkins_home
drwxr-xr-x  5 root    root    4096 May  9  2017 lib
drwxr-xr-x  2 root    root    4096 May  9  2017 local
drwxr-xr-x  3 root    root    4096 May  9  2017 lock
drwxr-xr-x  2 root    root    4096 May  9  2017 log
drwxr-xr-x  2 root    root    4096 May  9  2017 opt
drwxr-xr-x  2 root    root    4096 May  9  2017 run
drwxr-xr-x  3 root    root    4096 May  9  2017 spool
drwxrwxrwt  2 root    root    4096 May  9  2017 tmp

it will extract 4096 - the jenkins_home file size.

Things worth remembering:

  • use simple bash scripts like ls -la /var | grep jenkins_home | cut -d " " -f5. The example you have shown above does not work in my local bash as well as in the Jenkins server
  • add returnStdout: true parameter to sh step to return what your command prints to the console.
like image 163
Szymon Stepniak Avatar answered Sep 27 '22 22:09

Szymon Stepniak