9

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?

rafal1337
  • 164
  • 10
  • of course that is possible. For example there is http://groovy-lang.org/json.html. – smelm Oct 09 '19 at 14:05
  • If it's possible to define additional classpath in DSL plugin. then put into the folder that you will add to classpath groovy file(s) like `class GLOBAL{ def a=111; def b=222; }`. then in code you should be able to access it `GLOBAL.a` – daggett Oct 09 '19 at 14:07
  • @daggett I was trying as you wrote and always I have got error: `org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'org.codehaus.groovy.runtime.InvokerHelper$1@641eff69' with class 'org.codehaus.groovy.runtime.InvokerHelper$1' to class 'javaposse.jobdsl.dsl.JobParent'` This error occurs during processing your class GLOBAL – rafal1337 Oct 09 '19 at 14:21
  • i forgot . for each defined variable there should be `static` prefix. `class GLOBAL{ static def a=111; static def b=222; }`. but error you have is strange. could you show the variable you've declared and how you use it in dsl. – daggett Oct 09 '19 at 14:52
  • 1. My configuration of the dsl https://postimg.cc/TLHK5k6h 2. error during dsl processing https://postimg.cc/21F37jDs – rafal1337 Oct 09 '19 at 19:36
  • 3. structure of the DSL https://postimg.cc/BXfKTVF1 . Jenkins is the newest -2.190.1, addon DSL also up-to-date. – rafal1337 Oct 09 '19 at 19:42
  • @daggett do you have any idea? – rafal1337 Oct 11 '19 at 18:24
  • @rafal1337, i never worked with this plugin, and according to the screenshot there is no additional classpath parameter. check documentation for `config*` methods and probably `JsonSlurper` could be a solution. check example here: https://github.com/jenkinsci/job-dsl-plugin/wiki/Tutorial---Using-the-Jenkins-Job-DSL#4-adding-additional-jobs-to-the-dsl-script – daggett Oct 11 '19 at 19:41
  • The error means you have your class has to extend JobParent, as they have their own script loading infrastructure and that infrastructure expects only a JobParent, not an arbitrary class. – blackdrag Oct 12 '19 at 10:48

3 Answers3

3

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"
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • 2
    This is a minimally attributed copy from the other post; you'd at least want to follow the [referencing guidelines](https://stackoverflow.com/help/referencing) and name the author. – Martijn Pieters Oct 18 '19 at 15:21
  • Unfornetly that doesn't work in Jenkins DSL, can't find the file with properties, but this file is the same folder... `FATAL: test.properties (No such file or directory) java.io.FileNotFoundException: test.properties (No such file or directory)` – rafal1337 Oct 29 '19 at 10:33
  • @rafal1337 Did you try using partial/full path of file? – Ori Marko Oct 29 '19 at 11:05
  • @user7294900 Yes, I was trying multiple path but without positive effect. – rafal1337 Oct 29 '19 at 13:42
  • @rafal1337 try `readFile` https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/ – Ori Marko Oct 29 '19 at 13:50
2

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
}
Chan Lee
  • 71
  • 1
  • 5
  • looks good but in DSL plugin in Jenkins doesn't work: `ERROR: (pipeline.groovy, line 1) No signature of method: pipeline.load() is applicable for argument types: (java.lang.String) values: [a_test.groovy]Possible solutions: job(java.lang.String), find(), folder(java.lang.String), job(java.lang.String, groovy.lang.Closure), find(groovy.lang.Closure), wait()` – rafal1337 Oct 29 '19 at 10:31
2

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.

Kaus2b
  • 747
  • 4
  • 12