jenkinsjenkins-pipelinejenkins-groovy

How to manage (read & write) variables values between pipeline and shared libraries?


I have a Declarative Pipeline that is increasing in size, so I want to split the logic using shared libraries. But I´m facing some issues because I must share some values between them and I can´t find the way to do it. Here are some examples of what I need to do:

  1. I have different branches in my git repo and different environments where the job could work, so I have several variables that are dynamically populated based on the current execution requirements to store the right branch name, server name,... I need to expose that information with the library methods to execute the logic in the right targets.
  2. I also need library methods can change some pipeline variables values to reflect the results of different actions done in those methods. Ex: to allow a future action in the pipeline to know which specific components were affected.

Is there some way to solve this issue? I´m sure there must be something because the global/local variables scope is a foundation for coding, but I can´t find anything.

I tried using environment variables, but they can only be written in the environment clause. I know you can change them using withEnv or other environment clauses, but you cannot change the value and access to the new value in a different place, so they are more like constants that can be used in the piece of code they are populated. So I can access to environment variables with the value when they were defined, but I can´t change that value based on the current execution conditions (ex: if it must be executed in branch A or B).


Solution

  • Finally, I included multiple variables as input parameters for each method, and they return multiple values. It is not the best option for me, as I must pass around 10-12 variables for input and 4-5 for output, even when most of them are needed for input and output, so if I could pass them by reference to update them directly, or even better if I could access external vars inside of each method, it would be perfect, but at least it is working as required.

    Thanks for your help.