<p>This question seems similar to <a href="https://stackoverflow.com/questions/36188512/search-through-console-output-of-a-jenkins-job#36190644">another on StackOverflow</a>, but seems to be a close, but not exact duplicate of 
<a href="https://stackoverflow.com/questions/37018509/jenkinsfile-build-log">https://stackoverflow.com/questions/37018509/jenkinsfile-build-log</a></p>

<p>To quote those : </p>

<blockquote>
  <p>if you just want to check, that your log contains string myTestString you can just call manager.logContains('.<em>myTestString.</em>')</p>
  
  <p>If you want to get some information from the first matching line you can use manager.getLogMatcher(regexp)</p>
</blockquote>

<p>You could use the <a href="https://jenkins.io/doc/book/pipeline/syntax/#flow-control" rel="nofollow noreferrer">Groovy flow control</a> to break the build - you just need a step to execute the test:</p>

<p>Following the example on the link : </p>

<pre><code>node {
    stage('CheckLog') {
      steps {
        if (manager.logContains('.*myTestString.*')) {
          error("Build failed because of this and that..")    
        }
      }
    }
</code></pre>

CC BY-SA 4.0
2018-11-07T11:34:33.633Z
the other two questions which this is similar to don't actually ask how to fail the build based on the contents of the log, just how to get the contents of the log, so technically, this is not a duplicate.
Bruce Becker
CC BY-SA 4.0
2018-11-07T12:56:10.633Z
Agree with the not duplicate, and usually we do not close cross duplicates unless there's really no more in this site approach on the question than the other site. In brief good job bringing this together and adding the missing point
Tensibai
CC BY-SA 4.0
2019-04-06T06:10:57.160Z
Bruce, what if I want to match multiple patterns like if (manager.logContains('.*myTestString.*') || ('.*myTestString2.*')) )? I tried multiple options but it don't work. Any suggestion?
Narasimha
CC BY-SA 4.0
2018-11-07T11:33:33.440Z
2018-11-07T12:59:00.503Z
2018-11-07T12:59:00.503Z
Bruce Becker
<p>I solved this problem by following <a href="https://stackoverflow.com/a/38444093/3513260">this answer on StackOverflow</a>.</p>
<p>You can use <code>currentBuild.rawBuild.getLog(10)</code> (the <code>10</code> specifies that you want the last 10 lines) to get the last few lines of the log stream.</p>
<p>According to <a href="https://javadoc.jenkins-ci.org/hudson/model/Run.html#getLog-int-" rel="nofollow noreferrer">this documentation</a>, this method returns a List of Strings (<code>List&lt;String&gt;</code>) where each index contains a single line of the log stream.</p>
<p>You can either search line-by-line for what you want, or you can combine the entire List into a single String using the <code>.join()</code> method.</p>
<p>To search the actual String, you can use the <code>.contains()</code> method or you can use a <a href="https://docs.groovy-lang.org/next/html/documentation/#_regular_expression_operators" rel="nofollow noreferrer">RegExp</a></p>

CC BY-SA 4.0
2021-09-29T13:59:34.620Z
2021-09-29T13:59:34.620Z
Fearnbuster
<p>What would be a Groovy or pipeline step to parse Jenkins console output, identify a string and mark the build as failure if the string is matched?</p>

CC BY-SA 4.0
2018-11-07T11:05:44.857Z
When you say "pipeline" - do you mean "declarative pipeline"?
Bruce Becker
<jenkins-pipeline><salt><groovy>
Jenkins Groovy to parse console output and mark build failure
Narasimha
# Jenkins Groovy to parse console output and mark build failure

(Asked by: username_0 on 2018-11-07)

What would be a Groovy or pipeline step to parse Jenkins console output, identify a string and mark the build as failure if the string is matched?

**Tags:** jenkins-pipeline, salt, groovy

> **Comments:**
> **username_1**: When you say "pipeline" - do you mean "declarative pipeline"?  

---

## 2 answers

### Answer by username_1 on 2018-11-07. Score: 5

This question seems similar to another on StackOverflow, but seems to be a close, but not exact duplicate of 
https://stackoverflow.com/questions/37018509/jenkinsfile-build-log
To quote those : 

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)

You could use the Groovy flow control to break the build - you just need a step to execute the test:
Following the example on the link : 
node {
    stage('CheckLog') {
      steps {
        if (manager.logContains('.*myTestString.*')) {
          error("Build failed because of this and that..")    
        }
      }
    }

> **Comments:**
> **username_1**: the other two questions which this is similar to don't actually ask how to fail the build based on the contents of the log, just how to get the contents of the log, so technically, this is not a duplicate.  
> **username_2**: Agree with the not duplicate, and usually we do not close cross duplicates unless there's really no more in this site approach on the question than the other site. In brief good job bringing this together and adding the missing point  
> **username_0**: Bruce, what if I want to match multiple patterns like if (manager.logContains('.*myTestString.*') || ('.*myTestString2.*')) )? I tried multiple options but it don't work. Any suggestion?  

---

### Answer by username_3 on 2021-09-29. Score: 1

I solved this problem by following [this answer on StackOverflow](https://stackoverflow.com/a/38444093/3513260).

You can use `currentBuild.rawBuild.getLog(10)` (the `10` specifies that you want the last 10 lines) to get the last few lines of the log stream.

According to [this documentation](https://javadoc.jenkins-ci.org/hudson/model/Run.html#getLog-int-), this method returns a List of Strings (`List`) where each index contains a single line of the log stream.

You can either search line-by-line for what you want, or you can combine the entire List into a single String using the `.join()` method.

To search the actual String, you can use the `.contains()` method or you can use a [RegExp](https://docs.groovy-lang.org/next/html/documentation/#_regular_expression_operators)


---

