From fc5fd06ed657829d71ef8ef24726319095b44ea6 Mon Sep 17 00:00:00 2001 From: Hilario Fernandes Date: Mon, 21 Jun 2021 16:48:12 +0100 Subject: [PATCH] [CLEANUP] Fix resolveTemplate not assuming ConfigurationMap as a param. Moved to Groovy SimpleTemplateEngine. --- .../minion-multibranch-pipeline-default.vm | 15 ++++---- .../templates/minion-pipeline-default.vm | 25 +++++++------ .../ci/jenkins/MinionHandler.groovy | 37 ++++++++++--------- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/resources/templates/minion-multibranch-pipeline-default.vm b/resources/templates/minion-multibranch-pipeline-default.vm index e55bc1d..1124220 100644 --- a/resources/templates/minion-multibranch-pipeline-default.vm +++ b/resources/templates/minion-multibranch-pipeline-default.vm @@ -1,11 +1,12 @@ -#foreach ( $library in $libraries ) -library(identifier: '$library', changelog: false) -#end +<% +libraries.each { library -> + println "library(identifier: '$library', changelog: false)" +} -#set( $runCheckout = $properties.getBool('RUN_STAGE_CHECKOUT') ) -#set( $runBuild = $properties.getBool('RUN_STAGE_BUILD') ) -#set( $runSonar = $properties.getBool('RUN_STAGE_SONAR') && $item.auditable ) -## +runCheckout = properties.getBool('RUN_STAGE_CHECKOUT') +runBuild = properties.getBool('RUN_STAGE_BUILD') +runSonar = properties.getBool('RUN_STAGE_SONAR') && item.auditable +%> Map defaultParams = [ SLAVE_NODE_LABEL : '$properties.getString('SLAVE_NODE_LABEL')', WORKSPACE : '$properties.getString('WORKSPACE')', diff --git a/resources/templates/minion-pipeline-default.vm b/resources/templates/minion-pipeline-default.vm index 0cef53b..aa778c6 100644 --- a/resources/templates/minion-pipeline-default.vm +++ b/resources/templates/minion-pipeline-default.vm @@ -1,17 +1,18 @@ -#foreach ( $library in $libraries ) -library(identifier: '$library', changelog: false) -#end +<% +libraries.each { library -> + println "library(identifier: '$library', changelog: false)" +} -#set( $runCheckout = $properties.getBool('RUN_STAGE_CHECKOUT') ) -#set( $runBuild = $properties.getBool('RUN_STAGE_BUILD') ) -#set( $runTest = $properties.getBool('RUN_STAGE_TEST') && $item.testable ) -#set( $runArchive = $properties.getBool('RUN_STAGE_ARCHIVE') && $item.archivable ) -## +runCheckout = properties.getBool('RUN_STAGE_CHECKOUT') +runBuild = properties.getBool('RUN_STAGE_BUILD') +runTest = properties.getBool('RUN_STAGE_TEST') && item.testable +runArchive = properties.getBool('RUN_STAGE_ARCHIVE') && item.archivable +%> Map defaultParams = [ - SLAVE_NODE_LABEL : '$properties.getString('SLAVE_NODE_LABEL')', - WORKSPACE : '$properties.getString('WORKSPACE')', - BUILD_DATA_ROOT_PATH: '$properties.getString('BUILD_DATA_ROOT_PATH')', - BUILD_DATA_FILE : '$properties.getString('BUILD_DATA_FILE')', + SLAVE_NODE_LABEL : '$properties.SLAVE_NODE_LABEL', + WORKSPACE : '$properties.WORKSPACE', + BUILD_DATA_ROOT_PATH: '$properties.BUILD_DATA_ROOT_PATH', + BUILD_DATA_FILE : '$properties.BUILD_DATA_FILE', RUN_STAGE_CHECKOUT : $runCheckout, RUN_STAGE_BUILD : $runBuild, diff --git a/src/org/hitachivantara/ci/jenkins/MinionHandler.groovy b/src/org/hitachivantara/ci/jenkins/MinionHandler.groovy index b745f72..bc6827e 100644 --- a/src/org/hitachivantara/ci/jenkins/MinionHandler.groovy +++ b/src/org/hitachivantara/ci/jenkins/MinionHandler.groovy @@ -12,6 +12,8 @@ import org.hitachivantara.ci.config.BuildData import org.hitachivantara.ci.config.ConfigurationException import org.hitachivantara.ci.config.ConfigurationMap import org.jenkinsci.plugins.workflow.cps.CpsScript +import groovy.text.SimpleTemplateEngine +import groovy.text.TemplateEngine import java.nio.file.Paths @@ -89,11 +91,11 @@ class MinionHandler { String rootFolderPath = getRootFolderPath() // use the defined template or the default ones - Map templateSource + String templateSource if(buildData.isSet(MINION_PIPELINE_TEMPLATE)){ - templateSource = [file: buildData.getString(MINION_PIPELINE_TEMPLATE)] + templateSource = steps.readFile(file: buildData.getString(MINION_PIPELINE_TEMPLATE), encoding: 'UTF-8') } else { - templateSource = [text: getDefaultPipelineTemplate()] + templateSource = getDefaultPipelineTemplate() } // add all the current build libraries @@ -101,22 +103,23 @@ class MinionHandler { workJobItems.each { JobItem item -> steps.log.debug "Creating minion job for ${item.jobID}" - - Map extraProps = [(BUILD_DATA_ROOT_PATH): getBuildDataPath(), - (BUILD_DATA_FILE) : getBuildDataFilename(item)] - - String script = steps.resolveTemplate(templateSource + [ - parameters: [ - libraries : libraries, - properties : buildData.buildProperties + extraProps, - item : item.export() - ] - ]) + + Map parameters = [ + libraries : libraries, + properties: new ConfigurationMap(buildData.buildProperties, [ + (BUILD_DATA_ROOT_PATH): getBuildDataPath(), + (BUILD_DATA_FILE) : getBuildDataFilename(item), + ]), + item: item.export() + ] + + TemplateEngine engine = new SimpleTemplateEngine() + Writable template = engine.createTemplate(templateSource).make(parameters) Map jobConfig = [ name: getJobName(item), folder: rootFolderPath, - script: script + script: template.toString() ] if (buildData.getBool(USE_MINION_MULTIBRANCH_JOBS)) { @@ -135,8 +138,8 @@ class MinionHandler { prMerge : item.prMerge ] ] - } - + } + steps.createJob(jobConfig) }