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!

Yash
  • 2,944
  • 7
  • 25
  • 43

1 Answers1

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
}​
Mike W
  • 3,853
  • 2
  • 11
  • 18
  • 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 May 24 '17 at 17:56
  • @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 – Mike W May 24 '17 at 21:16
  • 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 May 25 '17 at 05:09
  • @Yash take a look at the answer here https://stackoverflow.com/questions/40195720/how-to-print-each-element-of-multi-line-string-parameter – Mike W May 25 '17 at 05:56
  • 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. – Yash May 25 '17 at 09:25
  • You may have an issue elsewhere, try the code out here in isolation https://groovyconsole.appspot.com/script/5129706068246528 – Mike W May 25 '17 at 09:35
  • Thanks, it works in the groovy console. Also in my set the chipDetails definition looks like this `def chipDetails = '1234-A0;1456-B1;2456-B0;3436-D0;4467-C0'` and hence the first element is not displayed. https://groovyconsole.appspot.com/edit/5129706068246528 – Yash May 25 '17 at 11:11
  • @Yash check updated answer to handle scenario where required number may be start of line – Mike W May 25 '17 at 11:41