主动选择jenkins管道中的反应参考参数

时间:2017-04-14 10:59:50

标签: jenkins groovy jenkins-plugins jenkins-pipeline

我在dsl作业中使用Active Choices Reactive Reference Parameter插件,代码为

shared_ptr

它的工作正常我现在想要的是如何在jenkinsFile中使用activeChoiceReactiveParam进行管道工作我做到了:

 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')
                  }

但是如何添加choice2参数!!!

5 个答案:

答案 0 :(得分:5)

感谢一吨@Yogeesh。这很有帮助。 在我的情况下,要求选择多个而不是一个。因此,使用了choiceType:“ PT_CHECKBOX”,就达到了目的。

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"]
                        }
                    '''
            ]
        ]
    ]
])

答案 1 :(得分:4)

我没有使用主动选择反应参考参数,而是在下面做了,而且工作正常!!!

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: ''] ])
        }
    }
}

答案 2 :(得分:3)

我需要类似的解决方案。 我在Google搜索的任何地方都没有找到与Active Choices插件相关的任何相关答案/示例。 通过一些研发,最终我能够完成一个管道项目。 这是Jenkinsfile代码

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"
        } }
      }
  }
}

即使您可以将“ Active Choices反应参考参数”与以下定义和其余代码一起使用,请按照上面的示例进行操作。

[$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'
        ]
    ]

注意:确保多个参数定义块之间有“,”定界符。

希望这对某人有帮助。

答案 3 :(得分:1)

我为我制作了最初的样品。 我必须在脚本语句'中将"的{​​{1}}更改为return ["a", "b"],将"的{​​{1}}更改为'

script('   ')

答案 4 :(得分:0)

尝试类似的东西:

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'
                        ]
相关问题