From 834dd1a1de6f2fec508a76285e6189165a795d99 Mon Sep 17 00:00:00 2001 From: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> Date: Mon, 11 Jan 2021 12:58:12 +0100 Subject: [PATCH] fix(CPE): correct handling of complex (json) data in writeToDisk (#2500) * correct handling of JSON data * fix typo * Update commonPipelineEnvironment.groovy --- vars/commonPipelineEnvironment.groovy | 32 ++++++++++----------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/vars/commonPipelineEnvironment.groovy b/vars/commonPipelineEnvironment.groovy index c13e2ef57..890294514 100644 --- a/vars/commonPipelineEnvironment.groovy +++ b/vars/commonPipelineEnvironment.groovy @@ -192,36 +192,28 @@ class commonPipelineEnvironment implements Serializable { ] void writeToDisk(script) { - files.each({f -> - if (this[f.property] && !script.fileExists(f.filename)) { - script.writeFile file: f.filename, text: this[f.property] - } + writeValueToFile(script, f.filename, this[f.property]) }) containerProperties.each({key, value -> - def fileName = ".pipeline/commonPipelineEnvironment/container/${key}" - if (value && !script.fileExists(fileName)) { - if(value in CharSequence) { - script.writeFile file: fileName, text: value - } else { - script.writeFile file: fileName, text: groovy.json.JsonOutput.toJson(value) - } - } + writeValueToFile(script, ".pipeline/commonPipelineEnvironment/container/${key}", value) }) valueMap.each({key, value -> - def fileName = ".pipeline/commonPipelineEnvironment/custom/${key}" - if (value && !script.fileExists(fileName)) { - if(value in CharSequence) { - script.writeFile file: fileName, text: value - } else { - script.writeFile file: fileName, text: groovy.json.JsonOutput.toJson(value) - } - } + writeValueToFile(script, ".pipeline/commonPipelineEnvironment/custom/${key}", value) }) } + void writeValueToFile(script, String filename, value){ + if (value){ + if (!value in CharSequence) filename += '.json' + if (script.fileExists(filename)) return + if (!value in CharSequence) value = groovy.json.JsonOutput.toJson(value) + script.writeFile file: filename, text: value + } + } + void readFromDisk(script) { files.each({f -> if (script.fileExists(f.filename)) {