0
0
Jenkinsdevops~5 mins

Implicit vs explicit loading in Jenkins - CLI Comparison

Choose your learning style9 modes available
Introduction
Jenkins pipelines can load shared code or libraries in two ways: implicitly or explicitly. This helps reuse code and organize your pipeline scripts better.
When you want Jenkins to automatically load shared libraries without mentioning them in every pipeline.
When you prefer to control exactly when and which shared libraries are loaded in your pipeline.
When you want to avoid loading unused libraries to speed up pipeline execution.
When you want to make your pipeline scripts clearer by showing all dependencies explicitly.
When you want to share common functions or steps across multiple pipelines.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Example with Explicit Loading') {
      steps {
        script {
          // Explicitly load the shared library
          def myLib = library('my-shared-library')
          myLib.someFunction()
        }
      }
    }
    stage('Example with Implicit Loading') {
      steps {
        script {
          // Use functions from implicitly loaded library directly
          someFunction()
        }
      }
    }
  }
}

This Jenkinsfile shows two ways to load shared libraries.

Explicit loading uses the library() step inside the script block to load the library and assign it to a variable. You then call functions via that variable.

Implicit loading assumes the library is configured in Jenkins global settings to load automatically. You can call its functions directly without extra code.

Commands
This command updates the Jenkins job configuration with the pipeline that uses explicit or implicit loading. It applies the pipeline script to Jenkins.
Terminal
jenkins-jobs --conf jenkins.ini update my-pipeline.yaml
Expected OutputExpected
Job 'my-pipeline' updated successfully
This command triggers the pipeline build and shows the console output to verify the shared library functions run correctly.
Terminal
jenkins-cli build my-pipeline -s
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Example with Explicit Loading) [Pipeline] script [Pipeline] library [Pipeline] someFunction Function from shared library executed [Pipeline] } [Pipeline] stage [Pipeline] { (Example with Implicit Loading) [Pipeline] script [Pipeline] someFunction Function from shared library executed [Pipeline] } [Pipeline] End of Pipeline Finished: SUCCESS
-s - Show console output after build
Key Concept

If you remember nothing else from this pattern, remember: implicit loading lets Jenkins auto-load shared libraries, while explicit loading requires you to load them in your pipeline code.

Common Mistakes
Calling shared library functions without explicit loading when the library is not configured for implicit loading.
Jenkins will fail to find the functions and the pipeline will error out.
Either configure the library for implicit loading in Jenkins global settings or load it explicitly in the pipeline using the library() step.
Using explicit loading but forgetting to assign the library to a variable before calling its functions.
Functions cannot be called directly without the variable reference, causing errors.
Assign the library to a variable like def myLib = library('lib-name') and call functions via myLib.functionName().
Summary
Explicit loading requires calling the library() step in the pipeline script to load shared code.
Implicit loading lets Jenkins automatically load shared libraries configured globally, so functions can be called directly.
Use explicit loading for control and clarity; use implicit loading for convenience and less code.