SnakeYaml, or why is writeYaml Limited?

23 views
Skip to first unread message

George Sexton

unread,
Mar 22, 2019, 10:39:07 AM3/22/19
to Jenkins Users
I need to change a pod definition for Jenkins to be in YAML format so that I can add privileged.

I have a map already defined, if I can add one more attribute and convert it to yaml, it would be done. The problem is that writeYaml can't send the output to a text variable, and I can't write to a temp file because I don't have my pod defined yet.

First, why is writeYaml purposely limited to not output to a variable? readYaml can read from a text variable. I've run into other cases where I have to do an ugly hack of writing the output to a file, and then using readFile()

Does anyone have any examples of how to use the actual SnakeYaml library in Jenkins?

Victor Martinez

unread,
Mar 25, 2019, 5:02:11 PM3/25/19
to Jenkins Users
Something like the below snippet should work:

import org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.Yaml

@NonCPS
def readYaml(data) {
  return (new Yaml()).dump(data)
}

println readYaml([name: "foo", surname: "bar", age: 1])

writeYaml implementation uses that particular Class and method:

My 2 cents
Reply all
Reply to author
Forward
0 new messages