0

I'm having trouble parsing the jenkins pipeline console output. Every time I pass a job, a line appears in the console:

12:29:08 [10:29:07] NIDD version is: SBTS23R1_NIDD_2217_100_01

enter image description here

I would like to extract a variable value from it: SBTS23R1_NIDD_2217_100_01 and save it in a variable so that I can use it further.

I tried to do something like described here: GROOVY: Finding a string from console output if a regexp string found

Unfortunately, I am getting the error:

an exception which occurred:
    in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@20cd216a
    in field com.cloudbees.groovy.cps.impl.CpsClosureDef.capture
    in object com.cloudbees.groovy.cps.impl.CpsClosureDef@3e3cd6ed
    in field com.cloudbees.groovy.cps.impl.CpsClosure.def
    in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@6be3d3c7
    in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@14fde9cd
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@14fde9cd
Caused: java.io.NotSerializableException: java.util.regex.Matcher

My code :

def matcher = ("12:29:08 [10:29:07] NIDD version is: SBTS23R1_NIDD_2217_100_01" =~ /NIDD version is:\SBTS\d{2}\w\d_NIDD_\d{4}_\d{3}_\d{2}/) 

if (matcher.hasGroup()) 
{ 
  def msg = matcher[0][1] println("Build failed because of ${msg}") 
}
haker146
  • 61
  • 5
  • What is the code that results in this error? – Wiktor Stribiżew Sep 06 '22 at 12:51
  • def matcher = ("12:29:08 [10:29:07] NIDD version is: SBTS23R1_NIDD_2217_100_01" =~ /NIDD version is:\SBTS\d{2}\w\d_NIDD_\d{4}_\d{3}_\d{2}/) if (matcher.hasGroup()) { def msg = matcher[0][1] println("Build failed because of ${msg}") } – haker146 Sep 06 '22 at 12:55
  • Looks like you missed `\s*` and a group that you refer to later from the code. Try https://ideone.com/5zO4d9 – Wiktor Stribiżew Sep 06 '22 at 13:00

1 Answers1

1

You need to use a capturing group and consume any amount of spaces between a colon and SBT (you mistakenly escaped S making S in SBT part of a non-whitespace matching shorthand character class):

def matcher = ("12:29:08 [10:29:07] NIDD version is: SBTS23R1_NIDD_2217_100_01" =~ /NIDD version is:\s*(SBTS\d{2}\w\d_NIDD_\d{4}_\d{3}_\d{2})/)
if (matcher) {
    def msg = matcher[0][1]
    println("Build failed because of ${msg}")
}

See the Groovy demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks for your help, I'll check if it works tomorrow and let you know – haker146 Sep 06 '22 at 20:20
  • Your fixes work, but something is wrong because the code looks for the NIDD value in what was typed in the code ("12:29:08 [10:29:07] NIDD version is: SBTS23R1_NIDD_2217_100_01") not the entire job console – haker146 Sep 07 '22 at 12:25