I am trying to create some shared library for Jenkins and after a very long journey I managed to almost finish it. Unfortunately at this point I am stuck with a very weird behaving / malfunctioning Jenkins instance.
All I need to achieve at last is to create some JSON string of a map with a nested list. Sounds trivial but seems highly problematic in a Jenkins shared library.
I have something like:
class SpecialJob implements Serializable {
private class Entry {
String name
String description
}
def steps
SpecialJob (steps) { this.steps = steps }
...
def provide (Map args) {
List<Entry> entries = new ArrayList<Entry>
... // Some code that adds entries to the list
// The following clearly shows that there are items in the list
// I get lines printed that show the content of the entries
entries.each { entry ->
steps.echo "Entry: ${entry.name} - ${entry.description}"
}
def content = [
name: args.name,
items: [buckets: entries]
]
def jsonContent = JsonOutput.toJson (content)
// Now in the Json, after the name: "items" : {"buckets": [{}]}}
The list buckets
is always empty in the Json string. If I test the code standalone, without Jenkins, it works as expected. I have absolutely no clue why this happens. The same happens if I use steps.writeJSON
– the list in the json string is empty.
To make sure: The list is not empty! The items in there get printed on the Jenkins console output, they are there.
I was already so frustrated that I thought I’ll just create that string myself. This doesn’t work as well, every quote I print in the string gets printed with the escaping backslash. So the following string in Jenkins:
String item = ""name": "${entry.descrition}""
Gets printed as "name": "Some description"
. Again, if I test the code outside Jenkins it works, the string contains the quotes correctly.
Can I somehow get this to work? All I want is a simple json string from this map.