简体   繁体   中英

Merging two yaml files in a Jenkins Groovy pipeline

In my Jenkins pipeline, I've got a yaml file that I need to apply to multiple environments, and separate environment specific yaml files that I'd like to inject or merge into the default file and write as a new file.

I've looked at readYaml and writeYaml here: https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/ But I'm not finding a good way of merging multiple files.

A simple example of what I'd like to achieve is here:

# config.yaml
config: 
   num_instances: 3
   instance_size: large
# dev-overrides.yaml
config:
    instance_size: small
# dev-config.yaml (desired output after merging dev-overrides.yaml in config.yaml)
config
    num_instances: 3
    instance_size: small

The Jenkins implementation of readYaml uses SnakeYAML as processor and supports YAML 1.1. You could possibly use the merge operator to accomplish your goal. But the merge operator has been removed in YAML 1.2. Thus I would not advise using this feature even it's currently available.

I would instead merge the objects with some Groovy code like this:

Map merge(Map... maps) {
    Map result = [:]
    maps.each { map ->
        map.each { k, v ->
            result[k] = result[k] instanceof Map ? merge(result[k], v) : v
        }
    }

    result
}


def config = readYaml text: """
config: 
   num_instances: 3
   instance_size: large
"""

def configOverrides = readYaml text: """
config:
    instance_size: small
"""

// Showcasing what the above code does:
println "merge(config, configOverrides): " + merge(config, configOverrides)
// => [config:[num_instances:3, instance_size:small]]
println "merge(configOverrides, config): " + merge(configOverrides, config)
// => [config:[instance_size:large, num_instances:3]]

// Write to file
writeYaml file: 'dev-config.yaml', data: merge(config, configOverrides)

Inspired by https://stackoverflow.com/a/27476077/1549149

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM