Bird
Raised Fist0
Jenkinsdevops~10 mins

Loading libraries in Jenkinsfile - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Loading libraries in Jenkinsfile
Start Jenkinsfile Execution
↓
Load Shared Library
↓
Library Methods Available
↓
Call Library Functions
↓
Continue Pipeline Steps
↓
End
The Jenkinsfile starts, loads a shared library, makes its functions available, calls them, then continues the pipeline.
Execution Sample
Jenkins
@Library('my-shared-lib') _

pipeline {
  agent any
  stages {
    stage('Use Lib') {
      steps {
        script {
          myFunction()
        }
      }
    }
  }
}
This Jenkinsfile loads a shared library named 'my-shared-lib' and calls a function 'myFunction' from it during the pipeline.
Process Table
StepActionEvaluationResult
1Start JenkinsfileN/AJenkinsfile execution begins
2Load library 'my-shared-lib'@Library('my-shared-lib') _Library loaded, functions available
3Enter pipeline stage 'Use Lib'stage('Use Lib')Stage starts
4Call function 'myFunction()' from librarymyFunction()Function executes successfully
5Complete stage 'Use Lib'stage completeStage ends
6Pipeline endspipeline completePipeline execution finishes
💡 Pipeline completes after all stages and library functions run
Status Tracker
VariableStartAfter Step 2After Step 4Final
libraryLoadedfalsetruetruetrue
myFunctionCalledfalsefalsetruetrue
Key Moments - 2 Insights
Why do we need to load the library before calling its functions?
Because the library functions are not available until Jenkins loads the library (see execution_table step 2). Calling functions before loading causes errors.
What happens if the library name is wrong?
Jenkins will fail at the library loading step (step 2) and stop the pipeline, because it cannot find the library.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the library loaded?
AStep 1
BStep 4
CStep 2
DStep 6
💡 Hint
Check the 'Action' column for 'Load library' in execution_table row 2
According to variable_tracker, when does 'myFunctionCalled' become true?
AAfter Step 4
BAfter Step 2
CAt Start
DNever
💡 Hint
Look at 'myFunctionCalled' row in variable_tracker after Step 4
If the library was not loaded, what would happen when calling 'myFunction()'?
AFunction runs normally
BPipeline fails with error
CPipeline skips the function
DFunction runs but returns null
💡 Hint
Refer to key_moments about calling functions before loading library
Concept Snapshot
Loading libraries in Jenkinsfile:
- Use library 'name' to load shared libraries
- Load before calling any library functions
- Loaded functions become available in pipeline
- Calling functions before loading causes errors
- Libraries help reuse code across pipelines
Full Transcript
This visual execution shows how Jenkinsfile loads a shared library named 'my-shared-lib'. The pipeline starts, then Jenkins loads the library making its functions available. Next, the pipeline enters a stage where it calls a function 'myFunction' from the library. After the function runs, the stage and pipeline complete. Variables track that the library is loaded before the function call. Key moments explain why loading first is important and what happens if the library name is wrong. The quiz tests understanding of when loading happens and effects of missing library load.

Practice

(1/5)
1. What is the main purpose of loading libraries in a Jenkinsfile?
easy
A. To automatically update Jenkins plugins
B. To reuse common pipeline code across multiple Jenkinsfiles
C. To increase the build speed by caching dependencies
D. To create new Jenkins jobs dynamically

Solution

  1. Step 1: Understand the role of libraries in Jenkinsfiles

    Libraries contain reusable pipeline code that can be shared across multiple Jenkinsfiles.
  2. Step 2: Identify the benefit of using libraries

    Using libraries helps keep Jenkinsfiles clean and avoids code duplication by reusing common code.
  3. Final Answer:

    To reuse common pipeline code across multiple Jenkinsfiles -> Option B
  4. Quick Check:

    Libraries = Reuse code [OK]
Hint: Libraries help reuse code, not speed or plugins [OK]
Common Mistakes:
  • Confusing libraries with plugin management
  • Thinking libraries speed up builds directly
  • Assuming libraries create jobs automatically
2. Which of the following is the correct syntax to load a shared library named 'utils' at version '1.2' in a Jenkinsfile?
easy
A. loadLibrary('utils', '1.2')
B. useLibrary utils version 1.2
C. import 'utils:1.2'
D. library 'utils@1.2'

Solution

  1. Step 1: Recall the Jenkinsfile syntax for loading libraries

    The correct syntax uses the keyword library followed by the library name and version in quotes, separated by '@'.
  2. Step 2: Match the syntax to the options

    library 'utils@1.2' matches the correct syntax: library 'utils@1.2'.
  3. Final Answer:

    library 'utils@1.2' -> Option D
  4. Quick Check:

    library 'name@version' = correct syntax [OK]
