Search through console output of a Jenkins job

拜拜、爱过 提交于 2019-12-04 00:47:42

I often use the Jenkins Script Console for tasks like this. The Groovy plugin provides the Script Console, but if you're going to use the Script Console for periodic maintenance, you'll also want the Scriptler plugin which allows you to manage the scripts that you run.

From Manage Jenkins -> Script Console, you can write a groovy script that iterates through the job's builds looking for the matching string:

JOB_NAME = "My Job"
BUILD_STRING = "Hello, world"

def job = Jenkins.instance.items.find { it.name == JOB_NAME }
for (build in job.builds) {
  def log = build.log
  if (log.contains(BUILD_STRING)) {
    println "${job.name}: ${build.id}"
  }
}

If there is no additional requirements I would do it simply in the shell, e.g.:

find $JENKINS_HOME/jobs/haystack -name log -exec grep -l needle {} \; \
    | sed 's|.*/\(.*\)/log|\1|'

Thanks everyone for your valuable solutions. After a bit of additional research i found that there is a plugin in Jenkins to do this.

https://wiki.jenkins-ci.org/display/JENKINS/Lucene-Search

This will save the console output results and users can do search in search box.

There is the Log Parser Plugin

highlighting lines of interest in the log (errors, warnings,information)

dividing the log into sections displaying a summary of number of errors, warnings and information lines within the log and its sections.

linking the summary of errors and warnings into the context of the full log, making it easy to find a line of interest in the log

showing a summary of errors and warnings on the build page

If it is old logs then @jil has the answer assuming you are on Linux.

Just to throw another plugin out there, this blog post pointed me at the TextFinder plugin which allows you to search for text in either a workspace file or the console output, and override the build status as success/failure when the text is found.

The original poster doesn't say what should happen when the text is found, but it was searching for this functionality that brought me here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!