Reading a HTML file through Groovy Script

I need to write a Jenkins pipeline script using Groovy where the below HTML is the input.

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

I am getting it from a RestAPI, and if any of the value is more than 100 I need to trigger an email.

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")) {

I have tried this, But not able to pick up the value and validate it.

This might seem very easy, but as I am very new to Groovy, I am kind of stuck on it. Thanks In Advance.

1 Like

Hi @poddingue , We discussed this earlier. Please let mw know if u have some suggesion.

What about using an XML parser?
Ideas taken from The Apache Groovy programming language - Processing XML

def response = '<table>  <TR>   <TD>Failed Value 1</TD>  <TD>2869</TD>  </TR> <TR><TD>Valid value</TD><TD>11111111111111111</TD></TR>  <TR>   <TD>Failed Value 2</TD>  <TD>9948</TD>  </TR>  <TR>   <TD>Failed Value 3</TD>  <TD>3456</TD>  </TR></table>'
def parsedResponse = new XmlSlurper().parseText(response)
def failedValues= parsedResponse.'**'.findAll { TR -> TR.TD.text().contains('Failed')}
for (groovy.util.slurpersupport.NodeChild failedValue : failedValues) {

I’m sure we can do more efficient and shorter, but that’s a start. :person_shrugging:


Scripts not permitted to use method groovy.util.XmlSlurper parseText java.lang.String. Administrators can decide whether to approve or reject this signature.

This is the error I am getting .

If you’re the administrator:

Is there any reason you need to do this inside of jenkinsfile instead of inside of an external script (which would be easier to test / reuse)?

I would recommend making it a seperate script and calling it via sh()/bat()/whatever

then you can write it in whatever language you like, and won’t take up resources on your jenkins controller.

1 Like

Actually I am not the admin, and its not allowing me to process this approach.

Just to make a job in Pipeline and then trigger email to the team if this scenario happens. I thought of implementing it inside. If any other approach is feasible, please sahre.

The idea of what is suggest by @halkeye is to “off load” the xml parsing into a (bash) script. The pipeline then calls the script and checks its result. The bash script is part of the project being built (as the jenkinsfile).
Using this technique makes the pipeline more readable. The bash script can be tested locally. You can even think about building an automated sub-module testing suite (useful if the pipeline is mission critical and to avoid regressions).

If your agent is a Linux machine you can use a very good set of XML-related programs xsltc and xmllint.

Hey , I am able to pass it through, but currently need to add a variable from this script to pass on to email body. Any idea how I can achieve that?