2

I am new to Groovy and am trying to set up a postbuild in Jenkins that allows me to count strings and determine if the build succeeded by how many the count returns at the end.

Here is my example code :

class Main {

  def manager = binding.getVariable("manager")
  def log = manager.build.logFile.text
  def list = log
  def JobCount = list.count {it.startsWith("====") && it.contains("COMPLETE")}
  if (JobCount == 7) {
      manager.listener.logger.println("All Jobs Completed Successfully")
  } else {
      manager.addWarningBadge("Not All Jobs Have Completed Successfully")
      manager.buildUnstable()  
  }
}

I am looking for a specific string that gets printed to the console when the test has completed successfully. The string is "====JOB COMPLETE====" and I should have 7 instances of this string if all 7 tests passed correctly.

Currently when I run this code I get the following error :

Script1.groovy: 6: unexpected token: if @ line 6, column 5.
   if (JobCount == 7)
   ^

Any help would be greatly appreciated

andrewkro
  • 23
  • 1
  • 6

3 Answers3

4

manager.build.logFile.text returns the whole file text as String.

What you need is readLines():

def list = manager.build.logFile.readLines()
def JobCount = list.count {it.startsWith("====") && it.contains("COMPLETE")}

and of course as mentioned below, the Jenkins Groovy Postbuild plugin runs Groovy scripts, so you will have get rid of the enclosing class declaration (Main)

Ori Dar
  • 18,687
  • 5
  • 58
  • 72
2

You have statements directly inside your class, without being in a method, which is not allowed in Java/Groovy. Since this is Groovy, you can run this as a script without the class at all, or put the offending code (the if statement) inside a method and call the method.

thecodesmith_
  • 1,277
  • 8
  • 18
  • I moved everything out of the class as you had mentioned. I was not aware that was unnecessary, thanks for that. Now when I run this without the main class, I get an error, that says no signature of method : java.lang.string.count() is applicable for argument types: (Script1$_run_closure1) values: [Script1$_run_closure1@77a3b365] – andrewkro Aug 19 '15 at 19:02
  • After that it says Possible solutions: count(java.lang.CharSequence), count(java.lang.String), toSet(), toSet(), toURL(), toURL() – andrewkro Aug 19 '15 at 19:02
0

Perhaps you're missing a closing }

def JobCount = list.count {it.startsWith("====") && it.contains("COMPLETE")}
Stanton
  • 497
  • 3
  • 14