Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-63282

Jenkins dynamic declarative pipeline parameters

XMLWordPrintable

      I want a the choice option values be populated at runtime by a function.
      The following code should to generate a list of options, but for some reason its not working ... 

      def getEnv() {
           return["Dev","Prod"] 
      }
      def getServerByEnvParam(env_param) {
          if (env_param.equals("Dev")){
              return["dev01","dev02"]
          }
          else if(env_param.equals("Prod")){
              return["prd001","prd02"]
          } else {
              return["unknown"]
          }
      }
      properties([
          parameters([
              [$class: 'ChoiceParameter', 
                  choiceType: 'PT_SINGLE_SELECT', 
                  description: 'Select Environment', 
                  name: 'myEnv', 
                  script: [
                      $class: 'GroovyScript', 
                      fallbackScript: [
                          classpath: [], 
                          sandbox: false, 
                          script: 
                              'return[\'myEnv Error\']'
                      ], 
                      script: [
                          classpath: [], 
                          sandbox: false, 
                          script: 
                              'return getEnv()'
                      ]
                  ]
              ], 
              [$class: 'CascadeChoiceParameter', 
                  choiceType: 'PT_SINGLE_SELECT', 
                  description: 'Select Server', 
                  filterable: false, 
                  name: 'myServer', 
                  referencedParameters: 'myEnv', 
                  script: [
                      $class: 'GroovyScript', 
                      fallbackScript: [
                          classpath: [], 
                          sandbox: false, 
                          script: 
                              'return[\'myServer Error\']'
                      ], 
                      script: [
                          classpath: [], 
                          sandbox: false, 
                          script: 
                              'return getServerByEnvParam(myEnv)'
                      ]
                  ]
              ]
          ])
      ])
      pipeline {
          agent any    stages {
              stage('Init') {
                  steps {
                      script{
                          println getServerByEnvParam(myEnv)
                      }
                  }
              }
          }
      }
      

       

      how can I to define and use these functions inside jenkins file? 
      is a pipeline supports dynamic parameters based on function in shared libraries? 

            kinow Bruno P. Kinoshita
            stamnik stam nick
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: