Groovy script to Grep a string from jenkins console output

1.1k Views Asked by At

I have to read the jenkins console output and grep the line which has the string "%)". I am able to grep the line wit the following code:

def result = fileContents.findAll { it.contains('%)') }
println result*.toString()

Output:

× 35 of 45 failed (78%) 06:13 247 3 38 66 140

From the above line, I need to fetch and print only failed %. ie. 78 and make the Jenkins job failed if the failed % is above 50%

Can someone please help me with this using Groovy? Is there any other way to do it without using Groovy also fine for me?

1

There are 1 best solutions below

0
On

According to me, instead of having the groovy script do this for you (complicated way), follow the below steps:

  1. download the console output of the current build using wget <jenkins_url_of_console_output>
  2. Use bash/python-regex to grep or capture the desired output you want check in your case
  3. Compare the string you generate from the step-2 with the required output format preferably using an if block and fail or pass the stage accordingly.

In you case, you can take the percentage success/failure and pass/fail the stage according to your requirement. Hope this helps.