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

Unable to use WorkflowScript multiple times in libraries class constructor

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • workflow-cps-plugin
    • None
    • Jenkins ver. 2.89.4
      Pipeline 2.5

      I build a Shared library using groovy classes that are located in the path /src.
      That classes use the WorkflowScript as constructor's argument, and that argument will be pass to the parent class, so it is possible to load all WorkflowScript properties like env and parms . When I try to instantiate multiple classes only the first class can access the WorkflowScript properties, if I change the order of instantiation then the first class can access the  WorkflowScript properties that in the previous order couldn't access

      In Example 1 the Foo class could access to the variables env, currentBuild and params , the class Foo and Stuff couldn't.

      In Example 2 I change the order and then the Bar class is the first class to be instantiated and now the Bar class could access the variables and the Foo couldn't.

      Could you help me to understand if it is a bug or miss use of the parent class

      Pipeline Example 1

       

      timestamps {
       node(env.NODE_NAME) {        
        def lib =   library("Common@${env.LIB_VERSION}").com.mycompany.jenkins             
        def fooLib = lib.Foo.new(this)        
        def barLib = lib.Bar.new(this)        
        def stuffLib = lib.Stuff.new(this)        
       }
      }

      Child Classes

       

      #Foo

       

      #/src/com/mycompany/jenkins/Foo.groovy
      package com.mycompany.jenkins
      
      class Foo extends WorkflowScriptLoader{
      
          Foo(script) {
              super(script)
          }
      }

       

      #Bar

       

      #/src/com/mycompany/jenkins/Bar.groovy
      package com.mycompany.jenkins 
      class Bar extends WorkflowScriptLoader{
        Bar(script) { 
          super(script) 
        }
      }
      

      #Stuff

       

       

      #/src/com/mycompany/jenkins/Stuff.groovy 
      package com.mycompany.jenkins 
      class Stuff extends WorkflowScriptLoader{ 
        Stuff(script) { 
          super(script) 
        } 
      }
      

      Parent Classe
      #WorkflowScriptLoader

       

       

      class WorkflowScriptLoader implements Serializable{
      
          def workFileScript
          def env
          def currentBuild
          def params
      
          WorkflowScriptLoader(script){
              workFileScript = script
              env = script.env
              currentBuild = script.currentBuild
              params = script.params
          }
      }

       

       

       

       

          [JENKINS-53283] Unable to use WorkflowScript multiple times in libraries class constructor

          Andrew Bayer added a comment -

          I honestly can't remember seeing that lib.Foo.new syntax before. Just for giggles, does it work if you just do

            library("Common@${env.LIB_VERSION}")
            def fooLib = new com.mycompany.jenkins.Foo(this)        
            def barLib = new com.mycompany.jenkins.Bar(this)        
            def stuffLib = new com.mycompany.jenkins.Stuff(this)
          

          Or, for that matter, if you instantiate the same class multiple times?

          Andrew Bayer added a comment - I honestly can't remember seeing that lib.Foo.new syntax before. Just for giggles, does it work if you just do library( "Common@${env.LIB_VERSION}" ) def fooLib = new com.mycompany.jenkins.Foo( this ) def barLib = new com.mycompany.jenkins.Bar( this ) def stuffLib = new com.mycompany.jenkins.Stuff( this ) Or, for that matter, if you instantiate the same class multiple times?

          abayer the syntax that you give me doesn't work. Also, the syntax I am using appears in the Jenkins shared library documentation 
          https://jenkins.io/doc/book/pipeline/shared-libraries/

           

          def useSomeLib(helper) { // dynamic: cannot declare as Helper
              helper.prepare()
              return helper.count()
          }
          
          def lib = library('my-shared-library').com.mycorp.pipeline // preselect the package
          
          echo useSomeLib(lib.Helper.new(lib.Constants.SOME_TEXT)) 
          

          But if I instantiate the same clase mutiple times and call the second instante it will work. That is very strange

          timestamps {
           node(env.NODE_NAME) {        
            def lib =   library("Common@${env.LIB_VERSION}").com.mycompany.jenkins             
            def fooLib1 = lib.Foo.new(this)
            def fooLib2 = lib.Foo.new(this)        
           
            fooLib2.doStuffWithCurrentBuild()
          }
          }
          

          Stephane Moser added a comment - abayer the syntax that you give me doesn't work. Also, the syntax I am using appears in the Jenkins shared library documentation  https://jenkins.io/doc/book/pipeline/shared-libraries/   def useSomeLib(helper) { // dynamic: cannot declare as Helper helper.prepare() return helper.count() } def lib = library( 'my-shared-library' ).com.mycorp.pipeline // preselect the package echo useSomeLib(lib.Helper. new (lib.Constants.SOME_TEXT)) But if I instantiate the same clase mutiple times and call the second instante it will work. That is very strange timestamps { node(env.NODE_NAME) { def lib = library( "Common@${env.LIB_VERSION}" ).com.mycompany.jenkins def fooLib1 = lib.Foo. new ( this ) def fooLib2 = lib.Foo. new ( this ) fooLib2.doStuffWithCurrentBuild() } }

            Unassigned Unassigned
            moser_sts Stephane Moser
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: