Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure Jenkins to show my logs in the workspace?

I am using Jenkins for CI - and want to be able to expose the logs so we don't have to telnet onto the CI box to see what's going on.

Is there a plugin that will do this? Or should I just need to write a script?

like image 545
laura Avatar asked Feb 14 '12 16:02

laura


1 Answers

My answer is about reading application server/container log files from different boxes than Jenkins is running on. For example, if you use Jenkins to build your wars and then deploy them to several environments and you want to read the log files from your Jenkins CI server without having to manually ssh/telnet the other boxes.

You can create a Jenkins Project/job that will do that for you, after creating the Jenkins project you will find the Configure option and inside that there is an option in the Build section where you can write a shell script:

Shell script area

Your script can be something like this:

#!/bin/sh

USER=<your ssh user>
HOST=<your remote host>
LOG_DIR=<your log file location>
FILE=<your log file>

cmd="ssh $USER@$HOST cat $LOG_DIR/$FILE"
echo $cmd
$cmd

Or something more complex, for example passing parameters before building the Jenkins Job so your users can select which environment and log file they want to see, there is an option called "This build is parameterized" that you can select and add your parameters:

Adding parameters

With that configuration the user will see something like this when trying to build the project:

enter image description here

The shell script can use those params like this:

#!/bin/sh

USER=<your ssh user>
LOG_DIR_DEV=<log files location in dev>
LOG_DIR_QA=<log files location in qa>

if [ $ENVIRONMENT = "dev" ]; then
  HOST=<your remote host for dev>
  LOG_DIR=$LOG_DIR_DEV
fi
if [ $ENVIRONMENT = "QA" ]; then
  HOST=<your remote host for qa>
  LOG_DIR=$LOG_DIR_QA
fi

echo ENVIRONMENT=$ENVIRONMENT
cmd="ssh $USER@$HOST cat $LOG_DIR/$FILE"
echo $cmd
$cmd

After building the Job they can see the output from the logs in the Jenkins Job Console for example:

Console Output

If your log files are too long maybe you can use "tail" instead of "cat" in the script.

like image 101
raspacorp Avatar answered Oct 26 '22 00:10

raspacorp