I would like to persist the state of some logic between calls in my Jenkins shared library. I don't want to pass in an object/map/... with the state in the arguments but use some kind of global variable/map for this. I failed with global varibales in the foobar.groovy below or use of environment variables. I found a work-around by creating a small helper class with a static map to store my data but this does not seem to be the right solution. Is there a cleaner way?
src/pkg/GlobalConfig.groovy
package pkg;
class GlobalConfig {
// define a static map
static Map data = new HashMap()
}
use map in shared library eg vars/foobar.groovy
import pkg.GlobalConfig
def do_something() {
def data = pkg.GlobalConfig.data
// do something with data
}
Define your global variables with the @groovy.transform.Field
annotation.
Take a look at the Defining global variables section in the pipeline documentation.
foobar.groovy
can look like:
import groovy.transform.Field
@Field
def data = [:]
def do_something() {
// use data as is
}
From outside this file just use foobar.data
.
Once the library is loaded the state will be persistent whenever you use it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.