I’m stuck on this, so I hope you can help me out …
In my Jenkinsfile there’s a section where I start collecting files into a tar-archive. Depending on the input parameters there’ll be between 20 and 200+ files that go into the tar-file.
Unfortunately tar doesn’t support any “quiet mode” … so a quick search suggests to append “>/dev/null 2>&1” to the command line
But this just dumps everything and we’ll need/want some kind of “debug mode” you could toggle.
So I created a variable: env.noTarMsg = “>/dev/null 2>&1”
… and my code looks like this:
env.noTarMsg = ">/dev/null 2>&1"
[...]
if (! debug) {
env.noTarMsg = ""
}
sh label: ('taring ...'), script: '''
tar -cfv myArchive.tar $File1 $File2 $File3 $noTarMsg
'''
[...]
Now two things happen here.
Jenkins belives these are two-Stings instead of one !
it treats the arguments as filenames and tries to add 'em to the tar-archive.
The only idea I have at the moment is moving your tar command into a shell file, and getting the shell file to interpret the $noTarMsg.
You wouldn’t then call directly tar but your new shell file in the Jenkins command.
My $0.02.
so because your using single quotes (3 of them,but single), groovy will do no interpretation and your shell will handle all the variables. I don’t think you can do redirection inside of shell environmental variables.
So options.
Do groovy string manipulation (like you suggested).
You probably just want to drop -v for non debug, that can be done in single quotes (-v is verbose, non v would be quiet)
use script { logs = sh(returnStdout: true, script) } then echo logs in debug mode