Alpha
Alpha

Reputation: 14026

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?

Upvotes: 3

Views: 12219

Answers (2)

zett42
zett42

Reputation: 27756

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

Upvotes: 1

Sam
Sam

Reputation: 2882

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

Upvotes: 1

Related Questions