John Elion John Elion -4 years ago 395
Groovy Question

Jenkins Pipeline Groovy: Read default parameter value from another job?

Within the Groovy code for a pipeline job which manages multiple jobs, is it possible to read the default value of a parameter defined in the configuration of another job?

That is, within pipeline job A, I want to read the default value of param P in job B, which hasn't been scheduled yet. (This will affect how the request to build B is constructed in job A.)

def val = "some-random-prefix" +
ReadJobParamDefault(job: 'B', parameter: 'paramName') +
"-some-random-suffix"
def b = build(job: 'B',
description: 'Some text',
parameters: [[$class: 'StringParameterValue',
name: 'paramName',
value: val]])


Is there something real for ReadJobParamDefault?

Answer Source

I guess that it could be possible to store some Groovy class into a shared library (https://jenkins.io/doc/book/pipeline/shared-libraries/) doing something like

public static String ReadJobParamDefault(jobName, String parameter) {
    def job = Hudson.instance.getJob(jobName)
    String ret = null;
    if (job != null && parameter != null  && parameter.trim().length() > 0) {
        job.getProperties().values().each {
            if(it instanceof hudson.model.ParametersDefinitionProperty) {
                if (it.getParameterDefinition(parameter) != null) {
                    ret = it.getParameterDefinition(parameter).
                        getDefaultParameterValue().getValue();
                }
            }
        }
    }
    return ret;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download