Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing variables beetwen groovy files

I'm managing many jobs in Jenkins by DSL plugin. That plugin is using .groovy definitions so I think even if someone doesn't use Jenkins but using groovy may be able to help.

Generally, I want to create an additional file, that may be a groovy file, JSON or YAML, whatever. It important is the possibility to connect that file with my .groovy file.

In that file, I'm defining variables(rather just strings) for example address IP or other stuff eg.

ip_gitlab: 1.2.3.4
default_user: admin

In my groovy files, I want to be able to use these variables.

That approach is possible in groovy?

like image 397
rafal1337 Avatar asked Oct 09 '19 13:10

rafal1337


3 Answers

I suggest use a property file as @JBaruch wrote

ip_gitlab=1.2.3.4
default_user=admin

And load it

Properties properties = new Properties()
File propertiesFile = new File('test.properties')
propertiesFile.withInputStream {
    properties.load(it)
}

Then you can use it, get ip for example:

def ipPropertyName= 'ip_gitlab'
properties."$ipPropertyName"
like image 167
user7294900 Avatar answered Nov 13 '22 14:11

user7294900


Make groovy file and define some general information and use load.

E.g., hello.conf (written by groovy)

build_name = 'hello'

build_config = [
    'git': 'your git repository',
    'build_job': ['bulid_a', 'build_b']
]

And use it by load

load 'hello.conf'

println(build_name)
for (job in build_config['build_job']) {
    build job: job
}
like image 35
Chan Lee Avatar answered Nov 13 '22 16:11

Chan Lee


if you want a Jenkins specific answer: There's a Config File Provider Plugin to jenkins.

You can store config/properties files via Managed files. Go to Manage Jenkins>Managed files and and create a new file. It supports .groovy, .json, .xml and many others.

Once you have that, you can load the said file inside a job using the Provide Config file checkbox which will load the file into an env variable automatically.

like image 2
Kaus2b Avatar answered Nov 13 '22 14:11

Kaus2b