Hint: Use library 'name@version' format to load libraries [OK]
Common Mistakes:
  • Using incorrect keywords like loadLibrary or import
  • Missing quotes around the library name and version
  • Using spaces instead of '@' between name and version
3. Given this Jenkinsfile snippet:
library 'common@2.0'
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        script {
          echo common.hello()
        }
      }
    }
  }
}

What will happen when this pipeline runs if the 'common' library has a method hello() that returns 'Hello from common library'?
medium
A. The pipeline will print 'Hello from common library' in the console output
B. The pipeline will fail with a method not found error
C. The pipeline will print 'common.hello()' as a string
D. The pipeline will skip the 'Test' stage silently

Solution

  1. Step 1: Understand library loading and method call

    The library 'common@2.0' is loaded, so its methods like hello() are available in the Jenkinsfile.
  2. Step 2: Analyze the echo statement

    The echo command calls common.hello(), which returns the string 'Hello from common library', so this string will be printed.
  3. Final Answer:

    The pipeline will print 'Hello from common library' in the console output -> Option A
  4. Quick Check:

    Loaded library method call prints returned string [OK]
Hint: Loaded library methods return values usable in pipeline [OK]
Common Mistakes:
  • Assuming method calls fail without explicit import
  • Thinking echo prints code literally
  • Expecting silent skip without error
4. You wrote this Jenkinsfile snippet:
library 'tools@latest'
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        script {
          tools.build()
        }
      }
    }
  }
}

But the pipeline fails with an error: groovy.lang.MissingPropertyException: No such property: tools. What is the likely cause?
medium
A. The agent 'any' is not supported in this Jenkins version
B. The method build() does not exist in the tools library
C. The library 'tools' is not configured or not loaded properly
D. The Jenkinsfile syntax for stages is incorrect

Solution

  1. Step 1: Understand the error message

    The error says 'No such property: tools', meaning the variable or library named 'tools' is not recognized in the pipeline.
  2. Step 2: Identify the cause related to library loading

    This usually happens if the shared library 'tools' is not configured in Jenkins or the library failed to load, so the variable 'tools' is undefined.
  3. Final Answer:

    The library 'tools' is not configured or not loaded properly -> Option C
  4. Quick Check:

    MissingPropertyException means library not loaded [OK]
Hint: MissingPropertyException means library not loaded or configured [OK]
Common Mistakes:
  • Assuming method build() missing causes this error
  • Blaming pipeline syntax for this error
  • Thinking agent 'any' causes this error
5. You want to load two shared libraries in your Jenkinsfile: 'utils' at version '1.0' and 'deploy' at version '2.1'. Which Jenkinsfile snippet correctly loads both libraries and calls a method deployApp() from the 'deploy' library inside a stage?
hard
A. library 'utils@1.0' library 'deploy@2.1' pipeline { agent any stages { stage('Deploy') { steps { script { deploy.deployApp() } } } } }
B. libraries { utils '1.0' deploy '2.1' } pipeline { agent any stages { stage('Deploy') { steps { script { deploy.deployApp() } } } } }
C. library 'utils@1.0,deploy@2.1' pipeline { agent any stages { stage('Deploy') { steps { script { deployApp() } } } } }
D. library 'utils@1.0' library 'deploy@2.1' pipeline { agent any stages { stage('Deploy') { steps { script { deployApp() } } } } }

Solution

  1. Step 1: Load multiple libraries separately

    Each library must be loaded with its own library 'name@version' statement; library 'utils@1.0' library 'deploy@2.1' pipeline { agent any stages { stage('Deploy') { steps { script { deploy.deployApp() } } } } } correctly loads 'utils@1.0' and 'deploy@2.1' separately.
  2. Step 2: Call method from the correct library namespace

    To call deployApp() from 'deploy' library, use deploy.deployApp() inside the script block, as shown in library 'utils@1.0' library 'deploy@2.1' pipeline { agent any stages { stage('Deploy') { steps { script { deploy.deployApp() } } } } }.
  3. Final Answer:

    Option A correctly loads both libraries and calls deploy.deployApp() -> Option A
  4. Quick Check:

    Separate library calls + qualified method call = library 'utils@1.0' library 'deploy@2.1' pipeline { agent any stages { stage('Deploy') { steps { script { deploy.deployApp() } } } } } [OK]
Hint: Load each library separately; call methods with library name prefix [OK]
Common Mistakes:
  • Trying to load multiple libraries in one statement
  • Calling methods without library prefix
  • Using incorrect syntax like 'libraries' block
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard