Saturday, January 15, 2022

Jenkins : Making Job Console Logs Less Verbose

 

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 -x is to enable while the +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!

 

 

 

Jenkins : Importing Your Custom Environment Variables

 

CONTEXT

You are working on Jenkins and you want setup and work with some of your own custom Environment Variables and you discover that Jenkins cannot pickup your machine environment variables. Today we are going to look at how you can load your own custom variables that you want to use inside your Jenkins Scope.

 

GETTING STARTED

Start by creating a new Job that just prints out the value from any of your machine environment variables, I have JBOSS_HOME. You will notice that the value from your machine environment variable did not print. So it’s not you it’s just the behaviour of Jenkins. 

 


echo 'The System ENV variable in my bash profile is : ${JBOSS_HOME}"



Simple Jenkins Print Out Shot


I am guessing that’s why there’s a reason we have a special plugin we can use.


INSTALLING THE PLUGIN

Head over to the plugins section of Jenkins Manage Jenkins > Plugin Manager and search for the plugin named "Environment Intjector"



Environment Injector Plugin Installation

 

Restart Jenkins when the installation is complete and then let’s configure the variables next. 

 

CONFIGURING YOUR CUSTOM VARIABLES

Now that the plugin is installed we can start configuring it. In our current case we want to add a random simple Environment Variable and then we will print it out. To do start by heading to the configurations section in Jenkins as follows : Manage Jenkins > Configure System


Manage System Configurations


Once you are on the Configuration page then, look for the heading or section, “Global properties”. You will notice a check box labelled, “Prepare jobs environment”. Check that box where a text box will show at the bottom labelled, “Properties File Path”.

 

Things to note well here are :

  • One more thing to note very well is that the text box label states clearly that it expects a .properties file path. This means that you must create a .properties file which will be the one that contains your variables.
  • The path you specify must be an absolute path, you cannot use the your OS short cuts like ~/ and ${HOME}/ . so for example I have a path named tools/jenkins/jenkins-environment.properties in my home directory

 

So modify your .properties file and add a key = value par line with a key and value of your choice, I have my first name in the file as follows : ENV_MY_FIRST_NAME = Thabo which I will be printing in Jenkins shortly.

The next thing is to configure the text box path to where your file is : /Path/to/your/environment/file.properties

 

Jenkins Environment Variables File Configuration


Click the “Apply” and then the “Save” buttons. The last thing to do now it to restart Jenkins so that it becomes aware of the new Environment Variables.

 

CREATING / UPDATING THE JOB TO USE THE CUSTOM VARIABLE

So now we have configured all we wanted to configure. Let’s look into creating or updating a Job that will perform a simple variable printout, that’s all. So remember that my key = value pair is : ENV_MY_FIRST_NAME = Thabo so this means that I can now reference the key of the value I want to use. Check out my Job Shell Script below :



echo 'The System ENV variable in my bash profile is : ${ENV_MY_FIRST_NAME}"


Jenkins Shell Referencing Custom Environment Variable


Click the “Apply” and then the “Save” buttons.


TESTING IT OUT

The moment we have been waiting for, you may now run your Job and checkout the console logs. Checkout the image below for my results :



Jenkins Environment Variable Printing Results


CONCLUSION

This is how you can configure Jenkins to use your own custom environment variables. I hope this is useful to your Jenkins usage. Let me know what you think. Cheers!