Jitesh Sojitra Jitesh Sojitra - 4 days ago 28
Groovy Question

Pass parameters in jenkins pipeline to internal groovy script

How can we pass parameters in Groovy script in Jenkins pipeline?

I have written xyz.groovy, it loads and executes fine but i would like to pass parameters in it instead of duplication of jobs. I tried passing load '../xyz.groovy' param1 param2 but no luck.

Pipeline script:



node {
load '../xyz.groovy'
}()


xyz.groovy



import hudson.model.*
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import java.net.URL

echo "\nParameters.."
echo param1
echo param2


Can anyone please help?

Answer

Can't you do something similar to this instead: How do you load a groovy file and execute it

You create methods in your groovy that you call with the parameters?

node {
    def script = load '../xyz.groovy'
    script.method(param1, param2)
}
Comments