1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

Merge pull request #69 from alejandraferreirovidal/mtaBuildNewConfig

mtaBuild with new config framework
This commit is contained in:
Alejandra Ferreiro Vidal 2018-02-09 16:48:35 +01:00 committed by GitHub
commit 68c666fab7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 34 deletions

View File

@ -17,8 +17,11 @@ steps:
dockerImage: 'maven:3.5-jdk-7'
influxWriteData:
influxServer: 'jenkins'
mtaBuild:
buildTarget: 'NEO'
neoDeploy:
deployMode: 'mta'
warAction: 'deploy'
vmSize: 'lite'
neoCredentialsId: 'CI_CREDENTIALS_ID'

View File

@ -46,4 +46,19 @@ class ConfigurationMerger {
return merged
}
@NonCPS
def static merge(
Map parameters, List parameterKeys,
Map generalConfigurationMap, List generalConfigurationKeys, Map generalConfigurationDefaults,
Map stepConfigurationMap, List stepConfigurationKeys, Map stepConfigurationDefaults=[:]
){
Map merged
Map mergedStepConfiguration = merge(stepConfigurationMap, stepConfigurationKeys, stepConfigurationDefaults)
Map mergedGeneralConfiguration = merge(generalConfigurationMap, generalConfigurationKeys, generalConfigurationDefaults)
merged = merge(mergedGeneralConfiguration, null, mergedStepConfiguration)
merged = merge(parameters, parameterKeys, merged)
return merged
}
}

View File

