Groovy Postbuild在Jenkins,解析日志字符串和数 [英] Groovy Postbuild in Jenkins, parsing the log for strings and counting them

查看:1273
本文介绍了Groovy Postbuild在Jenkins,解析日志字符串和数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Groovy的新手,我试图在Jenkins中设置一个postbuild,它允许我计算字符串,并确定创建是否成功通过多少计数返回结束。



这是我的示例代码:

  class Main {

def manager = binding.getVariable(manager)
def log = manager.build.logFile.text
def list = log
def JobCount = list.count { it.startsWith(====)&&&
if(JobCount == 7){
manager.listener.logger.println(所有作业成功完成)
} else {
manager.addWarningBadge(并非所有作业已成功完成)
manager.buildUnstable()
}
}

我正在寻找一个特定的字符串,当测试成功完成时打印到控制台。该字符串是==== JOB COMPLETE ====,如果所有7个测试正确传递,我应该有这个字符串的7个实例。



目前运行此代码时出现以下错误:

  Script1.groovy:6:意外令牌:if @ line 6,column 5. 
if(JobCount == 7)
^

任何帮助将非常感谢。

解决方案



你需要的是 readLines()

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

当然如下所述,Jenkins Groovy Postbuild插件运行Groovy脚本,所以你将摆脱包围类声明( Main


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

解决方案

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)

这篇关于Groovy Postbuild在Jenkins,解析日志字符串和数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