I have choice-properties in my Jenkinsfile like this:
properties(
[parameters([
choice(choices: ["app1", "app2"].join("\n"),
description: 'global parameter',
name: 'globalParam'),
choice(choices: ["foo", "bar", "baz"].join("\n"),
description: 'job-specific parameter',
name: 'localParam')
])
]
)
This works, but I have multiple jobs (=multiple Jenkisnfiles) which all use the same array of globalParam
, so when I want to add another choice (e.g. 'app3'
), I have to update every Jenkinsfile.
Is there a way to share such global parameter between multiple Jenkinsfiles?
In fact, there are also some other "global values" used in multiple Jenkinsfiles, so I already thought about using one global YAML-file to store those values - so I can use readYAML
inside my node{}
-block to fill my variables. But I don't know if/how I can use this yaml inside my properties()
-block.
Edit1: What I also tried:
import org.yaml.snakeyaml.Yaml
def myyaml = new Yaml().load(new FileReader('../../properties.yml'))
properties( ... )
But therefore I get
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new org.yaml.snakeyaml.Yaml
Edit2: Based on @herm s suggestion, I tried to use the library plugin:
import groovy.json.JsonSlurperClassic;
def propsText = libraryResource 'my/lib/properties.json'
def jsonSlurper = new JsonSlurperClassic()
def properties = jsonSlurper.parseText(propsText)
def jobEnv = 'release'
properties(
[parameters([
choice(choices: properties.params.modules.keySet().join("\n"),
description: properties.props.module.description[jobEnv],
name: 'globalParam'),
choice(choices: ["foo", "bar", "baz"].join("\n"),
description: 'job-specific parameter',
name: 'localParam')
])
]
)
I can see the correct choice parameter in my Jenkins UI! Based on this question, I approved JsonSlurperClassic. But the job still fails - without error message (and no pending approvals). So what's the problem?