【问题标题】:Reading a HTML file through Groovy Script通过 Groovy 脚本读取 HTML 文件
【发布时间】:2022-12-18 02:57:23
【问题描述】:

我需要使用 Groovy 编写 Jenkins 管道脚本,其中输入以下 HTML.

<table style="width:30%">
 <TR> 
 <TD>Failed Value 1</TD>
 <TD>2869</TD>
 </TR>
 <TR> 
 <TD>Failed Value 2</TD>
 <TD>9948</TD>
 </TR>
 <TR> 
 <TD>Failed Value 3</TD>
 <TD>3456</TD>
 </TR></table>

我从 Rest API 获取它,如果任何值超过 100,我需要触发一封电子邮件。

def response = httpRequest 'REST_API_URI'
println("Status: "+response.status)
def responseBody =  response.content
String[] TDcollection;
String[] splitData = responseBody.split("\n");
for (String eachSplit : splitData) {
  if (eachSplit.contains("Failed")) {
    print(eachSplit);
    }
  }

我试过这个,但无法获取值并验证它。

这可能看起来很容易,但因为我很

Groovy 的新手,我有点坚持它。提前致谢。

【问题讨论】:

    标签: jenkins groovy jenkins-pipeline jenkins-groovy jenkins-job-dsl


    【解决方案1】:

    毫不费力的常规:

    String input = '<table style="width:30%">  <TR>   <TD>Failed Value 1</TD>  <TD>2869</TD>  </TR>  <TR>   <TD>Failed Value 200</TD>  <TD>9948</TD>  </TR>  <TR>   <TD>Failed Value 3</TD>  <TD>3456</TD>  </TR></table>'
    
    //List<Integer> failedValues = ( input =~ /<TD>Failed Value (d+)</TD>/ ).findAll().collect{ it.last().toInteger() }
    
    List<Integer> failedValues = []
    input.eachMatch( /<TD>Failed Value (d+)</TD>/ ){ failedValues << it.last().toInteger() }
    
    assert failedValues == [1, 200, 3]
    
    boolean errorOccured = failedValues.any{ 100 <= it }
    
    assert errorOccured
    

    【讨论】:

    • 感谢分享这个。在管道脚本中实现它时出现以下错误。 org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:WorkflowScript:3:传播尚不支持 CPS 转换 @ 第 3 行,第 30 列。List<Integer> failedValues = ( input =~ /<TD>Failed Value (d+) </TD>/ ).findAll()*.last()*.toInteger() WorkflowScript:3:CPS 转换尚不支持传播 @ 第 3 行,第 30 列。List<Integer> failedValues = ( input =~ /< TD>失败值(d+)</TD>/ ).findAll()*.last()*.toInteger()
    • 查看更新,我用collect{}调用替换了传播
    • 感谢更新。但仍然面临一些问题。脚本不允许使用 staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods findAll java.lang.Object。管理员可以决定是批准还是拒绝此签名。 org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:脚本不允许使用 staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods findAll java.lang.Object
    • 使用eachMatch()查看更新
    猜你喜欢
    • 2019-05-04
    • 2017-02-22
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    相关资源
    最近更新 更多