GROOVY: Finding a string from console output if a regexp string found

1.6k views Asked by At

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]*)
1

There are 1 answers

2
sumitani On

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}")
    }