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!
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.
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).
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:
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 serverreturnStdout: true
parameter to sh
step to return what your command prints to the console.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With