Can vars scripts reference variables defined in a higher scope?

18 views
Skip to first unread message

David Karr

unread,
Sep 16, 2021, 4:21:10 PM9/16/21
to Jenkins Users

I work on a somewhat complicated build process using Jenkins scripted pipeline, where the Jenkinsfile is very small, but it imports a large shared library, sets some application-specific properties, and then calls a top-level function in the "vars" section of the shared library.

The top-level function is pretty complicated, and it calls a bunch of other "var" functions.  Data is passed to those functions through "env." vars, and function parameters.

I am working on some changes to one of the lower-level functions, where it will reference a couple of "statically-defined" map objects that specify some metadata about build steps.

Initially, I simply defined those maps inside the "call()" function in the "vars" file.  That does work.  However, I see that there is more than one "vars" file that needs to reference those maps.

It would make sense if I could move these map definitions to the top-level "vars" file, I assume right inside the "call()" function (or somewhere else, if that makes sense) and then reference them inside the "vars" functions that need that data.

If these were scalar values, I would just set "env.<variablename> = <value>" in the top-level "vars" file, inside the "call()" function, and then reference that environment variable in the lower-level "vars" file.  Can I set a Map object into environment variables?  I wouldn't think it would allow that.

I technically could simply reference the variable name in the top-level "vars" function, and pass it down through the layers, but that gets messy.  I'd prefer to reference these maps like "static" variables (conceptually if not in actuality) in the lower-level "vars" file.

I have browsed the documentation, but I haven't seen an obvious way to do this.

David Karr

unread,
Sep 17, 2021, 1:17:05 AM9/17/21
to Jenkins Users
I've managed to implement something that is effectively the same.  I realized that I can define a class in "src" that is just a static data container and can be accessed from any of the "vars" scripts.
Reply all
Reply to author
Forward
0 new messages