Jenkins console script with power

Problem

Many times I have been on a jenkins script console wanting to do a shell script using the pipe character to chain calls together.

du -sh --max-depth=1 | sort -h

Jenkins Script console default scripting

The suggested way of doing things leaves much to be desired.  It does give you groovy to do queries in but it is not very powerful.

println "uname -a".execute().text

The Good

Works for default simple single commands.

The Bad

You cannot pipe a command to another and white space in directory names does not work.

Step better

println(['ls', '/tmp/folder with spaces'].execute().text)

The Good

This to allow folders with spaces by turning command into array of strings. You do not have to escape any spaces and it will be able to deal with it.

The Bad

You cannot chain commands together

The better Jenkins script console command

Source of Jenkins shell with pipe discussion

println new ProcessBuilder( 'sh', '-c', 'du -h --max-depth=1 /var/foo/bar/ | sort -hr').redirectErrorStream(true).start().text

The good

Can chain commands together

The Bad

Have to escape whitespace in the java way with \\

println new ProcessBuilder( 'sh', '-c', 'du -h --max-depth=1 /var/foo/bar/folder\\ with\\ spaces | sort -hr').redirectErrorStream(true).start().text
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s