import groovy.json.JsonSlurper
file = "$WORKSPACE/$PIPELINE_CONFIG"
println "Reading config file: " + file
contents = new File(file).text
def description = new JsonSlurper().parseText(contents)
accessKey = null
secretKey = null
marathonUsername = null
marathonPassword = null
TEMPLATE_MAP = [
"mvn-build" : 'M-template-common-ci-build',
"mvn-release" : 'M-template-common-decp-maven-release',
"deploy-rtf" : 'M-template-common-rtf-deploy',
"deploy-decp" : 'M-template-common-decp-deploy',
"app-verify" : 'M-template-common-verify',
"config-checker" : 'M-template-common-config-checker',
"resource-checker": 'M-template-common-resource-checker',
"gate" : 'M-template-common-gate',
"rollback" : 'M-template-common-decp-rollback',
"decp-docker-push" : 'M-template-common-decp-china-docker-push'
]
description.jobs.each {
def config = it
config = injectKeysIntoConfig(config)
config = injectBlockOnIntoConfig(config)
if (!TEMPLATE_MAP.containsKey(config.type)) {
throw new Exception("Unknown job type: $config.type")
}
template = TEMPLATE_MAP.get(config.type)
if (config.type.toLowerCase().contains('mvn-')) {
job = MavenJobBuilder(config, template)
} else {
job = JobBuilder(config, template)
}
job.withDownstreamJob()
.withBlockOn()
.withParameters()
.withMaxBuilds()
.withSvnUrl()
.withGitUrl()
.withBuildTrigger()
.withMaven()
.withEnabled()
.withDisabled()
.withEnvVars()
.withAwsKeys()
.withAwsLambdas()
.withMarathonCredentials()
.withCodeDeploy()
.withCucumberTests()
.withPublishBuildInfo()
.build()
}
description.views.each {
def config = it
switch (config.type) {
case "view-build-pipeline":
viewBuildPipeline(config)
break
default:
throw new Exception("Unknown view type: $config.type")
}
}
/*
Configuration setup
*/
def injectKeysIntoConfig(config) {
if (config.injectJobKeys) {
switch (config.environment) {
case "dev":
accessKey = "$jenkins_dev_access_key"
secretKey = "$jenkins_dev_secret_key"
break
case "dev2":
if (binding.variables.containsKey("jenkins_dev2_access_key")) {
accessKey = "$jenkins_dev2_access_key"
secretKey = "$jenkins_dev2_secret_key"
} else {
accessKey = "$jenkins_dev_access_key"
secretKey = "$jenkins_dev_secret_key"
}
break
case "test":
accessKey = "$jenkins_test_access_key"
secretKey = "$jenkins_test_secret_key"
break
case "stage":
accessKey = "$jenkins_stage_access_key"
secretKey = "$jenkins_stage_secret_key"
break
case "cnstage":
accessKey = "$jenkins_cnstage_access_key"
secretKey = "$jenkins_cnstage_secret_key"
break
case "prod":
accessKey = "$jenkins_prod_access_key"
secretKey = "$jenkins_prod_secret_key"
break
case "cnprod":
accessKey = "$jenkins_cnprod_access_key"
secretKey = "$jenkins_cnprod_secret_key"
break
default:
throw new Exception("Unknown environment type: $config.environment")
break
}
config.accessKey = accessKey
config.secretKey = secretKey
config.marathonUsername = marathonUsername
config.marathonPassword = marathonPassword
}
return config
}
def injectBlockOnIntoConfig(config) {
blockOn = config.blockOn
if (blockOn == null) {
config.blockOn = []
}
return config
}
/*
Job creation
*/
def JobBuilder(config, template) {
_job = job(config.name) {
using(template)
}
config._job = _job
return new _JobBuilder(config)
}
def MavenJobBuilder(config, template) {
_job = mavenJob(config.name) {
using(template)
}
config._job = _job
return new _JobBuilder(config)
}
public class _JobBuilder {
def job
def config
def envVars
_JobBuilder(config) {
this.config = config
this.job = config._job
this.envVars = [:]
withEnvVar('app_env', this.config.environment)
}
def build() {
def props = new Properties()
if (this.job.node?.properties[0]?.'EnvInjectJobProperty'[0]) {
def templateVars = this.job.node.properties[0].'EnvInjectJobProperty'[0].info[0].propertiesContent[0].value().get(0)
props.load(new StringReader(templateVars))
}
this.envVars.each { key, value ->
props.setProperty(key, value)
}
this.job.with {
environmentVariables {
envs(props)
}
}
}
def withDownstreamJob() {
this.job.with {
publishers {
if (this.config.nextJob) {
downstream(this.config.nextJob)
} else if (this.config.nextParameterizedJob) {
downstreamParameterized {
trigger(this.config.nextParameterizedJob.name) {
currentBuild() if (this.config.nextParameterizedJob.parameterFile) {
propertiesFile(this.config.nextParameterizedJob.parameterFile, true)
}
if (this.config.nextParameterizedJob.predefinedParameters) {
this.config.nextParameterizedJob.predefinedParameters.each {
key, value ->
predefinedProp(key, value)
}
}
}
}
} else if (this.config.nextManualJob) {
buildPipelineTrigger(this.config.nextManualJob) {
parameters {
currentBuild()
}
}
}
}
}
return this
}
def withParameters() {
this.job.with {
if (this.config.parameters) {
parameters {
this.config.parameters.each { parameter ->
switch (parameter.type) {
case "string":
stringParam(parameter.name, parameter.default ?: null, parameter.description ?: parameter.name)
break
}
}
}
}
}
return this
}
def withMaxBuilds() {
this.job.with {
if (this.config.maxBuilds) {
logRotator(this.config.maxBuilds.days ?: -1, this.config.maxBuilds.number ?: -1,
this.config.maxBuilds.artifactDays ?: -1, this.config.maxBuilds.artifactNumber ?: -1)
} else {
logRotator {
numToKeep(30)
}
}
}
return this
}
def withBlockOn() {
this.job.with {
if (!this.config.blockOn.empty) {
blockOn(this.config.blockOn)
}
}
return this
}
def withGitUrl() {
this.job.with {
if (this.config.git) {
scm {
git(
this.config.git.url,
this.config.git.branch ?: '*/master',
{ node ->
if (this.config.git.pollingExcludedUsers) {
node / 'extensions' / 'hudson.plugins.git.extensions.impl.UserExclusion' {
excludedUsers(this.config.git.pollingExcludedUsers)
}
}
if (this.config.git.localBranch) {
node / 'extensions' / 'hudson.plugins.git.extensions.impl.LocalBranch' {
localBranch(this.config.git.localBranch)
}
}
}
)
}
}
}
return this
}
def withBuildTrigger() {
this.job.with {
if (this.config.withTrigger) {
triggers {
scm("H/5 * * * *")
}
}
}
return this
}
def withMaven() {
this.job.with {
if (this.config.maven) {
if (this.config.maven.version) {
mavenInstallation(this.config.maven.version)
}
if (this.config.maven.pom) {
rootPOM(this.config.maven.pom)
}
if (this.config.maven.goals) {
goals(this.config.maven.goals)
}
if (this.config.maven.opts) {
mavenOpts(this.config.maven.opts)
}
}
}
return this
}
def withDisabled() {
println "disabling job " + this.config.name
this.job.with {
if (config.withDisabled) {
disabled(true)
}
}
return this
}
def withEnabled() {
println "enabling job " + this.config.name
this.job.with {
disabled(false)
}
return this
}
def withEnvVar(name, value) {
this.envVars.put(name, value)
return this
}
def withEnvVars() {
if (this.config.environmentVars) {
this.config.environmentVars.each {
key, value ->
withEnvVar(key, value)
}
}
return this
}
def withEnvInjectPasswordWrapper() {
this.job.with {
if (this.config.withAwsKeys || this.config.withMarathonCredentials) {
configure { project ->
project / buildWrappers / 'EnvInjectPasswordWrapper' {
injectGlobalPasswords false
maskPasswordParameters true
passwordEntries {
}
}
}
}
}
return this
}
def withAwsKeys() {
this.job.with {
if (this.config.withAwsKeys) {
configure { project ->
project / buildWrappers / EnvInjectPasswordWrapper / passwordEntries / 'EnvInjectPasswordEntry' {
name 'AWS_ACCESS_KEY'
value this.config.accessKey
}
project / buildWrappers / EnvInjectPasswordWrapper / passwordEntries << 'EnvInjectPasswordEntry' {
name 'AWS_ACCESS_KEY_ID'
value this.config.accessKey
}
project / buildWrappers / EnvInjectPasswordWrapper / passwordEntries << 'EnvInjectPasswordEntry' {
name 'AWS_SECRET_KEY'
value this.config.secretKey
}
project / buildWrappers / EnvInjectPasswordWrapper / passwordEntries << 'EnvInjectPasswordEntry' {
name 'AWS_SECRET_ACCESS_KEY'
value this.config.secretKey
}
}
}
}
return this
}
def withCodeDeploy() {
this.job.with {
if (this.config.codedeploy) {
this.envVars.put("healthcheckUrl", this.config.codedeploy.healthcheckUrl)
this.envVars.put("runAsUser", this.config.codedeploy.runAsUser)
configure { project ->
project / 'publishers' / 'com.amazonaws.codedeploy.AWSCodeDeployPublisher' {
s3prefix this.config.codedeploy.s3prefix
s3bucket this.config.codedeploy.s3bucket
applicationName this.config.codedeploy.applicationName
deploymentGroupName this.config.codedeploy.deploymentGroupName
deploymentConfig this.config.codedeploy.deploymentConfig
region this.config.codedeploy.region
includes this.config.codedeploy.includes
excludes this.config.codedeploy.excludes
awsAccessKey this.config.accessKey
awsSecretKey this.config.secretKey
pollingTimeoutSec 900
pollingFreqSec 15
waitForCompletion true
proxyHost " "
proxyPort 0
credentials "awsAccessKey"
iamRoleArn " "
}
}
}
}
return this
}
def withCucumberTests() {
this.job.with {
if (config.cucumberTestResults) {
configure {
it / 'publishers' / 'org.jenkinsci.plugins.cucumber.jsontestsupport.CucumberTestResultArchiver'(plugin: 'cucumber-testresult-plugin') {
testResults config.cucumberTestResults
}
it / 'publishers' / 'net.masterthought.jenkins.CucumberReportPublisher'(plugin: 'cucumber-reports') {
jsonReportDirectory ''
pluginUrlPath ''
fileIncludePattern ''
fileExcludePattern ''
skippedFails(false)
undefinedFails(false)
noFlashCharts(false)
ignoreFailedTests(false)
parallelTesting(false)
}
}
}
}
return this
}
def withPublishBuildInfo() {
this.job.with {
if (config.type == "join" && config.publishBuildInfo) {
config.publishBuildInfo.projects.each { project ->
steps {
groovyScriptFile("nexus-scripts/nexus-version-with-timestamp.groovy") {
groovyInstallation(config.publishBuildInfo.groovyVersion)
prop("nexus_project_path", project.nexusProjectPath)
prop("build_version_var_name", project.projectName + "_version")
prop("build_timestamp_var_name", project.projectName + "_timestamp")
prop("build_properties_file_name", config.publishBuildInfo.propertiesFileName)
}
}
}
}
}
return this
}
def withAwsLambdas() {
this.job.with {
if (config.awsLambdas) {
configure { project ->
def lambdaDefaults = config.awsLambdas.defaults
config.awsLambdas.functions.each { function ->
project / 'builders' << 'com.xti.jenkins.plugin.awslambda.upload.LambdaUploadBuildStep' {
delegate.lambdaUploadBuildStepVariables {
delegate.useInstanceCredentials function.useInstanceCredentials ?: lambdaDefaults.useInstanceCredentials ?: false
delegate.awsAccessKeyId(this.config.accessKey)
delegate.awsSecretKey(new hudson.util.Secret(this.config.secretKey).getEncryptedValue())
delegate.awsRegion(function.awsRegion ?: lambdaDefaults.awsRegion ?: '')
delegate.artifactLocation(function.artifactLocation ?: lambdaDefaults.artifactLocation ?: '')
delegate.description(function.description ?: '')
delegate.functionName(function.functionName ?: '')
delegate.handler(function.handler ?: '')
delegate.memorySize(function.memorySize ?: lambdaDefaults.memorySize ?: '')
delegate.role(function.role ?: lambdaDefaults.role ?: '')
delegate.runtime(function.runtime ?: lambdaDefaults.runtime ?: '')
delegate.timeout(function.timeout ?: lambdaDefaults.timeout ?: '')
delegate.updateMode(function.updateMode ?: lambdaDefaults.updateMode ?: '')
delegate.publish(function.publish ?: lambdaDefaults.publish ?: false)
delegate.alias(function.alias ?: '')
delegate.createAlias(function.createAlias ?: false)
delegate.subnets(function.subnets ?: lambdaDefaults.subnets ?: '')
delegate.securityGroups(function.securityGroups ?: lambdaDefaults.securityGroups ?: '')
}
}
}
}
}
}
return this
}
def withMarathonCredentials() {
this.job.with {
if (config.withMarathonCredentials) {
configure { project ->
project / buildWrappers / EnvInjectPasswordWrapper / passwordEntries / 'EnvInjectPasswordEntry' {
name 'JENKINS_MARATHON_USERNAME'
value this.config.marathonUsername
}
project / buildWrappers / EnvInjectPasswordWrapper / passwordEntries << 'EnvInjectPasswordEntry' {
name 'JENKINS_MARATHON_PASSWORD'
value this.config.marathonPassword
}
}
}
}
return this
}
}
/*
Specific View types
*/
def viewBuildPipeline(config) {
buildPipelineView(config.name) {
selectedJob(config.firstJob)
alwaysAllowManualTrigger()
showPipelineParameters()
showPipelineParametersInHeaders()
displayedBuilds(config.displayedJobs)
showPipelineDefinitionHeader()
}
}
Please include a script that reproduces the problem.