thndrkiss
thndrkiss

Reputation: 4595

How to pass a variable declared as `def` inside Jenkins-groovy script

I was trying to pass the variable sampleArray to groovyScript function of activeChoiceReactiveParam unfortunately the xml generated doesn't pick the values. I tried on this playground http://job-dsl.herokuapp.com as well as with real Jenkins and it wasn't working out. What I meant is the sampleArray's value is not getting copied. Please tell me how this can be achieved

job("try-to-pass-array") {
    def sampleArray = ["one","two","three","four"]
    description("this is to test a element type")
    keepDependencies(false)
    parameters {
        activeChoiceReactiveParam('NUMBERS') {
            description('Choose numbers for which build has to be generated')
            choiceType('MULTI_SELECT')
            groovyScript {
                script('return $sampleArray')
                fallbackScript('"fallback choice"')
            }
        }
    }
    disabled(false)
    concurrentBuild(false)
    steps {
        shell('''
              echo $NUMBERS
              ''')
    }
}

Upvotes: 2

Views: 1422

Answers (1)

Michael Kemmerzell
Michael Kemmerzell

Reputation: 5256

You are not using the correct string interpolation. The jenkins (declarative pipeline) documentation has a very good example for this.

def username = 'Jenkins'
echo 'Hello Mr. ${username}'
echo "I said, Hello Mr. ${username}"

Would result in:

Hello Mr. ${username}
I said, Hello Mr. Jenkins

so if you want to pass the values of variables always use " and NOT '.

tl;dr script("return ${sampleArray}")

Upvotes: 1

Related Questions