Search code examples
jenkinsdynamicbuildparameters

Jenkins dynamic parameter not visible in stages


I'm using dynamic parameters for my Jenkins build. I have several dynamic parameters and they're all working fine except for one, which I've boiled down to the following example.

properties([parameters([

        choice(name: 'VERSION',
                choices: 'current\nprevious',
                description: "============ Select current or previous build ============"),

        [$class              : "DynamicReferenceParameter",
         choiceType          : "ET_FORMATTED_HTML",
         name                : "PREVIOUS_VERSION",
         referencedParameters: "VERSION",
         script              : [$class: "GroovyScript", script: [sandbox: true, script: '''
             if ( VERSION.equals("previous") ) {
                 return "<input name='nexus_version_number' value='' class='setting-input' type='text'>"
             } else {
                 return "not applicable"
             }
            ''']]]
])])
pipeline {
    environment {
        // Variables in the properties section above are not visible in the stages section below.
        // This environment section bridges those scopes to make the values visible.
        VERSION_INPUT = "${params.VERSION}"
        PREVIOUS_VERSION_INPUT = "${params.PREVIOUS_VERSION}"
    }
    stages {
        stage('Initialize') {
            steps {
                echo "######## Initialize ########"
                script {
                    println ENV_INPUT
                    println AZ_INPUT
                    println VERSION_INPUT
                    println PREVIOUS_VERSION_INPUT
                }
            }
        }
    }
}

So when the "version" dropdown is set to "current" then the text field (via HTML) is actually a label. But when it's set to "previous", the text field appears fine.

My experience (or belief, right or wrong) is that variables in the "properties" block are not in scope in the "stages". But I can use the "environment" block to bridge that gap and copy those values. (Maybe it's a waste, maybe not.)

But in any case, for the 4 printlns in the "Initialize" stage, I'm getting output for the first 3 only. The 4th line (for PREVIOUS_VERSION_INPUT) is simply not showing up.

I've taken this approach with other UI controls and cascading and reference and all that with no issues. Except for the "text field".

What about the name of the text field (PREVIOUS_VERSION vs "nexus_version_number"). Am I doing that wrong? I'm not getting any errors. I'm just not getting the value populated into the field "PREVIOUS_VERSION_INPUT".

Thanks,

Dave.

I've tried using different controls (instead of the "single_select"), like radio buttons and checkboxes, but without any obvious difference. I seem to be unable to grab the value of that field.


Solution

  • You just need to change the name attribute of your input tag to value.

    correct input tag would be:

    return "<input name='value' value='' class='setting-input' type='text'>"
    

    Also if you want to capture Not Applicable too, then you need to define it as disabled input field:

    return "<input disabled name='value' value='Not Applicable' class='setting-input' type='text'>"
    

    Note that the value received in script will have a trailing ,, which you'll need to trim in script.