1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

pipeline initialization - support nested keys (#695)

* pipeline initialization - support nested keys

support a nested structure for config keys for initialization conditions, like

```
Acceptance:
    stepConditions:
      cloudFoundryDeploy:
        configKeys:
          - 'cfSpace'
          - 'cloudFoundry/space'
```

* optimize code
This commit is contained in:
Oliver Nocon 2019-05-08 12:04:47 +02:00 committed by GitHub
parent a67f850fdb
commit e20eb3c320
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 5 deletions

View File

@ -451,4 +451,27 @@ steps: {}
assertThat(nullScript.commonPipelineEnvironment.configuration.runStage.Acceptance, is(true))
}
@Test
void testGetConfigValue() {
def config = [
invalidKey: 'invalidValue',
stringKey: 'stringValue',
listKey: [
'listValue1',
'listValue2'
],
nested: [
key: 'nestedValue'
]
]
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'stringKey'), is('stringValue'))
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'listKey'), is(['listValue1','listValue2']))
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'nested/key'), is('nestedValue'))
assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'invalidKey/key'), is(nullValue()))
//assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'nested/key'), is('nestedValue'))
}
}

View File

@ -60,31 +60,32 @@ void call(Map parameters = [:]) {
stage.getValue().stepConditions.each {step ->
def stepActive = false
step.getValue().each {condition ->
Map stepConfig = script.commonPipelineEnvironment.getStepConfiguration(step.getKey(), currentStage)
switch(condition.getKey()) {
case 'config':
if (condition.getValue() instanceof Map) {
condition.getValue().each {configCondition ->
if (script.commonPipelineEnvironment.getStepConfiguration(step.getKey(), currentStage)?.get(configCondition.getKey()) in configCondition.getValue()) {
if (getConfigValue(stepConfig, configCondition.getKey()) in configCondition.getValue()) {
stepActive = true
}
}
} else if (script.commonPipelineEnvironment.getStepConfiguration(step.getKey(), currentStage)?.get(condition.getValue())) {
} else if (getConfigValue(stepConfig, condition.getValue())) {
stepActive = true
}
break
case 'configKeys':
if (condition.getValue() instanceof List) {
condition.getValue().each {configKey ->
if (script.commonPipelineEnvironment.getStepConfiguration(step.getKey(), currentStage)?.get(configKey)) {
if (getConfigValue(stepConfig, configKey)) {
stepActive = true
}
}
} else if (script.commonPipelineEnvironment.getStepConfiguration(step.getKey(), currentStage)?.get(condition.getValue())) {
} else if (getConfigValue(stepConfig, condition.getValue())) {
stepActive = true
}
break
case 'filePatternFromConfig':
def conditionValue=script.commonPipelineEnvironment.getStepConfiguration(step.getKey(), currentStage)?.get(condition.getValue())
def conditionValue = getConfigValue(stepConfig, condition.getValue())
if (conditionValue && findFiles(glob: conditionValue)) {
stepActive = true
}
@ -109,3 +110,16 @@ void call(Map parameters = [:]) {
echo "[${STEP_NAME}] Debug - Run Step Configuration: ${script.commonPipelineEnvironment.configuration.runStep}"
}
}
private def getConfigValue(Map stepConfig, def configKey) {
if (stepConfig == null) return null
List configPath = configKey instanceof String ? configKey.tokenize('/') : configKey
def configValue = stepConfig[configPath.head()]
if (configPath.size() == 1) return configValue
if (configValue in Map) return getConfigValue(configValue, configPath.tail())
return null
}