Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins - retrieve full console output during build step

I have been scouring the internet for days, I have a problem similar to this.

I need to retrieve the console output in raw (plain) text. But if I can get it in HTML that is fine too, I can always parse it. The only thing is that I need to get it during the build step, which is a problem since the location where it should be available is truncated...

I have tried retrieving the console output from the following URL's (relative to the job):

  • /consoleText
  • /logText/progressiveText
  • /logText/progressiveHTML

The two text ones are plain text and would be perfect if not for the truncation, same goes for the HTML one... exactly what I need - only its truncated....

I am sure it is possible to retrieve this information somehow, since when viewing /consoleFull there is a real-time update of the console, without truncating or buffering.

However, upon examining that web page, instead of finding the content I desired, I found this code where it should have been (I did not include the full pages code, since it would be mostly irrelevant, and I believe those answering would be able to find out and know what should be there on their own)

      new Ajax.Request(href,{
          method: "post",
          parameters: {"start":e.fetchedBytes},
        requestHeaders: headers,
          onComplete: function(rsp,_) {

          var stickToBottom = scroller.isSticking();
          var text = rsp.responseText;
          if(text!="") {
            var p = document.createElement("DIV");
            e.appendChild(p); // Needs to be first for IE
            // Use "outerHTML" for IE; workaround for:
            // http://www.quirksmode.org/bugreports/archives/2004/11/innerhtml_and_t.html
            if (p.outerHTML) {
              p.outerHTML = '<pre>'+text+'</pre>';
              p = e.lastChild;
            }
            else p.innerHTML = text;
            Behaviour.applySubtree(p);
            if(stickToBottom) scroller.scrollToBottom();
          }

          e.fetchedBytes     = rsp.getResponseHeader("X-Text-Size");
          e.consoleAnnotator = rsp.getResponseHeader("X-ConsoleAnnotator");
            if(rsp.getResponseHeader("X-More-Data")=="true")
              setTimeout(function(){fetchNext(e,href);},1000);
          else
              $("spinner").style.display = "none";
          }
      });

Specifically, I am hoping there is a way for me to get the content from text whatever it may be. I am not familiar with this language and so am not sure how I might be able to get the content I want. Plugins won't help since I want to retrieve this content as part of my script during the build step

like image 951
Inbar Rose Avatar asked Jul 22 '13 15:07

Inbar Rose


People also ask

Where does Jenkins store console output?

On Windows, Jenkins log files are stored as jenkins. out (console output logs) and jenkins. err (error logs) in the Jenkins home folder. Note: Jenkins logs may be stored in the Jenkins installation folder in Program Files on some Windows systems.


2 Answers

You did pretty much good investigation already. I can only add the following: all console related plug-ins I know are designed as a post build actions.

The Log Trigger plugin provides a post-build action that allows Hudson builds to search their console log for a given regular expression and if found, trigger additional downstream jobs.

So it looks like there is no straightforward solution to your problem. I can see the following options:

1. Use tee or something similar (applicable to shell build steps only)

This solution is far from being universal, but it can provide quick access to the latest console output, produced by a command or set of command.

tee - read from standard input and write to standard output and files

Using synonyms on the system level other Jenkins build steps can modified in order to produce console output. File with console output can be referenced through Jenkins or using any other way.

2. Modify Jenkins code

You can just do a quick fix for internal usage or provide a patch introducing specific system-wide setting.

3. Mimic /console behavior

Code in your example is used to request updates from the Jenkins server. As you may expect the server side can return piece of information starting with some offset. Let me show.


Periodically console page sends requests to the server:

enter image description here


Parameters are straightforward:

enter image description here


Response is a chunk of information to be added:

enter image description here


Another request with updated offset (start) value

enter image description here


You can easily understand there is no data by analyzing Content-Length

enter image description here


So the answer is: use url/job-name/build-number/logText/progressiveHtml, specify start offset, send request and receive console update.

like image 197
Renat Gilmanov Avatar answered Sep 22 '22 12:09

Renat Gilmanov


I had a similar issue, the last part of my Jenkinsfile build script needs to parse the ConsoleLog for particular error messages to put in an email build report.

First attempt: http request.
It felt like a hack, it mostly worked, but ran into issues when we locked down access to the Jenkins server & my build nodes could no longer perform annon http gets on the page

Second attempt: use the APIs to enumerate the log lines.
It felt like the right thing to do, but it failed horribly as my nodes would take 30 minutes to get through the 100 meg log files. My presumption is that the Jenkins server was not caching the file, so each request involved a re-reading of the entire file up until the point of the last read.

Third and most successful solution: run grep on the server.

node('master') {  
  sh 'grep some_criteria $JENKINS_HOME/workspace/path/to/job/console.log'  
}

it was fast, reliable, and it didn't matter how big the log files were.

Yes, this required trust of the Jenkins admin and knowledge of the directory paths on the Jenkins server - but since I was the admin, I trusted myself to do the right thing. Your mileage may vary.

like image 32
Jason De Arte Avatar answered Sep 22 '22 12:09

Jason De Arte