Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline Parameter to hold an array

I've two pipelines say - CallingPipeline and CalledPipeline where CallingPipeline calls CalledPipeline(downstream pipeline)

In CallingPipeline, I create an array and I want to pass it to CalledPipeline. For that, I need to create a Parameter in CalledPipeline but I could not find parameter which holds an array. Could you please suggest which Parameter will hold an array?

like image 556
Alpha Avatar asked Jan 21 '26 05:01

Alpha


2 Answers

What if you just join() and made it a delimited string and then split()/reformed it back to a list/array in the CalledPipeline?

All jenkins parameters boil down to String or Boolean afaik

like image 131
metalisticpain Avatar answered Jan 23 '26 19:01

metalisticpain


Use a string parameter. Serialize your data in CallingPipeline and deserialize it in CalledPipeline. This is a straightforward task using the Groovy classes JsonOutput and JsonSlurper. Compared to simple join / split this approach can be used even for more complex data (e. g. nested objects).

CallingPipeline

import groovy.json.JsonOutput

node {
    stage('test') {
        def myArray = [ 42, 'bar', 'baz' ]

        build job: 'CalledPipeline', parameters: [
            string(name: 'myParam', value: JsonOutput.toJson( myArray ) )
        ]
    }
}

CalledPipeline

import groovy.json.JsonSlurper

node {
    stage('test') {
        echo "myParam: $myParam"

        def myParamObject = new JsonSlurper().parseText( myParam )
        for( def elem in myParamObject ) {
            echo "$elem"
        }
    }
}

Output:

myParam: [42,"bar","baz"]
42
bar
baz
like image 40
zett42 Avatar answered Jan 23 '26 21:01

zett42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!