0
0
JenkinsConceptBeginner · 3 min read

What is the vars Directory in Jenkins Shared Library?

In Jenkins shared libraries, the 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.

groovy
def call(String name = 'World') {
    echo "Hello, ${name}!"
}
Output
Hello, Jenkins!
🎯

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 vars are 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.

Key Takeaways

The vars directory defines global pipeline steps as Groovy scripts in Jenkins shared libraries.
Scripts in vars are automatically available to all pipelines using the library.
Use vars for simple reusable functions to keep Jenkinsfiles clean and consistent.
Each file in vars corresponds to one global variable or step callable in pipelines.
It helps teams share common pipeline logic easily across projects.