20

Is there any builtin variable that gives access to the text of the currently executing build?

I tried using something like currentBuild.log, currentBuild.buildLog but without any luck.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115

4 Answers4

21

Actually it is possible using currentBuild.rawBuild.log or better (not deprecated) currentBuild.rawBuild.getLog(100) (for the last 100 lines), reference: http://javadoc.jenkins-ci.org/hudson/model/Run.html#getLog-int-

Dave Anderson
  • 11,836
  • 3
  • 58
  • 79
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • 3
    With 1.652 multibranch pipeline, that doesn't work :( `org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method hudson.model.Run getLog int` & the my.jenkins.site/scriptApproval/ page doesn't even show it. I'm still looking for an answer myself to this vexing problem – Jason De Arte Sep 20 '16 at 21:54
  • @JasonDeArte - Have you tried this on the current Jenkins LTS with current plugin versions? – BitwiseMan Jan 14 '17 at 00:12
  • Took me a long time to find this answer. This provides one way to tell if a build was aborted, which isn't clearly provided yet - https://issues.jenkins-ci.org/browse/JENKINS-28822 – Display Name is missing Apr 04 '17 at 20:10
  • 1
    How can I use get ALL the logs of the current build? currentBuild.rawBuild.getLog(100) works but currentBuild.rawBuild.getLog() is not working (empty) while I was hoping for all the logs. – DenCowboy Dec 05 '17 at 13:17
10

I searched a lot for a solution to analyze the log.

  • use rawBuild was not OK, because I want to execute my scripts in a sandbox without additional approvals
  • use tee in the steps, that I want to analyse was not OK, because I don't want to modify previous steps, nor I don't want to have my whole log in RAM (and unfortunately I needed that on a Windows machine)

I found a solution inspired by Jesse Glicks answer:

  • Under my.jenkins.url/pipeline-syntax/globals you can see, that the manager-variable allows you to analyse the log by using manager.logContains(regexp) or manager.getLogMatcher(regexp)
  • So if you just want to check, that your log contains string myTestString you can just call manager.logContains('.*myTestString.*')
  • If you want to get some information from the first matching line you can use manager.getLogMatcher(regexp)

Unfortunately I found no way to analyze the whole log (getLogMatcher returns only the first matching line Matcher). So I see currently no way to e.g. count how often a log file contains a special string.

Sergej Werfel
  • 1,335
  • 2
  • 14
  • 25
  • 3
    there is no more "manager" in jenkins/pipeline-syntax/globals as for august 2018. But there is "currentBuild" that has rawBuild property that has getLog(int). – Andrey Regentov Aug 21 '18 at 04:30
3

Not currently. (Properties of currentBuild are documented under Snippet Generator » Global Variables by the way.)

It could be implemented, fairly easily, though it would not scale well with huge builds. JENKINS-28119 would provide a more scalable solution to what I am guessing your underlying request is.

Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
  • Actually I'm trying to parse the logs to retrieve all deployed artifacts (`mvn clean deploy`) and in case the server tests fail I want to remove those artifacts from nexus repository, the easiest way currently for me is to parse the logs for *Uploaded: http.....* text and do HTTP DELETE on them. – Krzysztof Krasoń May 06 '16 at 14:24
  • If you are versioning your deployed artifacts with some variable which you can access, like your build number or your git commit, you can maybe delete them by building the artifact names using the variable. If you are publishing hundreds of artifacts that won't work too well though – Mark Chorley May 23 '16 at 15:39
  • 1
    Then just `sh 'mvn clean deploy | tee logs'; def logs = readFile 'logs'` without trying to access the logs of the _build_ as a whole. – Jesse Glick May 24 '16 at 18:02
1

Since Jenkins gives you BUILD_URL, you can download it:

sh('wget -q --no-check-certificate -O build.log ' + BUILD_URL + 'consoleText')
Axel Heider
  • 557
  • 4
  • 14
  • Note that this only work when anonymous read access to jenkins builds is allowed. But that might be better than using `currentBuild.rawBuild.log` and allow access to `rawBuild` for scripts. – Axel Heider Aug 24 '22 at 14:37