0

I have the following console output from a jenkins build:

23:21:16  [ ERROR ] - Problem occured while installing the chart AbCdEfG , aborting!

if the line appears, i want to be able to get only AbCdEfG and put it in error message, or in a varaiable. i tried something like:

if (manager.logContains('.*\${error_string}')) {
     error("Build failed because of ${AbCdEfG}")

regexp string that seems to be working, but stuck

[\n\r]*Problem occured while installing the chart:*([^\n\r]*)
sumitani
  • 228
  • 2
  • 11
  • def regex = '(?<=Problem occured while installing the chart).*?(?<=\\s).*?(?=\\s)' if (manager.logContains('.*Problem occured while installing the chart.*')) { def msg = manager.getLogMatcher(regex) error "$msg" i got to Here, but the regex is returning NULL :\ – Kobe Vaknin Dec 15 '20 at 23:02
  • Hi, can you edit your own question with the updates? Add EDIT or UPDATE would be enough. Regardless of the note, the example 4 (https://plugins.jenkins.io/groovy-postbuild/) won't work for you? getLogMatcher is not returning a msg but a Matcher. – sumitani Dec 16 '20 at 11:12

1 Answers1

3

Have you tested example 4 from plugin documentation? That example is using Java, not Groovy.

Anyway, the method getLogMatcher returns a Matcher, not a String.

Calling logContains will evaluate the log twice.

Using Groovy, (not tested inside Jenkins, only locally) you can adapt this snippet:

​    def matcher = ("23:21:16  [ ERROR ] - Problem occured while installing the chart AbCdEfG , aborting!" =~ /Problem occured while installing the chart\s?([^\n\r]*)\s?, aborting!/)

    /* One item from array is the group, first item from this group is the full match and the second is group match */
    if (matcher.hasGroup()) {
        def msg = matcher[0][1]
        println("Build failed because of ${msg}")
    }
sumitani
  • 228
  • 2
  • 11
  • it is working! thanks for the deatils answer!! – Kobe Vaknin Dec 16 '20 at 19:30
  • 1
    @KobeVaknin Great!! When what a person post it works or answer your question. if you are not waiting for other responses, please mark this as an accepted answer, otherwise, comment any doubts. :) – sumitani Dec 16 '20 at 20:21