简体   繁体   English

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

[英]Active Choices Reactive Reference Parameter in jenkins pipeline

I'm using the Active Choices Reactive Reference Parameter plugin in a dsl job here the code我在 dsl 作业中使用 Active Choices Reactive Reference Parameter 插件代码

 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:它工作正常我现在想要的是如何在 jenkinsFile 中使用 activeChoiceReactiveParam 进行管道作业我这样做了:

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

but how can i add the choice2 parameter !!!但是我怎样才能添加 choice2 参数!!!

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.我在谷歌搜索的任何地方都没有找到任何特定于 Active Choices 插件的相关答案/示例。 With some R & D, finally I was able to get this done for a pipeline project.通过一些研发,我终于能够为管道项目完成这项工作。 Here is the Jenkinsfile code这是 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"
        } }
      }
  }
}

Even you can use "Active Choices Reactive Reference Parameter" with below definitions and rest of the code, follow as in above example.即使您可以使用具有以下定义和其余代码的“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'
        ]
    ]

Note: Ensure you have "," delimiter between multiple parameter definition blocks.注意:确保在多个参数定义块之间有“,”分隔符。

Hope this helps someone.希望这可以帮助某人。

Instead of using the Active Choices Reactive Reference Parameter i did below and it's working fine !!!我在下面做的而不是使用Active Choices Reactive Reference 参数,它工作正常!!!

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

Thanks a ton @Yogeesh.非常感谢@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.因此,使用了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"]
                        }
                    '''
            ]
        ]
    ]
])

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(' ')我不得不在返回语句中更改' for " return ["a", "b"]" by '在脚本语句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')
        }

    }
}

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

Is there any way that we can put the script logic in a function and call for example:有什么办法可以将脚本逻辑放在 function 中并调用例如:

 [$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 ?

if (Env.equals("Dev")){如果 (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"] 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”]

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jenkins 声明式管道:如何从“主动选择反应引用参数的 Groovy 脚本”调用函数? - Jenkins Declarative Pipeline: How to call functions from "Active choices reactive reference parameter's Groovy script"? 如何从 Jenkins 的活动选择反应参数中引用节点参数 - How to reference the node paramater from the active choices reactive parameter in Jenkins Jenkins Groovy 和 Active Choices 反应参数 - Jenkins Groovy and Active Choices Reactive Parameter Jenkins Active Choices Reactive Parameter中的OutputStreamWriter替代方法 - OutputStreamWriter alternative in Jenkins Active Choices Reactive Parameter Jenkins管道参数代码,用于多个活动选择 - Jenkins pipeline parameter code for multiple active choices Jenkins Active Choices反应参考参数不适用于格式化的HTML输入文本框 - Jenkins Active Choices Reactive Reference Parameter does not work with Formatted HTML Input-Text-Box Jenkins - 主动选择反应参考参数 - Jenkins - Active choice reactive reference parameter 查询 AWS CLI 以填充 Jenkins “Active Choices Reactive Parameter” (Linux) - Query AWS CLI to populate Jenkins “Active Choices Reactive Parameter” (Linux) 如何在 Jenkins 中为 Active Choices Reactive Parameter 插件设置默认值? - How to set default value for Active Choices Reactive Parameter plugin in Jenkins? K8s 在 Active Choices Reactive Reference Parameter 脚本中 - k8s in Active Choices Reactive Reference Parameter script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM