Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2018-11-07T11:34:33.633Z
Answers: 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.
Answers: Bruce Becker
Answers: CC BY-SA 4.0
Answers: 2018-11-07T12:56:10.633Z
Answers: 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
Answers: Tensibai
Answers: CC BY-SA 4.0
Answers: 2019-04-06T06:10:57.160Z
Answers: 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?
Answers: Narasimha
Answers: CC BY-SA 4.0
Answers: 2018-11-07T11:33:33.440Z
Answers: 2018-11-07T12:59:00.503Z
Answers: 2018-11-07T12:59:00.503Z
Answers: Bruce Becker
Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2021-09-29T13:59:34.620Z
Answers: 2021-09-29T13:59:34.620Z
Answers: Fearnbuster
Body: <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>
  
Comments: CC BY-SA 4.0
Comments: 2018-11-07T11:05:44.857Z
Comments: When you say "pipeline" - do you mean "declarative pipeline"?
Comments: Bruce Becker
Tags: <jenkins-pipeline><salt><groovy>
Title: Jenkins Groovy to parse console output and mark build failure
UserDisplayName: Narasimha
ThreadText: # 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](https://stackoverflow.com/questions/36188512/search-through-console-output-of-a-jenkins-job#36190644), 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](https://jenkins.io/doc/book/pipeline/syntax/#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**: username_1, 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<String>`) 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)
  
  
  ---
  
  