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

Issue with passing build parameters to Library vars closure

    • Icon: Bug Bug
    • Resolution: Not A Defect
    • Icon: Minor Minor
    • pipeline

      Basically I can't pass build properties to Library var call without extra nonsense.

      tc_test{
       repo = 'test1'
       folder = 'test2'
       submodules = true
       refs = params.GitCheckout
      }
      

      results in error

      java.lang.NullPointerException: Cannot get property 'GitCheckout' on null object

      This, however, works:

      def a1 = params.GitCheckout
      tc_test{
        repo = 'tc3'
        folder = 'tc3'
        submodules = true
        refs = a1
      }
      

      I'm not really good with groovy, so sorry if it's something obvious.

          [JENKINS-44928] Issue with passing build parameters to Library vars closure

          Jesse Glick added a comment -

          From inside the closure, there is a different scope.

          Given the subtle aspects of the “builder” syntax I do not really recommend it for library authors. Simpler and more reliable to define a function taking a Map<String, Object> of inputs, and pass it arguments like

          tc_test [
            repo: 'test1',
            folder: 'test2',
            submodules: true,
            refs = params.GitCheckout
          ]

          which should work since the map is constructed with its actual values by the script, then passed as data to the library.

          Jesse Glick added a comment - From inside the closure, there is a different scope. Given the subtle aspects of the “builder” syntax I do not really recommend it for library authors. Simpler and more reliable to define a function taking a Map<String, Object> of inputs, and pass it arguments like tc_test [ repo: 'test1' , folder: 'test2' , submodules: true , refs = params.GitCheckout ] which should work since the map is constructed with its actual values by the script, then passed as data to the library.

          Yep. that worked perfectly.

          Alexey Kiryanov added a comment - Yep. that worked perfectly.

          Kevin Raymond added a comment - - edited

          Hi there,

          Sorry to bump into this close ticket but I should miss something, I can't use properly the solution proposed here. I always get the following error:
          .NoSuchMethodError: No such DSL method 'getAt' found among steps
          The signature of my "cloneProject.groovy" method is:

           

          call(Map parameters) {
           def project = get.parameters('project', 'false')
          

           

          Edit: sorry for the burden, the get.parameters if wrong in this case I dunno where I took it from... parameters.find() resolve my issue.

           

           

          Kevin Raymond added a comment - - edited Hi there, Sorry to bump into this close ticket but I should miss something, I can't use properly the solution proposed here. I always get the following error: .NoSuchMethodError: No such DSL method 'getAt' found among steps The signature of my "cloneProject.groovy" method is:   call(Map parameters) {  def project = get.parameters( 'project' , ' false ' )   Edit: sorry for the burden, the get.parameters if wrong in this case I dunno where I took it from... parameters.find() resolve my issue.    

            Unassigned Unassigned
            trueconf_kiryanov Alexey Kiryanov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: