mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-03-03 15:02:35 +02:00
Merge pull request #122 from SAP/CCFenner/config
change configuration loading
This commit is contained in:
commit
815f5c358e
@ -1,8 +1,42 @@
|
||||
package com.sap.piper
|
||||
|
||||
class ConfigurationHelper implements Serializable {
|
||||
static def loadStepDefaults(Script step){
|
||||
return new ConfigurationHelper(step)
|
||||
.initDefaults(step)
|
||||
.loadDefaults()
|
||||
}
|
||||
|
||||
private final Map config
|
||||
private Map config
|
||||
private String name
|
||||
|
||||
ConfigurationHelper(Script step){
|
||||
name = step.STEP_NAME
|
||||
if(!name) throw new IllegalArgumentException('Step has no public name property!')
|
||||
}
|
||||
|
||||
private final ConfigurationHelper initDefaults(Script step){
|
||||
step.prepareDefaultValues()
|
||||
return this
|
||||
}
|
||||
|
||||
private final ConfigurationHelper loadDefaults(){
|
||||
config = ConfigurationLoader.defaultStepConfiguration(null, name)
|
||||
return this
|
||||
}
|
||||
|
||||
ConfigurationHelper mixinStepConfig(commonPipelineEnvironment, Set filter = null){
|
||||
if(!name) throw new IllegalArgumentException('Step has no public name property!')
|
||||
Map stepConfiguration = ConfigurationLoader.stepConfiguration([commonPipelineEnvironment: commonPipelineEnvironment], name)
|
||||
return mixin(stepConfiguration, filter)
|
||||
}
|
||||
|
||||
ConfigurationHelper mixin(Map parameters, Set filter = null){
|
||||
config = ConfigurationMerger.merge(parameters, filter, config)
|
||||
return this
|
||||
}
|
||||
|
||||
Map use(){ return config }
|
||||
|
||||
ConfigurationHelper(Map config = [:]){
|
||||
this.config = config
|
||||
|
@ -4,6 +4,8 @@ import groovy.test.GroovyAssert
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
|
||||
import static org.hamcrest.Matchers.*
|
||||
|
||||
class ConfigurationHelperTest {
|
||||
|
||||
private static getConfiguration() {
|
||||
@ -42,4 +44,32 @@ class ConfigurationHelperTest {
|
||||
|
||||
GroovyAssert.shouldFail { configuration.getMandatoryProperty('something') }
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConfigurationLoaderWithDefaults() {
|
||||
Map config = new ConfigurationHelper([property1: '27']).use()
|
||||
// asserts
|
||||
Assert.assertThat(config, hasEntry('property1', '27'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConfigurationLoaderWithCustomSettings() {
|
||||
Map config = new ConfigurationHelper([property1: '27'])
|
||||
.mixin([property1: '41'])
|
||||
.use()
|
||||
// asserts
|
||||
Assert.assertThat(config, hasEntry('property1', '41'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConfigurationLoaderWithFilteredCustomSettings() {
|
||||
Set filter = ['property2']
|
||||
Map config = new ConfigurationHelper([property1: '27'])
|
||||
.mixin([property1: '41', property2: '28', property3: '29'], filter)
|
||||
.use()
|
||||
// asserts
|
||||
Assert.assertThat(config, hasEntry('property1', '27'))
|
||||
Assert.assertThat(config, hasEntry('property2', '28'))
|
||||
Assert.assertThat(config, not(hasKey('property3')))
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,32 @@
|
||||
import com.sap.piper.ConfigurationMerger
|
||||
import com.sap.piper.ConfigurationHelper
|
||||
import com.sap.piper.GitUtils
|
||||
import com.sap.piper.Utils
|
||||
import com.sap.piper.versioning.ArtifactVersioning
|
||||
|
||||
import groovy.transform.Field
|
||||
import groovy.text.SimpleTemplateEngine
|
||||
|
||||
@Field String STEP_NAME = 'artifactSetVersion'
|
||||
@Field Set STEP_CONFIG_KEYS = [
|
||||
'artifactType',
|
||||
'buildTool',
|
||||
'commitVersion',
|
||||
'dockerVersionSource',
|
||||
'filePath',
|
||||
'gitCredentialsId',
|
||||
'gitUserEMail',
|
||||
'gitUserName',
|
||||
'gitSshUrl',
|
||||
'tagPrefix',
|
||||
'timestamp',
|
||||
'timestampTemplate',
|
||||
'versioningTemplate'
|
||||
]
|
||||
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.plus('gitCommitId')
|
||||
|
||||
def call(Map parameters = [:]) {
|
||||
|
||||
def stepName = 'artifactSetVersion'
|
||||
|
||||
handlePipelineStepErrors (stepName: stepName, stepParameters: parameters) {
|
||||
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
|
||||
|
||||
def gitUtils = parameters.juStabGitUtils
|
||||
if (gitUtils == null) {
|
||||
@ -17,51 +35,20 @@ def call(Map parameters = [:]) {
|
||||
|
||||
if (fileExists('.git')) {
|
||||
if (sh(returnStatus: true, script: 'git diff --quiet HEAD') != 0)
|
||||
error "[${stepName}] Files in the workspace have been changed previously - aborting ${stepName}"
|
||||
error "[${STEP_NAME}] Files in the workspace have been changed previously - aborting ${STEP_NAME}"
|
||||
}
|
||||
|
||||
def script = parameters.script
|
||||
if (script == null)
|
||||
script = [commonPipelineEnvironment: commonPipelineEnvironment]
|
||||
|
||||
prepareDefaultValues script: script
|
||||
|
||||
Set parameterKeys = [
|
||||
'artifactType',
|
||||
'buildTool',
|
||||
'commitVersion',
|
||||
'dockerVersionSource',
|
||||
'filePath',
|
||||
'gitCommitId',
|
||||
'gitCredentialsId',
|
||||
'gitUserEMail',
|
||||
'gitUserName',
|
||||
'gitSshUrl',
|
||||
'tagPrefix',
|
||||
'timestamp',
|
||||
'timestampTemplate',
|
||||
'versioningTemplate'
|
||||
]
|
||||
Map pipelineDataMap = [
|
||||
gitCommitId: gitUtils.getGitCommitIdOrNull()
|
||||
]
|
||||
Set stepConfigurationKeys = [
|
||||
'artifactType',
|
||||
'buildTool',
|
||||
'commitVersion',
|
||||
'dockerVersionSource',
|
||||
'filePath',
|
||||
'gitCredentialsId',
|
||||
'gitUserEMail',
|
||||
'gitUserName',
|
||||
'gitSshUrl',
|
||||
'tagPrefix',
|
||||
'timestamp',
|
||||
'timestampTemplate',
|
||||
'versioningTemplate'
|
||||
]
|
||||
|
||||
Map configuration = ConfigurationMerger.merge(script, stepName, parameters, parameterKeys, pipelineDataMap, stepConfigurationKeys)
|
||||
// load default & individual configuration
|
||||
Map configuration = ConfigurationHelper
|
||||
.loadStepDefaults(this)
|
||||
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
|
||||
.mixin(gitCommitId: gitUtils.getGitCommitIdOrNull())
|
||||
.mixin(parameters, PARAMETER_KEYS)
|
||||
.use()
|
||||
|
||||
def utils = new Utils()
|
||||
def buildTool = utils.getMandatoryParameter(configuration, 'buildTool')
|
||||
@ -77,7 +64,7 @@ def call(Map parameters = [:]) {
|
||||
//replace + sign if available since + is not allowed in a Docker tag
|
||||
newVersion = script.commonPipelineEnvironment.getArtifactVersion().replace('+', '_')
|
||||
else
|
||||
error ("[${stepName}] No artifact version available for 'dockerVersionSource: appVersion' -> executeBuild needs to run for the application artifact first to set the artifactVersion for the application artifact.'")
|
||||
error ("[${STEP_NAME}] No artifact version available for 'dockerVersionSource: appVersion' -> executeBuild needs to run for the application artifact first to set the artifactVersion for the application artifact.'")
|
||||
} else {
|
||||
def currentVersion = artifactVersioning.getVersion()
|
||||
|
||||
@ -106,7 +93,7 @@ def call(Map parameters = [:]) {
|
||||
try {
|
||||
sh "git ${gitUserMailConfig} commit -m 'update version ${newVersion}'"
|
||||
} catch (e) {
|
||||
error "[${stepName}]git commit failed: ${e}"
|
||||
error "[${STEP_NAME}]git commit failed: ${e}"
|
||||
}
|
||||
sh "git remote set-url origin ${configuration.gitSshUrl}"
|
||||
sh "git tag ${configuration.tagPrefix}${newVersion}"
|
||||
@ -125,7 +112,7 @@ def call(Map parameters = [:]) {
|
||||
script.commonPipelineEnvironment.setGitCommitId(gitCommitId)
|
||||
}
|
||||
|
||||
echo "[${stepName}]New version: ${newVersion}"
|
||||
echo "[${STEP_NAME}]New version: ${newVersion}"
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user