Search code examples
jenkinsjenkins-pipelinejenkins-pluginsjenkins-groovy

Adding multiple reference parameter for a single active choice parameter in Jenkins


I'm trying to add multiple reactive parameter for a single active parameter. But when I implement it in Jenkins Pipeline, 2nd reactive parameter is going fallback script. But when I tried to do the same through Jenkins Console, it is working. So, why it's not working through the pipeline? Any help would be highly appreciated.

Pipeline Script

pipeline {
    

 parameters{
    
                activeChoice choiceType: 'PT_SINGLE_SELECT', 
                filterLength: 1, 
                filterable: false, 
                name: 'Env',
                randomName: 'choice-parameter-25251019944217', 
                script: 
                    groovyScript(
                        fallbackScript: [
                            classpath: [], 
                            oldScript: '', 
                            sandbox: true, 
                            script: 'return[\'Could not get Env\']'],
                            script: [classpath: [], 
                            oldScript: '', 
                            sandbox: true, 
                            //script: 'return'+func1()
                            script: 'return[\'demo\',\'tenant\']'
                        ]
                    )
               
                reactiveChoice choiceType: 'PT_SINGLE_SELECT',
                filterLength: 1, 
                filterable: false, 
                name: 'machines', 
                randomName: 'choice-parameter-37944444770435', 
                referencedParameters: 'Env', 
                script: 
                    groovyScript(
                        fallbackScript: [
                            classpath: [], 
                            oldScript: '', 
                            sandbox: true, 
                            script: 'return[\'error\']'], 
                            script: [classpath: [], 
                            oldScript: '', 
                            sandbox: true, 
                            script: '''if(Env.equals('tenant')){
return[\'tenant1\',\'tenant2\']
}
else if(Env.equals(\'demo\')){
return[\'demo1\',\'demo2\']
}'''
                        ]
                    )
                
                reactiveChoice choiceType: 'PT_SINGLE_SELECT',
                filterLength: 1, 
                filterable: false, 
                name: 'servers', 
                randomName: 'choice-parameter-37944444770435', 
                referencedParameters: 'Env', 
                script: 
                    groovyScript(
                        fallbackScript: [
                            classpath: [], 
                            oldScript: '', 
                            sandbox: true, 
                            script: 'return[\'error\']'], 
                            script: [classpath: [], 
                            oldScript: '', 
                            sandbox: true, 
                            script: '''if(Env.equals('tenant')){
return[\'dotnet\',\'php\']
}
else if(Env.equals(\'demo\')){
return[\'nodejs\',\'php\']
}'''
                        ]
                    )
    }

  agent any
  stages {
      stage ("Example") {
        steps {
         script{
          echo 'Hello'
          echo "${params.Env}"

        } }
      }
  }

Issue in Jenkins Console

enter image description here


Solution

  • I moved my Parameters into properties section. And now it is working.

    properties([
        parameters([
            [$class: 'ChoiceParameter', 
                choiceType: 'PT_SINGLE_SELECT', 
                description: 'Select the Env Name from the Dropdown List', 
                filterLength: 1, 
                filterable: true, 
                name: 'Env', 
                randomName: 'choice-parameter-5631314439613978', 
                script: [
                    $class: 'GroovyScript', 
                    fallbackScript: [
                        classpath: [], 
                        sandbox: true, 
                        script: 
                            'return[\'Could not get Env\']'
                    ], 
                    script: [
                        classpath: [], 
                        sandbox: true, 
                        script: 
                            'return[\'demo\',\'tenant\']'
                            
                    ]
                ]
            ], 
            [$class: 'CascadeChoiceParameter', 
                choiceType: 'PT_SINGLE_SELECT', 
                description: 'Select the Server from the Dropdown List', 
                filterLength: 1, 
                filterable: true, 
                name: 'Server', 
                randomName: 'choice-parameter-5631314456178619', 
                referencedParameters: 'Env', 
                script: [
                    $class: 'GroovyScript', 
                    fallbackScript: [
                        classpath: [], 
                        sandbox: true, 
                        script: 
                            'return[\'Could not get Environment from Env Param\']'
                    ], 
                    script: [
                        classpath: [], 
                        sandbox: true, 
                        script: '''if(Env.equals('tenant')){
    return[\'tenant1\',\'tenant2\']
    }
    else if(Env.equals(\'demo\')){
    return[\'demo1\',\'demo2\']
    }'''
                    ]
                ]
            ],
        [$class: 'CascadeChoiceParameter', 
                    choiceType: 'PT_SINGLE_SELECT', 
                    description: 'Select the Server from the Dropdown List', 
                    filterLength: 1, 
                    filterable: true, 
                    name: 'Server2', 
                    randomName: 'choice-parameter-5631314456178619123', 
                    referencedParameters: 'Env', 
                    script: [
                        $class: 'GroovyScript', 
                        fallbackScript: [
                            classpath: [], 
                            sandbox: true, 
                            script: 
                                'return[\'Could not get Environment from Env Param\']'
                        ], 
                        script: [
                            classpath: [], 
                            sandbox: true, 
                            script: '''if(Env.equals('tenant')){
    return[\'dotnet\',\'php\']
    }
    else if(Env.equals(\'demo\')){
    return[\'nodejs\',\'php\']
    }'''
                        ]
                    ]
                ]
        ])
    ])
    
    pipeline {
      agent any
      stages {
          stage ("Example") {
            steps {
             script{
              echo 'Hello'
              echo "${params.Env}"
              echo "Crossed param validation"
            } }
          }
      }
    }