Jenkins Groovy-使用来自readYaml的修改后的数据写回yml文件 [英] Jenkins Groovy - using modified data from readYaml to write back into yml file

查看:551
本文介绍了Jenkins Groovy-使用来自readYaml的修改后的数据写回yml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jenkins readYaml读取数据,如下所示:

I am using Jenkins readYaml to read the data as follows:

data = readYaml file: "test.yml"
//modify
data.info = "b"

我想将此修改后的数据写回到Jenkins中的test.yml.如何做到这一点?

I want to write this modified data back to test.yml in Jenkins. How can this be achieved?

推荐答案

test.yml:

data:
  info: change me
  aaa: bbb
  ddd: ccc

管道脚本:

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.DumperOptions
import static org.yaml.snakeyaml.DumperOptions.FlowStyle.BLOCK

node {
    def yaml = readYaml file: "test.yml"
    yaml.data.info = 'hello world!'
    writeFile file:"test.yml", text:yamlToString(yaml)
}

@NonCPS
String yamlToString(Object data){
    def opts = new DumperOptions()
    opts.setDefaultFlowStyle(BLOCK)
    return new Yaml(opts).dump(data)
}

final test.yml:

final test.yml:

data:
  info: hello world!
  aaa: bbb
  ddd: ccc

这篇关于Jenkins Groovy-使用来自readYaml的修改后的数据写回yml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