How do we get the python print statements logged in the Jenkins Console?

Hello folks and welcome back to my blog! It's been a while and I thoguht I could keept this one short and talk about Jenkins + Python!

As part of an experiment, I was working on integrating a Python script with Jenkins an noticed that I was not able to see any logs on the console output section! After a bit of research, I realized that python has a buffered and un-buffered input output system.

Very simply put, a buffered output is when there is a temporary storage area (buffer) for all the messages that are part of the standard out. The messages are then served from the buffer area to the output device. Contrary to this, unbuffered output is basical service the data to the output device as they come (without the buffer in place). More information about this can be found here.

Now, python by default has a buffered stdout. Therefore, in order to switch to an unbuffered output for stdout, run your python program in your jenkins console shell execution step with the flag -u.

python -u script.py

Source: https://stackoverflow.com/questions/22826006/how-to-get-python-print-result-in-jenkins-console-output

I hope you learned something new! :)

Previous
Previous

How to use git diff to compare files from different branches?

Next
Next

How to deploy your own Jenkins Instance?