Active Choices Reactive Reference Parameter in jenkins pipeline
Asked Answered
A

7

25

I'm using the Active Choices Reactive Reference Parameter plugin in a dsl job here the code

 parameters {
                  activeChoiceParam('choice1') {
                      description('select your choice')
                      choiceType('RADIO')
                      groovyScript {
                          script("return['aaa','bbb']")
                          fallbackScript('return ["error"]')
                      }
                  }
                  activeChoiceReactiveParam('choice2') {
                      description('select your choice')
                      choiceType('RADIO')
                      groovyScript {
                          script("if(choice1.equals("aaa")){return ['a', 'b']} else {return ['aaaaaa','fffffff']}")
                          fallbackScript('return ["error"]')
                      }
                      referencedParameter('choice1')
                  }

and it work's fine what i want now is how to use the activeChoiceReactiveParam in a jenkinsFile for pipeline job i did that :

properties(
    [
            [
                    $class              : 'ParametersDefinitionProperty',
                    parameterDefinitions: [
                            [
                                    $class     : 'ChoiceParameterDefinition',
                                    choices    : 'aaa\nbbb',
                                    description: 'select your choice : ',
                                    name       : 'choice1'
                            ]

but how can i add the choice2 parameter !!!

Amaryllidaceous answered 14/4, 2017 at 10:59 Comment(0)
A
9

Instead of using the Active Choices Reactive Reference Parameter i did below and it's working fine !!!

node('slave') {
    def choice1
    def choice2

    stage ('Select'){
        choice1 = input( id: 'userInput', message: 'Select your choice', parameters: [ [\$class: 'ChoiceParameterDefinition', choices: 'aa\nbb', description: '', name: ''] ])
        if(choice1.equals("aa")){
            choice2 = input( id: 'userInput', message: 'Select your choice', parameters: [ [\$class: 'ChoiceParameterDefinition', choices: 'yy\nww', description: '', name: ''] ])
        }else{
            choice2 = input( id: 'userInput', message: 'Select your choice', parameters: [ [\$class: 'ChoiceParameterDefinition', choices: 'gg\nkk', description: '', name: ''] ])
        }
    }
}
Amaryllidaceous answered 22/8, 2017 at 15:45 Comment(1)
Nice answer because it uses native Jenkins commands instead of relying on more plugins.Counselor
J
44

I was in need of the similar solution. I did not find any related answers/examples specific to Active Choices plugin anywhere in my google search. With some R & D, finally I was able to get this done for a pipeline project. Here is the Jenkinsfile code

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: false, 
                    script: 
                        'return[\'Could not get Env\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return["Dev","QA","Stage","Prod"]'
                ]
            ]
        ], 
        [$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: false, 
                    script: 
                        'return[\'Could not get Environment from Env Param\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        ''' if (Env.equals("Dev")){
                                return["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
                            }
                            else if(Env.equals("QA")){
                                return["qaaaa001","qabbb002","qaccc003"]
                            }
                            else if(Env.equals("Stage")){
                                return["staaa001","stbbb002","stccc003"]
                            }
                            else if(Env.equals("Prod")){
                                return["praaa001","prbbb002","prccc003"]
                            }
                        '''
                ]
            ]
        ]
    ])
])

pipeline {
  environment {
         vari = ""
  }
  agent any
  stages {
      stage ("Example") {
        steps {
         script{
          echo 'Hello'
          echo "${params.Env}"
          echo "${params.Server}"
          if (params.Server.equals("Could not get Environment from Env Param")) {
              echo "Must be the first build after Pipeline deployment.  Aborting the build"
              currentBuild.result = 'ABORTED'
              return
          }
          echo "Crossed param validation"
        } }
      }
  }
}

Even you can use "Active Choices Reactive Reference Parameter" with below definitions and rest of the code, follow as in above example.

[$class: 'DynamicReferenceParameter', 
            choiceType: 'ET_FORMATTED_HTML', 
            description: 'These are the details in HTML format', 
            name: 'DetailsInHTML', 
            omitValueField: false, 
            randomName: 'choice-parameter-5633384460832175', 
            referencedParameters: 'Env, Server',
            script: [
                  $class: 'ScriptlerScript',
                  parameters: [[$class: 'org.biouno.unochoice.model.ScriptlerScriptParameter', name: '', value: '$value']],
                  scriptlerScriptId: 'script.groovy'
        ]
    ]

Note: Ensure you have "," delimiter between multiple parameter definition blocks.

Hope this helps someone.

Justly answered 9/1, 2019 at 6:21 Comment(0)
A
9

Instead of using the Active Choices Reactive Reference Parameter i did below and it's working fine !!!

node('slave') {
    def choice1
    def choice2

    stage ('Select'){
        choice1 = input( id: 'userInput', message: 'Select your choice', parameters: [ [\$class: 'ChoiceParameterDefinition', choices: 'aa\nbb', description: '', name: ''] ])
        if(choice1.equals("aa")){
            choice2 = input( id: 'userInput', message: 'Select your choice', parameters: [ [\$class: 'ChoiceParameterDefinition', choices: 'yy\nww', description: '', name: ''] ])
        }else{
            choice2 = input( id: 'userInput', message: 'Select your choice', parameters: [ [\$class: 'ChoiceParameterDefinition', choices: 'gg\nkk', description: '', name: ''] ])
        }
    }
}
Amaryllidaceous answered 22/8, 2017 at 15:45 Comment(1)
Nice answer because it uses native Jenkins commands instead of relying on more plugins.Counselor
P
5

Thanks a ton @Yogeesh. It helped a lot. In my case, the requirement was to select multiple instead of one. So, used choiceType: 'PT_CHECKBOX', and it served the purpose.

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: false, 
                script: 
                    'return[\'Could not get Env\']'
            ], 
            script: [
                classpath: [], 
                sandbox: false, 
                script: 
                    'return["Dev","QA","Stage","Prod"]'
            ]
        ]
    ], 
    [$class: 'CascadeChoiceParameter', 
        choiceType: 'PT_CHECKBOX', 
        description: 'Select Servers', 
        filterLength: 1, 
        filterable: true, 
        name: 'Server', 
        randomName: 'choice-parameter-5631314456178619', 
        referencedParameters: 'Env', 
        script: [
            $class: 'GroovyScript', 
            fallbackScript: [
                classpath: [], 
                sandbox: false, 
                script: 
                    'return[\'Could not get Environment from Env Param\']'
            ], 
            script: [
                classpath: [], 
                sandbox: false, 
                script: 
                    ''' if (Env.equals("Dev")){
                            return["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
                        }
                        else if(Env.equals("QA")){
                            return["qaaaa001","qabbb002","qaccc003"]
                        }
                        else if(Env.equals("Stage")){
                            return["staaa001","stbbb002","stccc003"]
                        }
                        else if(Env.equals("Prod")){
                            return["praaa001","prbbb002","prccc003"]
                        }
                    '''
            ]
        ]
    ]
])
Placeeda answered 20/2, 2020 at 12:42 Comment(3)
How do you read selected values from checkbox? if (Server.equals("devaaa001")) {...}? There could be multiple values selected, so we cannot use "equals".Balcony
So, here in this example, "Env" is the Choice Parameter and based on the selection, "Server", which is a Cascade Choice Parameter appears. We can use ${params.Server} to get the value of checkbox selection/sPlaceeda
what is randomName nameded for?Maneating
B
5

I made the initial sample work for me. I had to change ' for " in the return statement return ["a", "b"] and " by ' in the script statement script(' ')

job("MyJob") {
    description ("This job creates ....")
    
    //def targetEnvironment=""
    

    parameters {
         activeChoiceParam('choice1') {
                      description('select your choice')
                      choiceType('RADIO')
                      groovyScript {
                          script('return["aaa","bbb"]')
                          fallbackScript('return ["error"]')
                      }
        }
        activeChoiceReactiveParam('choice2') {
                      description('select your choice')
                      choiceType('RADIO')
                      groovyScript {
                          script(' if(choice1.equals("aaa")) { return ["a", "b"] } else {return ["aaaaaa","fffffff"] } ')
                          fallbackScript('return ["error"]')
                      }
                      referencedParameter('choice1')
        }

    }
}
Bravar answered 14/8, 2020 at 7:55 Comment(1)
how would i make the reactive param a text box, but with different values depending on the choice?Maneating
M
1

Is there any way that we can put the script logic in a function and call for example:

 [$class: 'CascadeChoiceParameter', 
        choiceType: 'PT_CHECKBOX', 
        description: 'Select Servers', 
        filterLength: 1, 
        filterable: true, 
        name: 'Server', 
        randomName: 'choice-parameter-5631314456178619', 
        referencedParameters: 'Env', 
        script: [
            $class: 'GroovyScript', 
            fallbackScript: [
                classpath: [], 
                sandbox: false, 
                script: 
                    'return[\'Could not get Environment from Env Param\']'
            ], 
            script: [
                classpath: [], 
                sandbox: false, 
                script: 
                    ''' if (Env.equals("Dev")){
                            return["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
                        }
                        else if(Env.equals("QA")){
                            return["qaaaa001","qabbb002","qaccc003"]
                        }
                        else if(Env.equals("Stage")){
                            return["staaa001","stbbb002","stccc003"]
                        }
                        else if(Env.equals("Prod")){
                            return["praaa001","prbbb002","prccc003"]
                        }
                    '''
            ]
        ]
    ]

Define below code in function and call?

 if (Env.equals("Dev")){
                            
     return["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
     }
                        

else if(Env.equals("QA")){ return["qaaaa001","qabbb002","qaccc003"] } else if(Env.equals("Stage")){ return["staaa001","stbbb002","stccc003"] } else if(Env.equals("Prod")){ return["praaa001","prbbb002","prccc003"]
------------------------------------------------------------------------

}
                  
Montez answered 14/2, 2022 at 17:47 Comment(0)
H
0

Try something like that:

properties(
[
        [
                $class              : 'ParametersDefinitionProperty',
                parameterDefinitions: [
                        [
                                $class     : 'ChoiceParameterDefinition',
                                choices    : 'aaa\nbbb',
                                description: 'select your choice : ',
                                name       : 'choice1'
                        ],
                        [
                                $class     : 'ChoiceParameterDefinition',
                                choices    : 'ccc\nddd',
                                description: 'select another choice : ',
                                name       : 'choice2'
                        ]
Horripilate answered 16/6, 2017 at 7:8 Comment(1)
the choice2 depend on choice1; if the user select choice1 as "aaa" than the choices on choice2 gonna be "cc" or "dd" and if the user select choice1 as "bbb" than the choices on choice2 gonna be "ee" or "rr"Amaryllidaceous
S
0

I have Active Choices Plug-in 2.8.3. The following demonstrates both an Active Choices Parameter and an Active Choices Reactive Parameter in a pipeline DSL:

pipeline {
    agent any
    parameters {
        activeChoice(
            name: 'choice1',
            choiceType: 'PT_SINGLE_SELECT',
            script: [
                $class: 'GroovyScript', 
                script: [
                    script: '''
return ["aaa", "bbb"]
'''
                ]
            ]
        )
        reactiveChoice(
            name: 'choice2',
            choiceType: 'PT_SINGLE_SELECT',
            script: [
                $class: 'GroovyScript', 
                script: [
                    script: '''
if(choice1.equals("aaa")){return ['a', 'b']} else {return ['aaaaaa','fffffff']}
'''
                ]
            ],
            referencedParameters: 'choice1'
        )
    }
    // ...
}
Scaler answered 12/6 at 0:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.