Jenkins Job DSL: Using parameters in groovyScript in job step

anha1979 picture anha1979 · Mar 16, 2018 · Viewed 11.7k times · Source

For my build job "generated-job-1" I need several parameters, which are passed in when the build (of the generated-job-1) is triggered via URL.

Here my Job Definition with parameters inside the SeedJob DSL:

job('generated-job-1'){
label ('master')
parameters{
    stringParam('DEPLOY_URI', 'https://192.168.200.176/hyperManager', 'Provide the URL where DeploymentManager can be accessed.')
    stringParam('REG_ID', '12', 'The id of the owner (Registration) of this deployment.')
}
steps {
    groovyCommand(readFileFromWorkspace('stepscript.groovy')){
        prop('name', 'value')
        prop('DEPLOY_URI', $DEPLOY_URI)
    }
}

}

I tried to use DEPLOY_URI, $DEPLOY_URI and ${DEPLOY_URI} and it the build fails with different error messages like No such property: DEPLOY_URI for class: javaposse.jobdsl.dsl.helpers.step.GroovyContext

or ERROR: (script, line 12) No such property: $DEPLOY_URI for class: javaposse.jobdsl.dsl.helpers.step.GroovyContext

or ERROR: (script, line 12) No signature of method: javaposse.jobdsl.dsl.helpers.step.GroovyContext.$() is applicable for argument types: (script$_run_closure1$_closure3$_closure4$_closure5) values: [script$_run_closure1$_closure3$_closure4$_closure5@1a11cf0]

  1. How can I define and pass those parameters to my step-script.groovy?
  2. How could I use those parameters in other steps, such as shell or batchFile?
  3. How do I access those parameters in my step-script.groovy, to work with the given data?

I searched for a while now and tried hard to get it working... No success.

Help really appreciated, as I am new to Job DSL and to Groovy.

Thanks in advance, Anne

Answer

daspilker picture daspilker · Mar 16, 2018

You need to put the variable name in quotes so that it gets evaluated when the generated job is executed, not when the DSL script runs.

job('generated-job-1') {
    parameters {
        stringParam('DEPLOY_URI', '...', '...')
    }
    steps {
        groovyCommand(readFileFromWorkspace('stepscript.groovy')) {
            prop('DEPLOY_URI', '$DEPLOY_URI')
        }
    }
}