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

          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()

          Dominik Bartholdi added a comment - pleibiger see JENKINS-40642

          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

          Code changed in jenkins
          User: Andrew Bayer
          Path:
          pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTLibraries.java
          pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTPipelineDef.java
          pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidator.java
          pipeline-model-api/src/main/resources/ast-schema.json
          pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Libraries.groovy
          pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Root.groovy
          pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/JSONParser.groovy
          pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/ModelParser.groovy
          pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidatorImpl.groovy
          pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ClosureModelTranslator.groovy
          pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/LibrariesTranslator.groovy
          pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/Messages.properties
          pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy
          http://jenkins-ci.org/commit/pipeline-model-definition-plugin/91d361bb30134b79fdf6ed4089e7cd110008c289
          Log:
          JENKINS-38110 Add a libraries section

          Note that this is very preliminary at this point - like, it doesn't
          actually call the library step yet, since we don't yet have a release
          of workflow-cps-global-lib-plugin with that included to depend on. We
          also don't have parsing tests or any of that jazz. But that's ok.

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTLibraries.java pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTPipelineDef.java pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidator.java pipeline-model-api/src/main/resources/ast-schema.json pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Libraries.groovy pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Root.groovy pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/JSONParser.groovy pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/ModelParser.groovy pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidatorImpl.groovy pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ClosureModelTranslator.groovy pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/LibrariesTranslator.groovy pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/Messages.properties pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy http://jenkins-ci.org/commit/pipeline-model-definition-plugin/91d361bb30134b79fdf6ed4089e7cd110008c289 Log: JENKINS-38110 Add a libraries section Note that this is very preliminary at this point - like, it doesn't actually call the library step yet, since we don't yet have a release of workflow-cps-global-lib-plugin with that included to depend on. We also don't have parsing tests or any of that jazz. But that's ok.

          Code changed in jenkins
          User: Andrew Bayer
          Path:
          pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTLibraries.java
          pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTPipelineDef.java
          pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidator.java
          pipeline-model-api/src/main/resources/ast-schema.json
          pipeline-model-definition/pom.xml
          pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Libraries.groovy
          pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Root.groovy
          pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/JSONParser.groovy
          pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/ModelParser.groovy
          pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidatorImpl.groovy
          pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ClosureModelTranslator.groovy
          pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/LibrariesTranslator.groovy
          pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/Messages.properties
          pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy
          pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/AbstractModelDefTest.java
          pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BasicModelDefTest.java
          pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/ValidatorTest.java
          pipeline-model-definition/src/test/resources/errors/emptyLibrariesDirective.groovy
          pipeline-model-definition/src/test/resources/errors/invalidLibrariesDirectiveContent.groovy
          pipeline-model-definition/src/test/resources/librariesDirective.groovy
          pom.xml
          http://jenkins-ci.org/commit/pipeline-model-definition-plugin/0c7d4fdadd4ad3b2a749853a3bb3eee567c54ae6
          Log:
          Merge pull request #128 from abayer/jenkins-38110

          JENKINS-38110 Add a libraries section

          Compare: https://github.com/jenkinsci/pipeline-model-definition-plugin/compare/4c9b7e24b846...0c7d4fdadd4a

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTLibraries.java pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTPipelineDef.java pipeline-model-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidator.java pipeline-model-api/src/main/resources/ast-schema.json pipeline-model-definition/pom.xml pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Libraries.groovy pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Root.groovy pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/JSONParser.groovy pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/parser/ModelParser.groovy pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/validator/ModelValidatorImpl.groovy pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ClosureModelTranslator.groovy pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/LibrariesTranslator.groovy pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/Messages.properties pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/AbstractModelDefTest.java pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BasicModelDefTest.java pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/ValidatorTest.java pipeline-model-definition/src/test/resources/errors/emptyLibrariesDirective.groovy pipeline-model-definition/src/test/resources/errors/invalidLibrariesDirectiveContent.groovy pipeline-model-definition/src/test/resources/librariesDirective.groovy pom.xml http://jenkins-ci.org/commit/pipeline-model-definition-plugin/0c7d4fdadd4ad3b2a749853a3bb3eee567c54ae6 Log: Merge pull request #128 from abayer/jenkins-38110 JENKINS-38110 Add a libraries section Compare: https://github.com/jenkinsci/pipeline-model-definition-plugin/compare/4c9b7e24b846...0c7d4fdadd4a

          Reinhold Füreder added a comment - See https://issues.jenkins-ci.org/browse/JENKINS-42730?focusedCommentId=301690&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-301690 of JENKINS-42730

          Liam Newman added a comment -

          Bulk closing resolved issues.

          Liam Newman added a comment - Bulk closing resolved issues.

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

              Created:
              Updated:
              Resolved: