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

Library section within declarative syntax to explicitly load shared libraries

      Right now users must use an @Library annotation to load libraries in Pipeline script. This works within a Pipeline with declarative syntax but must be called outside of the pipeline block.

      It would be nice if we could load libraries within the pipeline block with a library configuration section.

          [JENKINS-38110] Library section within declarative syntax to explicitly load shared libraries

          Patrick Wolf created issue -
          Andrew Bayer made changes -
          Link New: This issue relates to JENKINS-37825 [ JENKINS-37825 ]

          Peter Leibiger added a comment - - edited

          Not sure if this is the right issue but there are probably some things to consider for this issue.
          Up until version 0.4 I was able to use @Library like this:

          @Library('foo@master')
          def util = new foo.Util()
          
          timestamps {
            pipeline {
              agent docker: 'maven:3-jdk-8'
          
              stages {
                stage('Pre-Build') {
                  util.doSomething()
                }
                // more stages
              }
          
              notifications {
                success {
                  util.notifyBuildStatusSuccess()
                }
                ...
              }
          }
          

          Now with 0.4 it fails to find the @Library annotation, I can work around by using the FQN.
          But I can't get a single Util instance to work anymore, best I came up with is this:

          @org.jenkinsci.plugins.workflow.libs.Library('foo@master')
          import foo.Util
          
          // timestamps don't work anymore
          //timestamps {
            pipeline {
              agent docker: 'maven:3-jdk-8'
          
              stages {
                stage('Pre-Build') {
                  script {
                    def util = new Util()
                    util.doSomething()
                  }
                }
                // more stages
              }
          
              notifications {
                success {
                  script {
                    def util = new Util()
                    util.notifyBuildStatusSuccess()
                  }
                }
                ...
              }
            }
          //}
          

          I tried putting the Util in environment but that throws ....sandbox.RejectedAccessException:

          @org.jenkinsci.plugins.workflow.libs.Library('foo@master')
          import foo.Util
          
          // timestamps don't work anymore
          //timestamps {
            pipeline {
              agent docker: 'maven:3-jdk-8'
          
              environment {
                util = new Util()
              }
          
              stages {
                stage('Pre-Build') {
                  script {
                    env.util.doSomething()
                  }
                }
                // more stages
              }
          
              notifications {
                success {
                  script {
                    env.util.notifyBuildStatusSuccess()
                  }
                }
                ...
              }
            }
          //}
          

          Peter Leibiger added a comment - - edited Not sure if this is the right issue but there are probably some things to consider for this issue. Up until version 0.4 I was able to use @Library like this: @Library( 'foo@master' ) def util = new foo.Util() timestamps { pipeline { agent docker: 'maven:3-jdk-8' stages { stage( 'Pre-Build' ) { util.doSomething() } // more stages } notifications { success { util.notifyBuildStatusSuccess() } ... } } Now with 0.4 it fails to find the @Library annotation, I can work around by using the FQN. But I can't get a single Util instance to work anymore, best I came up with is this: @org.jenkinsci.plugins.workflow.libs.Library( 'foo@master' ) import foo.Util // timestamps don't work anymore //timestamps { pipeline { agent docker: 'maven:3-jdk-8' stages { stage( 'Pre-Build' ) { script { def util = new Util() util.doSomething() } } // more stages } notifications { success { script { def util = new Util() util.notifyBuildStatusSuccess() } } ... } } //} I tried putting the Util in environment but that throws ....sandbox.RejectedAccessException : @org.jenkinsci.plugins.workflow.libs.Library( 'foo@master' ) import foo.Util // timestamps don't work anymore //timestamps { pipeline { agent docker: 'maven:3-jdk-8' environment { util = new Util() } stages { stage( 'Pre-Build' ) { script { env.util.doSomething() } } // more stages } notifications { success { script { env.util.notifyBuildStatusSuccess() } } ... } } //}

          Actually this works as well. Until 0.4 it worked without the import, now it doesn't.

          @org.jenkinsci.plugins.workflow.libs.Library('foo@master')
          import foo.Util
          
          // doesn't work anymore
          // def util = new foo.Util()
          def util = new Util()
          

          Peter Leibiger added a comment - Actually this works as well. Until 0.4 it worked without the import, now it doesn't. @org.jenkinsci.plugins.workflow.libs.Library( 'foo@master' ) import foo.Util // doesn't work anymore // def util = new foo.Util() def util = new Util()
          Andrew Bayer made changes -
          Link New: This issue depends on JENKINS-39450 [ JENKINS-39450 ]

          Dominik Bartholdi added a comment - pleibiger see JENKINS-40642
          Liam Newman made changes -
          Link New: This issue relates to JENKINS-41396 [ JENKINS-41396 ]
          Andrew Bayer made changes -
          Status Original: Open [ 1 ] New: In Progress [ 3 ]

          Andrew Bayer added a comment -

          Andrew Bayer added a comment - Very early work-in-progress PR up at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/128
          Andrew Bayer made changes -
          Remote Link New: This issue links to "PR #128 (Web Link)" [ 15508 ]

            abayer Andrew Bayer
            hrmpw Patrick Wolf
            Votes:
            3 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: