Skip to content

Commit

Permalink
KOGITO-8577 Fail or Ignore on missing environment (apache#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
radtriste authored Feb 2, 2023
1 parent 92811cc commit 93a0e33
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
7 changes: 7 additions & 0 deletions docs/jenkins.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ The branch config file is given to the seed job via the configuration into the m
All values from this config file will be available to job generation as env variables.
```yaml
# Specific generation config behaviors
generation_config:
missing_environment: fail # Accept `fail` (will fail on any environment missing) or `ignore` (will not generate the job on missing environment). Other values will be considered as "continue"

# Allow to disable job types
# This is useful in testing to avoid to generate all jobs
# Current jobtype can be found in ../dsl/seed/src/main/groovy/org/kie/jenkins/jobdsl/model/JobType.groovy
Expand Down Expand Up @@ -199,6 +203,9 @@ cloud:
latest_git_branch: main
jenkins:
email_creds_id: KOGITO_CI_EMAIL_TO_PERSO
default_tools:
jdk: kie-jdk11
maven: kie-maven-3.8.7

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.kie.jenkins.jobdsl.model.Folder
import org.kie.jenkins.jobdsl.model.JenkinsFolder
import org.kie.jenkins.jobdsl.model.JenkinsFolderRegistry
import org.kie.jenkins.jobdsl.model.JobType
import org.kie.jenkins.jobdsl.utils.EnvUtils
import org.kie.jenkins.jobdsl.utils.JobParamsUtils
import org.kie.jenkins.jobdsl.utils.PrintUtils
import org.kie.jenkins.jobdsl.utils.RegexUtils
Expand Down Expand Up @@ -43,14 +44,26 @@ class KogitoJobTemplate {
static def createPipelineJob(def script, Map jobParams = [:]) {
String jobFolderName = ''
Map jobFolderEnv = [:]
if (jobParams.job.folder) {
if (![Folder, JenkinsFolder].any { it.isAssignableFrom(jobParams.job.folder.getClass()) }) {
def jobFolder = jobParams.job.folder
if (jobFolder) {
if (![Folder, JenkinsFolder].any { it.isAssignableFrom(jobFolder.getClass()) }) {
throw new RuntimeException('Folder is not of type org.kie.jenkins.jobdsl.model.JenkinsFolder')
}

// Expect org.kie.jenkins.jobdsl.model.Folder structure
jobFolderName = jobParams.job.folder.getName()
jobFolderEnv = jobParams.job.folder.getDefaultEnvVars(script)
if (!jobParams.job.folder.isActive(script)) {
jobFolderName = jobFolder.getName()
jobFolderEnv = jobFolder.getDefaultEnvVars(script)

if (!EnvUtils.isEnvironmentDefined(script, jobFolder.getEnvironmentName())) {
String output = "Cannot create job name ${jobParams.job.name} in jobFolder ${jobFolderName} as environment ${jobFolder.getEnvironmentName()} is NOT configured"
if (Utils.isGenerationFailOnMissingEnvironment(script)) {
throw new RuntimeException(output)
} else if (Utils.isGenerationIgnoreOnMissingEnvironment(script)) {
// Do no create the job if the folder is not active
PrintUtils.warn(script, output)
return
}
} else if (!jobFolder.isActive(script)) {
// Do no create the job if the folder is not active
PrintUtils.warn(script, "Cannot create job name ${jobParams.job.name} in jobFolder ${jobFolderName} as folder is NOT active")
return
Expand Down Expand Up @@ -137,7 +150,7 @@ class KogitoJobTemplate {
}
}
}
}
}
}

/**
Expand Down
24 changes: 24 additions & 0 deletions dsl/seed/src/main/groovy/org/kie/jenkins/jobdsl/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ class Utils {
return getBindingValue(script, envVar).toBoolean()
}

static List getBindingValuesStartingWith(def script, String keyPrefix) {
return script.getBinding()
.getVariables()
.keySet()
.findAll { key -> key.startsWith(keyPrefix) }
.collect { it } // Transform to list
}

static boolean hasBindingValuesStartingWith(def script, String keyPrefix) {
return getBindingValuesStartingWith(script, keyPrefix).size() > 0
}

////////////////////////////////////////////////////////////////////////////////////////////
// *DEPRECATED* section
// Should be deleted once https://issues.redhat.com/browse/PLANNER-2870 is implemented
Expand Down Expand Up @@ -263,6 +275,18 @@ class Utils {
return getBindingValue(script, 'OLD_FOLDER_STRUCTURE')?.toBoolean()
}

static String getGenerationMissingEnvironment(def script) {
return getBindingValue(script, 'GENERATION_CONFIG_MISSING_ENVIRONMENT')
}

static boolean isGenerationIgnoreOnMissingEnvironment(def script) {
return getGenerationMissingEnvironment(script) == 'ignore'
}

static boolean isGenerationFailOnMissingEnvironment(def script) {
return getGenerationMissingEnvironment(script) == 'fail'
}

/**
* Return the given string with all words beginning with first letter as upper case
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class EnvUtils {
return value != '' ? value.toBoolean() : true
}

static boolean isEnvironmentDefined(def script, String envName) {
if (!envName) {
return true
}
boolean value = Utils.hasBindingValuesStartingWith(script, createEnvironmentsKeyPrefix(envName))
PrintUtils.debug(script, "isEnvironmentDefined ${envName} => ${value}")
return value
}

static List<String> getAllEnabledEnvironments(def script) {
List<String> enabledEnvironments = getAllEnvironments(script).findAll { isEnvironmentEnabled(script, it) }
PrintUtils.debug(script, "getAllEnabledEnvironments => ${enabledEnvironments}")
Expand All @@ -43,10 +52,7 @@ class EnvUtils {
static Map getEnvironmentEnvVars(def script, String envName) {
String keyPrefix = "${createEnvironmentsKeyPrefix(envName)}ENV_VARS_"
PrintUtils.debug(script, "Looking for env vars for env ${envName}. Key prefix is ${keyPrefix}")
Map envVars = script.getBinding()
.getVariables()
.keySet()
.findAll { it.startsWith(keyPrefix) }
Map envVars = Utils.getBindingValuesStartingWith(script, keyPrefix)
.collectEntries {
["${it.replace(keyPrefix, '')}", Utils.getBindingValue(script, it)]
}
Expand All @@ -57,7 +63,7 @@ class EnvUtils {
static String getEnvironmentEnvVar(def script, String envName, String envKey) {
// Using find method as the keys are stored as gstring
// and it creates problem when searching for a gstring/string key with `map.get(key)` ...
return getEnvironmentEnvVars(script, envName).find { it.key == envKey }?.value
return getEnvironmentEnvVars(script, envName).find { it.key == envKey }?.value
}

static List<String> getEnvironmentIds(def script, String envName) {
Expand Down

0 comments on commit 93a0e33

Please sign in to comment.