krkr
3/5/2014 - 3:59 PM

Groovy script to disable Jenkins Jobs which name contains "ping" or "post" if the last x result builds are failures

Groovy script to disable Jenkins Jobs which name contains "ping" or "post" if the last x result builds are failures

def ret = 0

def MAX_FAILURES = 3

def projects = jenkins.model.Jenkins.instance.projects.findAll { it.name.contains("ping") || it.name.contains("post") }
  
for (p in  projects) {
  
  def projectName = p.name
    
    if (p.builds.size() > MAX_FAILURES) {   
      
      def failures = p.builds[0..(MAX_FAILURES-1)].count { it.result == hudson.model.Result.FAILURE }
      
      if (failures == MAX_FAILURES) {
        println(" \033[31mKO\033[0m " + p.name + " \033[31m(the "+ MAX_FAILURES + " last builds have failed)\033[0m")
        p.disable()
        ret = 1
      } else {
      	println(" \033[32mOK\033[0m " + p.name)
      }
    }
}

return ret