@ -1,6 +1,6 @@
import hudson.AbortException
import org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.Yaml
import org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.parser.ParserException
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.parser.ParserException
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@ -42,10 +42,6 @@ public class MTABuildTest extends BasePipelineTest {
mtaYaml = new File("$currentDir/mta.yaml")
mtaYaml << defaultMtaYaml()
helper.registerAllowedMethod('readYaml', [Map], {
m ->
return new Yaml().load((m.file as File).text)
})
helper.registerAllowedMethod('pwd', [], { currentDir } )
binding.setVariable('PATH', '/usr/bin')
@ -123,7 +119,7 @@ public class MTABuildTest extends BasePipelineTest {
assert jscr.shell[1].contains(' -jar /mylocation/mta/mta.jar --mtar ')
assert jlr.log.contains('[mtaBuild] MTA JAR "/mylocation/mta/mta.jar" retrieved from parameters.')
assert jlr.log.contains('[mtaBuild] MTA JAR "/mylocation/mta/mta.jar" retrieved from configuration.')
}
@ -161,16 +157,6 @@ public class MTABuildTest extends BasePipelineTest {
}
@Test
void noBuildTargetTest() {
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTarget')
mtaBuildScript.call()
}
@Test
void mtaJarLocationFromEnvironmentTest() {
@ -184,6 +170,50 @@ public class MTABuildTest extends BasePipelineTest {
}
@Test
void mtaJarLocationFromCustomStepConfigurationTest() {
cpe.configuration = [general:[mtaJarLocation: '/general/mta']]
mtaBuildScript.call(script: [commonPipelineEnvironment: cpe],
buildTarget: 'NEO')
assert jscr.shell[1].contains('-jar /general/mta/mta.jar --mtar')
assert jlr.log.contains('[mtaBuild] MTA JAR "/general/mta/mta.jar" retrieved from configuration.')
}
@Test
void buildTargetFromParametersTest() {
mtaBuildScript.call(buildTarget: 'NEO')
assert jscr.shell[1].contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')
}
@Test
void buildTargetFromCustomStepConfigurationTest() {
cpe.configuration = [steps:[mtaBuild:[buildTarget: 'NEO']]]
mtaBuildScript.call(script: [commonPipelineEnvironment: cpe])
assert jscr.shell[1].contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')
}
@Test
void buildTargetFromDefaultStepConfigurationTest() {
cpe.defaultConfiguration = [steps:[mtaBuild:[buildTarget: 'NEO']]]
mtaBuildScript.call(script: [commonPipelineEnvironment: cpe])
assert jscr.shell[1].contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')
}
private defaultMtaYaml() {
return '''
_schema-version: "2.0.0"

View File

@ -29,7 +29,7 @@ class JenkinsReadYamlRule implements TestRule {
if(m.text) {
return new Yaml().load(m.text)
} else if(m.file) {
throw new UnsupportedOperationException()
return new Yaml().load((m.file as File).text)
} else {
throw new IllegalArgumentException("Key 'text' is missing in map ${m}.")
}

View File

@ -1,16 +1,37 @@
import com.sap.piper.Utils
import com.sap.piper.ConfigurationLoader
import com.sap.piper.ConfigurationMerger
def call(Map parameters = [:]) {
handlePipelineStepErrors (stepName: 'mtaBuild', stepParameters: parameters) {
def stepName = 'mtaBuild'
def utils = new Utils()
def buildTarget = utils.getMandatoryParameter(parameters, 'buildTarget', null)
def script = parameters.script
if (script == null){
script = [commonPipelineEnvironment: commonPipelineEnvironment]
}
List parameterKeys = [
'buildTarget',
'mtaJarLocation'
]
List stepConfigurationKeys = [
'buildTarget'
]
List generalConfigurationKeys = [
'mtaJarLocation'
]
handlePipelineStepErrors (stepName: stepName, stepParameters: parameters) {
final script = parameters?.script ?: [commonPipelineEnvironment: commonPipelineEnvironment]
prepareDefaultValues script: script
final Map stepConfiguration = ConfigurationLoader.stepConfiguration(script, stepName)
final Map stepDefaults = ConfigurationLoader.defaultStepConfiguration(script, stepName)
final Map generalConfiguration = ConfigurationLoader.generalConfiguration(script)
final Map configuration = ConfigurationMerger.merge(
parameters, parameterKeys,
generalConfiguration, generalConfigurationKeys, [:],
stepConfiguration, stepConfigurationKeys, stepDefaults)
def mtaYaml = readYaml file: "${pwd()}/mta.yaml"
@ -24,7 +45,8 @@ def call(Map parameters = [:]) {
def mtarFileName = "${id}.mtar"
def mtaJar = getMtaJar(parameters)
def mtaJar = getMtaJar(stepName, configuration)
def buildTarget = configuration.buildTarget
sh """#!/bin/bash
export PATH=./node_modules/.bin:${PATH}
@ -32,27 +54,28 @@ def call(Map parameters = [:]) {
"""
def mtarFilePath = "${pwd()}/${mtarFileName}"
script.commonPipelineEnvironment.setMtarFilePath(mtarFilePath)
script?.commonPipelineEnvironment?.setMtarFilePath(mtarFilePath)
return mtarFilePath
}
}
private getMtaJar(parameters) {
private getMtaJar(stepName, configuration) {
def mtaJarLocation = 'mta.jar' //default, maybe it is in current working directory
if(parameters?.mtaJarLocation){
mtaJarLocation = "${parameters.mtaJarLocation}/mta.jar"
echo "[mtaBuild] MTA JAR \"${mtaJarLocation}\" retrieved from parameters."
if(configuration?.mtaJarLocation){
mtaJarLocation = "${configuration.mtaJarLocation}/mta.jar"
echo "[$stepName] MTA JAR \"${mtaJarLocation}\" retrieved from configuration."
return mtaJarLocation
}
if(env?.MTA_JAR_LOCATION){
mtaJarLocation = "${env.MTA_JAR_LOCATION}/mta.jar"
echo "[mtaBuild] MTA JAR \"${mtaJarLocation}\" retrieved from environment."
echo "[$stepName] MTA JAR \"${mtaJarLocation}\" retrieved from environment."
return mtaJarLocation
}
echo "[mtaBuild] Using MTA JAR from current working directory."
echo "[$stepName] Using MTA JAR from current working directory."
return mtaJarLocation
}