CONTEXT
Jenkins prints out commands as they happen, mostly on Unix and this can be annoying if you don’t want a lot of Jenkins console logs, especially if they are just more like duplicates of your commands. This is an automatic out of the box behaviour. There’s a way we can tell Jenkins to instruct the machine to turn off these verbose duplicate commands.
SETTING UP THE INSTRUCTION
COMMAND CONTEXT
To achieve this, we can look into the Unix Bash’s Set -x
and Set +x
commands. So setting the “x” mainly instructs Bash to perform extra duplicate print out of commands as they are being run or executed. See this is another useful way of debugging your Bash scripts if you want to know the statements that are being executed and also the order they are being executed.
To toggle this on and off one can do any of the following :
- On the command line via the
Set -x
or
Set +x
command to enable or disable the printouts. - Via the “Shabang” line in your script by appending the
-x
or+x
.
Now the confusing thing is that the commands work the opposite way, i.e. the
is to enable while the -x
+x
is to disable the printouts. So this is just a note right there. You can try these commands in your Bash terminal and scripts, I am going to just jump into Jenkins since that’s the point of this article.
WRITING THE JENKINS SCRIPT
In my previous Jenkins article Jenkins : Importing Your Custom Environment Variables I had a Jenkins job that ran and we can use that to showcase the Set -x
and +x
commands. Looking at the image :
Jenkins Annoying Duplicate Echos |
You will see the highlighted line in a purple block. This is the annoying line I would want to get rid of. At the moment my Job Script is as follows :
Script Before Disabling The Command Echos |
This is before we use the set command to disable the echos, now let’s change the script to disable the echos.
As mentioned this can be done in two way, we can use the “Shabang” line at once with some like : #!/bin/bash +x
Script Updates To Disable The Command Echos Using Shabang |
Or you can just use the Set +x
command :
Script Updates To Disable The Command Echos Using Set Command |
Either way you will get the same effect. The only difference is that using the shabang line makes sure the echos are off before running any line in you script while the set command will have the echos until the set command is executed. That’s all.
TESTING IT OUT
Run your Job and you will spot that the the annoying “echo” line we had before is now gone. You may refer to my results below :
Script After Disabling The Command Echos |
CONCLUSION
And there you have it, you now see one of the many possible ways of disabling the duplicate command echos as they are being executed. I hope this is yet another helpful tip to disabling the command echos as they happen. Let me know what you think, Cheers!
No comments:
Post a Comment