groovy Jenkins管道无功参考参数的主动选择

7jmck4yq  于 2023-01-04  发布在  Jenkins
关注(0)|答案(6)|浏览(184)

我在dsl作业中使用Active Choices无功参考参数插件,代码如下

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

它的工作很好,我现在想要的是如何使用在一个jenkinsFile管道作业的activeChoiceReactiveParam我这样做:

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

但是我怎么能添加choice 2参数呢!!!

smdncfj3

smdncfj31#

我需要类似的解决方案。我没有找到任何相关的答案/例子,具体到积极的选择插件在我的谷歌搜索任何地方。与一些研发,终于我能够做到这一点的管道项目。这里是Jenkins文件的代码

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 Reactive Reference Parameter"(激活选择无功参考参数),定义和代码的其余部分如下所示。

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

注意:确保多个参数定义块之间有","分隔符。
希望这能帮到什么人。

js5cn81o

js5cn81o2#

我没有使用Active Choices无功参考参数,而是使用了下面的参数,它工作正常!!!

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

4urapxun3#

非常感谢@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"]
                        }
                    '''
            ]
        ]
    ]
])
c0vxltue

c0vxltue4#

我使最初的示例为我工作,我必须在return语句return ["a", "b"]中将'改为",在脚本语句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')
        }

    }
}
lstz6jyr

lstz6jyr5#

你可以这样说:

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'
                        ]
u5i3ibmn

u5i3ibmn6#

有没有什么方法可以把脚本逻辑放在一个函数中,并调用例如:

[$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 funciton and call ?

如果(环境等于(“偏差”)){

return["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
 }

else if(环境等于(“质量保证”)){ return[“qaaa 001”,“qabbb 002”,“qaccc 003”] } else if(环境等于(“阶段”)){ return[“staaa 001”,“stbbb 002”,“stccc 003”] } else if(环境等于(“生产”)){ return[“praaa 001”,“prbb 002”,“prccc 003”]

}

相关问题