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

How to use jenkins pipeline function in parameter plugin groovy script

      I have a pipeline, such as:

      env_file='/tmp/.test_envs'
      
      def test_fun(){
          return env_file
      }
      
      pipeline {
          agent any
          environment {
              //test env_file
              test_env="${env_file}"
              main_script="/scripts/release/release_dynamic_v2.sh"
          }
          parameters {
              extendedChoice bindings: '',description:'test_param', groovyClasspath: '', groovyScript: '''
      def test_env="${env_file}"
      // def test_env = test_fun()
      // Here is error
      println test_env    
      
      def ns_file = new File(file_path)
      def value_list = []
      def line_list = ns_file.readLines()
      line_list.each{
          value_list.push(it.tokenize(" ")[0])
      }
      return value_list''', multiSelectDelimiter: ',', name: 'project_name', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', visibleItemCount: 10,
               descriptionBindings: '', descriptionGroovyClasspath: '', descriptionGroovyScript: '' 
          }
      
         stages {
             stage("check_params"){
                 
                 steps {
                     buildName "#${BUILD_NUMBER}"
                     buildDescription "Checking parameters is incorrect."
                     error 'The project_env_name mustn\'t be none or the project_name is empty.'
                 }
             }
            stage('show_info'){
                 
                 steps {
                     sh '''
                          env
                     '''
                 }
             }
         }
          post { 
              success { 
                  sh '''
                     echo post_success
                     '''
              }
              failure {
                  sh '''
                     echo post_failure
                     '''
              }
          }
      }
         
      

      I want to use the function test_fun or the variable env_file in parameters block. 
      But it dons't work.
      What should I do? Thanks.

          [JENKINS-68609] How to use jenkins pipeline function in parameter plugin groovy script

          Daniel Ellis added a comment -

          We have a large function which is messy to insert inline.  We have tried moving it to a function at the top of the Jenkinsfile, but this doesn't work.   We are currently making use of the "groovyScriptFile" property, however this has a problem because the file needs to be available on the Jenkins master, and the file is not checked out by default, so we have to manually checkout the file on the master for this to work.

          It would be great if we could just move this code to a function at the top of the Jenkinsfile.

          Daniel Ellis added a comment - We have a large function which is messy to insert inline.  We have tried moving it to a function at the top of the Jenkinsfile, but this doesn't work.   We are currently making use of the "groovyScriptFile" property, however this has a problem because the file needs to be available on the Jenkins master, and the file is not checked out by default, so we have to manually checkout the file on the master for this to work. It would be great if we could just move this code to a function at the top of the Jenkinsfile.

          Martin Wang added a comment - - edited

          The problem is resolved. I ignored the difference between single quotation marks and double quotation marks。

          env_file='/tmp/.test_envs'
          
          def test_fun(){
              return env_file
          }
          
          pipeline {
              agent any
              environment {
                  //test env_file
                  test_env="${env_file}"
                  main_script="/scripts/release/release_dynamic_v2.sh"
              }
              parameters {
                  extendedChoice bindings: '',description:'test_param', groovyClasspath: '', groovyScript: """
          def test_env="${env_file}"
          // def test_env = test_fun()
          // Here is error
          println test_env    
          
          def ns_file = new File(file_path)
          def value_list = []
          def line_list = ns_file.readLines()
          line_list.each{
              value_list.push(it.tokenize(" ")[0])
          }
          return value_list""", multiSelectDelimiter: ',', name: 'project_name', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', visibleItemCount: 10,
                   descriptionBindings: '', descriptionGroovyClasspath: '', descriptionGroovyScript: '' 
              }
          
             stages {
                 stage("check_params"){
                     
                     steps {
                         buildName "#${BUILD_NUMBER}"
                         buildDescription "Checking parameters is incorrect."
                         error 'The project_env_name mustn\'t be none or the project_name is empty.'
                     }
                 }
                stage('show_info'){
                     
                     steps {
                         sh '''
                              env
                         '''
                     }
                 }
             }
              post { 
                  success { 
                      sh '''
                         echo post_success
                         '''
                  }
                  failure {
                      sh '''
                         echo post_failure
                         '''
                  }
              }
          }
             
          

          Martin Wang added a comment - - edited The problem is resolved. I ignored the difference between single quotation marks and double quotation marks。 env_file= '/tmp/.test_envs' def test_fun(){     return env_file } pipeline {     agent any     environment {         //test env_file         test_env= "${env_file}"         main_script= "/scripts/release/release_dynamic_v2.sh"     }     parameters {         extendedChoice bindings: '',description:' test_param ', groovyClasspath: ' ', groovyScript: """ def test_env= "${env_file}" // def test_env = test_fun() // Here is error println test_env     def ns_file = new File(file_path) def value_list = [] def line_list = ns_file.readLines() line_list.each{     value_list.push(it.tokenize( " " )[0]) } return value_list""", multiSelectDelimiter: ',' , name: 'project_name' , quoteValue: false , saveJSONParameterToFile: false , type: 'PT_SINGLE_SELECT' , visibleItemCount: 10,          descriptionBindings: '', descriptionGroovyClasspath: ' ', descriptionGroovyScript: ' '      }    stages {        stage( "check_params" ){                        steps {                buildName "#${BUILD_NUMBER}"                buildDescription "Checking parameters is incorrect."                error 'The project_env_name mustn\' t be none or the project_name is empty.'            }        }       stage( 'show_info' ){                        steps {                sh '''                     env                '''            }        }    }     post {          success {              sh '''                echo post_success                '''         }         failure {             sh '''                echo post_failure                '''         }     } }    

          Celia added a comment - - edited

          What are some best practices for error handling in Jenkins pipelines? geometry dash lite

          Celia added a comment - - edited What are some best practices for error handling in Jenkins pipelines? geometry dash lite

            martin_wang Martin Wang
            martin_wang Martin Wang
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: