write yaml file in jenkins with groovy

25,718

Solution 1

The Pipeline Utility Steps plugin has the readYaml and writeYaml steps to interact with YAML files. writeYaml will not overwrite your file by default so you have to remove it first.

def filename = 'values.yaml'
def data = readYaml file: filename

// Change something in the file
data.image.tag = applicationVersion

sh "rm $filename"
writeYaml file: filename, data: data

Solution 2

You can use "writeYaml" with a flag "overwrite" set to true.
This will allow making updates to YAML file in place.
By default, it is set to false.

You can read more about that in Pipeline Utility Steps Documentation

Share:
25,718
Christopher
Author by

Christopher

Updated on July 05, 2022

Comments

  • Christopher
    Christopher almost 2 years

    What is the best way to write/modify a *.yaml file in Groovy?

    I would like to modify the version maintained in a yaml file within my jenkins pipeline job. With readYaml I can get the content, but how can I write it back again?

    One way that comes to my mind would be to do a sed on the file. But I think thats not very accurate.