Home > front end >  How to parse output jenkins console, return value and save in variable
How to parse output jenkins console, return value and save in variable

Time:09-07

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

CodePudding user response:

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.

CodePudding user response:

@Wiktor Stribiżew I made your suggested corrections unfortunately I still get an error

an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@66cfd87c
in field com.cloudbees.groovy.cps.impl.CpsClosureDef.capture
in object com.cloudbees.groovy.cps.impl.CpsClosureDef@1c9b2538
in field com.cloudbees.groovy.cps.impl.CpsClosure.def
in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@152eb829
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@487541fa
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@487541fa
Caused: java.io.NotSerializableException: java.util.regex.Matcher

Adding @NonCPS like this doesn't help:

@NonCPS 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}) /)

How do I add this code to any stage in the pipeline to check the output console there?

  • Related