What is the vars Directory in Jenkins Shared Library?
vars directory holds Groovy scripts that define global variables or functions accessible in pipelines. These scripts simplify pipeline code by providing reusable steps as simple function calls. Each file in vars represents a global variable or step available to all Jenkinsfiles using the library.How It Works
The vars directory in a Jenkins shared library acts like a toolbox of ready-to-use functions or variables for your pipelines. Imagine it as a kitchen drawer where you keep your favorite cooking tools handy. Each Groovy script file inside vars defines a global variable or step that Jenkins pipelines can call directly without importing anything.
When Jenkins runs a pipeline that uses a shared library, it automatically loads all scripts from the vars folder. This means you can write simple, clean pipeline code that calls these global steps as if they were built-in commands. It helps teams share common logic like deployment steps, notifications, or build commands in one place.
Example
This example shows a simple global step defined in vars/hello.groovy that prints a greeting message. You can call hello() directly in your Jenkinsfile after loading the shared library.
def call(String name = 'World') { echo "Hello, ${name}!" }
When to Use
Use the vars directory when you want to create simple, reusable pipeline steps that can be called easily across many Jenkinsfiles. It is perfect for common tasks like sending notifications, running tests, or deploying applications.
For example, if your team always sends a Slack message after a build, you can write a vars/sendSlack.groovy script once and call sendSlack() in all pipelines. This saves time, reduces errors, and keeps your Jenkinsfiles clean and consistent.
Key Points
- vars holds Groovy scripts defining global variables or steps.
- Scripts in
varsare automatically loaded and callable in pipelines. - Each file represents one global step or variable.
- Use it for simple reusable pipeline functions.
- Keeps Jenkinsfiles clean and promotes code sharing.