30

I am using a Perl script to trigger a build in Jenkins using LWP modules. This works but after executing the job, I would like to parse the console output.

Is there someway to get this?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
dr.jekyllandme
  • 613
  • 2
  • 11
  • 22

3 Answers3

74

Log into Jenkins and take a look at the bottom of the webpage near the right hand side and click on the REST API link. This will give you information about the Jenkins RESTful API which is a great way to pull information off of Jenkins once you understand how to construct the URL.

And, here's how you get the console text:

$ curl "${JENKINS_URL}/job/${JOB_NAME}/lastBuild/consoleText"

You can use Perl's various LWP modules to talk to Jenkins.

Andy
  • 17,423
  • 9
  • 52
  • 69
David W.
  • 105,218
  • 39
  • 216
  • 337
  • Ugh… I was super excited when I saw this but it doesn't include the timestamps in the log messages!!! At least on version 1.595 – Clintm Jan 11 '18 at 15:20
  • 1
    I think the time stamps are not part of the log. It's an option for Jenkins to insert the time stamps when you view the log If you replace `consoleText` with `consoleFull` you'll get the log in HTML. You then need to parse that of course to extract the content. On Windows you can get that for free with Powershell. $resp = Invoke-Webrequest "$(JENKINS_URL)/job/$(JOB_NAME)/lastBuild/consoleFull" $resp.ParsedHtml.body.innerText > someFile.log (Powershell Core that is available also on Linux doesn't include ParsedHtml in the object due to a requirement to a MS DLL) – Ingemar Apr 04 '18 at 15:13
  • 2
    And now I found the real solution to this :D `${JENKINS_URL}/job/${JOB_NAME}/lastBuild/timestamps?appendLog&elapsed=HH:mm:ss.S` – Ingemar Apr 06 '18 at 13:43
  • 1
    @Ingemar I believe the `timestamps` api method is provided by the [Timestamper plugin](https://wiki.jenkins.io/display/JENKINS/Timestamper). It also offers parameters like `startLine`, `endLine`, `locale`, etc. – cod3monk3y Mar 02 '19 at 18:20
  • Is there a way to include the pending build too? When I trigger a build it takes 2 top 3 seconds to initialize, during that time `latestBuild` indeed returns the previous build. I hate to add a sleep or something. – nehem May 17 '19 at 03:53
  • I am getting authentication failure even after passing ?token={tokenname} as a request parameter with the above URL. Anything I am doing wrong ? – Vishal A Jun 11 '23 at 09:19
0

Use the Log Parser Plugin for Jenkins. Read the page number 24 here for the configuration and other details.

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
0

If you want to access the log from another Jenkins job that runs on the master, you can do it from a chained (child) job without needing to send an http GET. See https://stackoverflow.com/a/41974193/1580627.

Community
  • 1
  • 1
Ben
  • 313
  • 4
  • 8