frankd
frankd

Reputation: 1413

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

Upvotes: 0

Views: 5492

Answers (1)

Sebastian Häni
Sebastian Häni

Reputation: 566

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

Upvotes: 2

Related Questions