Write yaml file in jenkins using groovy

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

I would like to change the version stored in the yaml file in my work with jenkins labor. With readYamlI can get the content, but how can I record it again?

One of the ways that comes to my mind is to do sedin a file. But I think this is not very accurate.

+4
source share
2 answers

The program steps for the pipeline , there are steps readYamland writeYamlto interact with YAML files. writeYamlwill not overwrite your default file, so you need to delete 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
+2

yaml, , String .

, unit test, :

, src/test/resources version.yaml, :

version: '0.0.1'

anotherProperty: 'value'

@Test
void replaceVersion() {
    File yaml = new File("src/test/resources/version.yaml")
    println yaml.text

    String newVersion = "2.0.0"
    yaml.text = yaml.text.replaceFirst(/version: '.*'/, "version: '${newVersion}'")
    println yaml.text
}
0

Source: https://habr.com/ru/post/1673711/


All Articles