我正在使用Jenkins readYaml读取数据,如下所示:
data = readYaml file: "test.yml" //modify data.info = "b"
我想将此修改后的数据写回到Jenkins中的test.yml。如何做到这一点?
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) }
最终test.yml:
data: info: hello world! aaa: bbb ddd: ccc