From Extending with Shared Libraries - Directory structure I created a shared library where I have multiple *.groovy files implementing global variables in the vars folder.
Can I add subfolders to vars to organize better my files? I tried, with no luck at the moment of consuming the global variable. Is there a specific syntax I need to use to reference a file in a subfolder? or subfolder are just not supported?
Unfortunately, no, you cannot. There is a declined improvement request at Jenkins' issue tracker. The given reason is that filenames are mapped directly to variable names.
Other approaches typical in Groovy like
evaluate(new File("../tools/Tools.groovy"))
do not work as well, because the Jenkins global vars files are not native Groovy code but processed.
However, there is something you can use to better organize helper functions for those which are not custom pipeline steps.
I have an includes.groovy
file containing different functions like
def doSomething() {
}
def doSomethingElse() {
}
In a customPipelineStep.groovy
file I can then access them with
def call() {
includes.doSomethingElse()
}
So includes
works somehow like a namespace, and you could have multiple such utility files. They are no folders, but help organizing stuff.
Instead of defining custom steps in individual files, you could also group them together in files, but then you would have to wrap them in a script block within your pipeline to access them, as pointed out in the documentation. In the same way, include-functions are also publicly available in script-blocks, so be aware that they are not private.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With