In jenkins how to restrict users to select first default element with other options in extended choice parameter

Viewed 18

I have a jenkins job with multi select extended choice parameter. There are list of elements in a parameter. So, my requirement is I want to allow users to select multiple parameters excluding first element in a parameter. Means user should not able to select first element with other elements in a parameters. I am using jenkinsfile to create parameter.

enter image description here

Like shown above, users should not able to select 'None' with any other element in a parameter. Does anyone know how to do this?

1 Answers

I don't think you can do this with just Extended choice parameter. As a workaround, you can add two parameters. The first parameter to check whether it's None or not, if not you can bring up the second parameter. You can use the Active Choice Parameter for this.

properties([
    parameters([
        [$class: 'CascadeChoiceParameter', 
            choiceType: 'PT_MULTI_SELECT',
            description: 'Select a choice',
            filterLength: 1,
            name: 'choice1',
            referencedParameters: 'CHOICE',
            script: [$class: 'GroovyScript',
                fallbackScript: [
                    classpath: [], 
                    sandbox: true, 
                    script: 'return ["ERROR"]'
                ],
                script: [
                    classpath: [], 
                    sandbox: true, 
                    script: """
                        if (CHOICE == 'Yes') { 
                            return['Item1','Item2','Item3']
                        }
                        else {
                            return[]
                        }
                    """.stripIndent()
                ]
            ]
        ]
    ])
])

pipeline {
    agent any
    parameters {
        choice(name: 'CHOICE', description: "Do you have any choices?", choices: ["Yes", "None"])
    }
    stages {
        stage("Run Tests") {
            steps {
                sh "echo SUCCESS on ${params.CHOICE}"
            }
        }
    }
}
Related