How can I configure Jenkins to show my logs in the

2019-02-16 13:18发布

问题:

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?

回答1:

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:

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:

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

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:

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



回答2:

If you mean the general output of a Jenkins build, this is available via the web UI for each build, under the "Console Output" link.

If you mean a specific log file generated in the workspace during the build, you can choose the "Archive the artifacts" option in the "Post-build steps" section of your job configuration.

There you can specify a file pattern, e.g. **/*.log.

Any files in the workspace matching that pattern will be archived at the end of each build, and will be visible on the build's web page.

Note that the build will fail if no files match this pattern.



回答3:

You can create logs during the build and attache them as an artifact however the jenkins core does not allow you to save the console log to the workspace. If you want to save the console log you will need to install the ConsoleLogToWorkspace plugin. This plugin will give you the ability to save the entire jenkins console log.