0

I am writing a GROOVY script for a Jenkinsfile to do following:

Step-1: read the input file info_file.txt. Contents of info file are as under:

sh> cat info_file.txt CHIP_DETAILS:1234-A0;1456-B1;2456-B0;3436-D0;4467-C0 

Step-2: Store the CHIP_DETAILS in an array after removing the suffix -A0,-B1 etc..

for example:

Integer[] testArray = ["1234", "1456", "2456" , "3436" , "4467"] 

Step-3: print the elements of the array sequentially. For example:

testArray.each { println "chip num is ${it}" } 

I have written a small code to test if the CHIP_DETAILS 'key' is present in the input file and it is working. However i'm finding it difficult to write the regex for removing the suffix (-A0,-B1 etc..) and storing the corresponding value in an array. the code is as under:

stage('Read_Params') { steps { script { println ("Let's validate and see whether the "Key:Value" is present in the info_file.txt\n"); if ( new File("$JobPath/Inputparams.txt").text?.contains("CHIPS_DETAILS")) { println ("INFO: "Key:Value" is present in the info_file.txt \n"); >>>>> proceed with Step-1, Step-2 and Step-3... <<<<< } else { println ("WARN: "Key:Value" is NOT present in the info_file.txt \n"); exit 0; } } } } 

Thanks in advance for help!

1 Answer 1

1

You could try something like the following:

def chipDetails = 'CHIP_DETAILS:1234-A0;1456-B1;2456-B0;3436-D0;4467-C0' def testArray = ( chipDetails =~ /(?<=^|[:;])\d*(?=[-])/)​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ assert​ '1234' == testArray[0] assert '1456' == testArray[1] assert '2456' == testArray[2] assert '3436' == testArray[3] assert '4467' == testArray[4]​ 

The regular expression ensures the number you're trying to capture is either at the start of a line or prefixed with : or ; and suffixed with -

There can be any amount of digits between the delimiters.

Iterate over the results like:

testArray.each{ println it }​ 
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @Mike, this regex sounds good. Is there a way wherein I can write values directly to an array instead of writing one by one using assert . The reason is that the number of entries in the CHIP_DETAILS will be dynamic in my case since its an input param provided by user at the time of triggering the pipeline. I mean some times we may have only one entry say 1234-A0 or we may have more than 10 entries as well depending upon users input.
@Yash the asserts are just there to show you the results are as expected, they are not required for the solution, all is needed is the regex and assignment to testArray, I've updated the question to show how to iterate over the array
Hey @Mike, I tried running your code standalone but it gave following error in first line itself: Calling public static java.lang.Object org.codehaus.groovy.runtime.DefaultGroovyMethods.each(java.lang.Object,groovy.lang.Closure) on a CPS-transformed closure is not yet supported (JENKINS-26481); encapsulate in a @NonCPS method, or use Java-style loops. Am I missing something?
@Yash take a look at the answer here stackoverflow.com/questions/40195720/…
Thanks @Mike. On disabling the Use Groovy Sandbox option from pipeline configuration window the code runs successfully. However, I think we need to fine tune the regex as every-time only the second element is printed. For example: in our case 'CHIP_DETAILS:1234-A0;1456-B1;2456-B0;3436-D0;4467-C0' only 1234 is printed everytime.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.