How to Collect and Display Test Logs in Jenkins

Harsh Murari
Feb 26, 2018 · 2 min read

I was recently faced with a requirement to show test result logs (which is a zip file containing various runtime logs from an embedded device) on Jenkins webpage. Though the solution is simple, it was hard enough to find on Google. What was I going to search? “How to collect Jenkins Logs” returned a bunch of results with Jenkins framework logs, not my build logs. So I thought I would put together this post which might help someone quickly enable showing their build/test logs on Jenkins webpage.

The word you are looking for is “artifact”. Jenkins Build Artifacts refers to run-time logs generated by your build or test scripts. By default, Jenkins does not collect any logs from Slave machines to Master for display. But these can be enabled through Build Artifacts.

First, I wrote a Python script which would download a bunch of logs from my embedded instrument which is connected to the Jenkins slave machine. This python script was pretty straightforward — and called a REST API to get the logs from the instrument. Below is the code for it (for your case — it is most likely not needed, as you would have alternate ways of collecting test logs).

def collect_instrument_logs():    filename = ‘logs.zip’    ret = requests.get(urljoin(server_ip, ‘/api/system/logs’), stream=True)    with open (filename, ‘wb’) as f:        for chunk in ret.iter_content(chunk_size=1024):            f.write(chunk)

Next, I enabled running this script after the job through the pipeline script. For those using Freestyle Jenkins jobs, replace this step by enabling a post-build action doing the same thing.

stage(‘Log Collection’) {
dir(‘series_5-test/tests/utils’) {
timeout(5) {
echo ‘Collecting Instrument Logs’
bat ‘python collect_instrument_logs.py’
}
}
}

Now, you can open Jenkins Pipeline Editor which is available next to the job and select “archiveArtifacts” option to generate Pipeline Code for archiving build artifacts.

Jenkins Pipeline Syntax and Archive Artifacts Script Generator.

That’s it! Copy this code into your pipeline code as follows.

stage(‘Log Collection’) {
dir(‘series_5-test/tests/utils’) {
timeout(5) {
echo ‘Collecting Instrument Logs’
bat ‘python collect_instrument_logs.py’
archiveArtifacts '*.zip'
}
}
}

Now your Jenkins job results would include the zip file collected during each run.

Happy Hacking!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store