1

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?

Munchkin
  • 4,528
  • 7
  • 45
  • 93

2 Answers2

0

Yes you can use the global shared libraries plugin for that and have those parameters in a library

herm
  • 14,613
  • 7
  • 41
  • 62
0

Finally, it works:

I added a library in my vars folder:

// vars/jsonParse.groovy
import groovy.json.JsonSlurperClassic;

@NonCPS
def call(def json) {
    return new groovy.json.JsonSlurperClassic().parseText(json)
}

My json-file is located at resources/some/project/properties.json

And my Jenkinsfile:

def propsText = libraryResource 'some/project/properties.json'
def properties = jsonParse(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')
    ])
]
)

Edit:

Sadly, I couldn't manage to solve this for a Declarative Pipeline.

Munchkin
  • 4,528
  • 7
  • 45
  • 93