gpt4 book ai didi

Jenkins Groovy - 使用 readYaml 中的修改数据写回 yml 文件

转载 作者:行者123 更新时间:2023-12-02 20:30:25 31 4
gpt4 key购买 nike

我使用Jenkins readYaml读取数据如下:

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

我想将修改后的数据写回到 Jenkins 中的 test.yml 中。如何才能实现这一目标?

最佳答案

测试.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)
}

最终测试.yml:

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

关于Jenkins Groovy - 使用 readYaml 中的修改数据写回 yml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45694656/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com