0
0
Jenkinsdevops~5 mins

Library directory structure in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you build many Jenkins pipelines, you often reuse code. The library directory structure helps organize this reusable code so you can share it easily across projects.
When you want to share common pipeline steps between multiple Jenkins jobs without copying code.
When you want to keep your pipeline code clean by moving complex logic to separate files.
When you want to update shared pipeline code in one place and have all jobs use the new version.
When you want to organize helper functions, classes, or scripts used by your Jenkins pipelines.
When you want to separate pipeline code by teams or projects but still keep it in one place.
Config File - vars/mySharedStep.groovy
vars/mySharedStep.groovy
def call(String name) {
    echo "Hello, ${name}! This is a shared step."
}

This file defines a shared step named mySharedStep in the vars directory. The call method allows this step to be used like a function in Jenkins pipelines. The vars folder is where you put reusable pipeline steps.

Commands
Create the directory structure for the shared library inside Jenkins home directory. The vars folder holds reusable pipeline steps.
Terminal
mkdir -p /var/jenkins_home/shared-libraries/vars
Expected OutputExpected
No output (command runs silently)
Create a Groovy file defining a shared step named mySharedStep that prints a greeting message. This file goes inside the vars directory.
Terminal
echo 'def call(String name) {\n    echo "Hello, ${name}! This is a shared step."\n}' > /var/jenkins_home/shared-libraries/vars/mySharedStep.groovy
Expected OutputExpected
No output (command runs silently)
Create a Jenkins pipeline job that uses the shared library. This command uploads the job configuration to Jenkins.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080/ create-job my-pipeline < my-pipeline-config.xml
Expected OutputExpected
Created job: my-pipeline
Trigger a build of the Jenkins pipeline job to test if the shared library step works.
Terminal
curl -X POST http://localhost:8080/job/my-pipeline/build --user admin:admin123
Expected OutputExpected
Started build #1
Key Concept

If you remember nothing else from this pattern, remember: the vars directory inside a Jenkins shared library holds reusable pipeline steps as Groovy scripts.

Common Mistakes
Placing shared step Groovy files outside the vars directory.
Jenkins only recognizes reusable steps inside the vars folder; files elsewhere won't be loaded automatically.
Always put reusable pipeline step scripts inside the vars directory in your shared library.
Not defining a call method in the Groovy file for the shared step.
Without a call method, Jenkins cannot invoke the step like a function in pipelines.
Define a call method in your Groovy script to make the step callable.
Forgetting to configure the shared library in Jenkins global settings.
Jenkins won't know where to find your shared library code without proper configuration.
Add the shared library path in Jenkins global configuration under 'Configure System'.
Summary
Create a shared library directory with a vars folder to hold reusable pipeline steps.
Write Groovy scripts with a call method inside vars to define shared steps.
Use Jenkins CLI or UI to create pipeline jobs that use the shared library.
Trigger builds to verify the shared steps work as expected.