We have been using same JobDSL generation scripts for ages and after we did have some plugin updates to latest ones, JobDSL generation started to fail and not finding how to get it fixed.
Here is example what we are doing:
package foobar.qvantel.dsl.builders;
import javaposse.jobdsl.dsl.Context:
import javaposse.jobdsl.dsl.Item;
import groovy.json.JsonSlurper;
import com.foobar.dsl.builders.PipelineJobBuilder;
import com.foobar.dsl.common.CommonConstants;
class StackstormJobBuilder {
Map mapping = [:];
String resourcePath = 'default'
Item build(Context context) {
def mappingFile = context.readFileFromWorkspace(CommonConstants.RESOURCES_PATH +'stackstorm/' + this.resourcePath + '/mapping.json');
def mappingData = new JsonSlurper().parseText(mappingFile);
def combinedMapping = mappingData + this.mapping
def eventMapping = ' def mapping = [\n'
eventMapping += combinedMapping.collect { key, value ->
def content = " '${key}': [\n"
content += value.collect { source, target ->
def line = " '${source}': ["
if (target instanceof List) {
line += target.collect { "'${it}'" }.join(', ')
} else {
line += "'${target}'"
}
line += ']'
return line
}.join(',\n')
content += '\n ]'
return content
}.join(',\n')
eventMapping += '\n ]'
def pipelineScript = """
try {
${eventMapping}
if (mapping[INSTANCE] && mapping[INSTANCE][JOB]) {
for (job in mapping[INSTANCE][JOB]) {
build job: job, wait: false
}
} else {
unstable "No matching event mapping found."
}
} catch (all) {
echo 'ERROR: ' + all.toString();
currentBuild.result = 'FAILURE';
}
"""
Here is example mapping.json content processed by above snippet:
{
"auto-jenkins": {
"devops_jenkins-config-as-code_pipeline/master": "deploy_jenkins-config-as-code"
}
}
Previously / expected output in generated job config would be something like this:
try {
def mapping = [
'auto-jenkins': [
'devops_jenkins-config-as-code_pipeline/master': ['deploy_jenkins-config-as-code']
]
]
if (mapping[INSTANCE] && mapping[INSTANCE][JOB]) {
for (job in mapping[INSTANCE][JOB]) {
build job: job, wait: false
}
} else {
unstable "No matching event mapping found."
}
} catch (all) {
echo 'ERROR: ' + all.toString();
currentBuild.result = 'FAILURE';
}
But there is failure in JobDSL generation is like this:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ' 'devops_jenkins-config-as-code_pipeline/master': ['deploy_jenkins-config-as-code']' with class 'org.codehaus.groovy.runtime.GStringImpl' to class 'javaposse.jobdsl.dsl.Item'
And it is coming exactly from this line in first snippet above:
And unfortunately these JobDSL stuff are not my core knowledge area (guy who has implemented this stuff not available anymore), so any help is highly appreciated!