2016-12-09 13:53:29 +00:00
|
|
|
|
import org.yaml.snakeyaml.Yaml
|
2016-12-09 13:43:49 +00:00
|
|
|
|
|
2016-12-07 23:28:53 +00:00
|
|
|
|
def getOcCmd() {
|
|
|
|
|
return "oc --token=`cat /var/run/secrets/kubernetes.io/serviceaccount/token` --server=https://openshift.default.svc.cluster.local --certificate-authority=/run/secrets/kubernetes.io/serviceaccount/ca.crt"
|
|
|
|
|
}
|
2016-12-06 09:10:10 +00:00
|
|
|
|
|
2016-12-15 16:35:06 +00:00
|
|
|
|
def getReplicas(namespace, name) {
|
2016-12-07 23:28:53 +00:00
|
|
|
|
def ocCmd = getOcCmd()
|
2016-12-15 16:35:06 +00:00
|
|
|
|
return sh(script: "${ocCmd} get dc ${name} --template='{{ .spec.replicas }}' -n ${namespace} || true", returnStdout: true).trim()
|
2016-12-07 23:26:40 +00:00
|
|
|
|
}
|
2016-12-07 23:24:05 +00:00
|
|
|
|
|
2016-12-09 14:18:30 +00:00
|
|
|
|
@NonCPS
|
2016-12-09 15:46:44 +00:00
|
|
|
|
def parseYaml(content) {
|
|
|
|
|
return new Yaml().load(content)
|
2016-12-09 14:17:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-15 16:35:06 +00:00
|
|
|
|
def ocTemplateParametersAsCommandLineOpt(parameters) {
|
|
|
|
|
return parameters.collect { v -> "-v ${v}" }.join(" ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def ocApplyTemplate(namespace, config) {
|
|
|
|
|
ocApplyTemplate(namespace, config.manifest, config.parameters)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def ocApplyTemplate(namespace, manifest, parameters) {
|
|
|
|
|
def ocCmd = getOcCmd()
|
|
|
|
|
def parametersOpt = ocTemplateParametersAsCommandLineOpt(parameters)
|
|
|
|
|
sh "${ocCmd} process -f ${manifest} ${parametersOpt} -n ${namespace} | ${ocCmd} apply -f - -n ${namespace}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def ocDelete(namespace, target) {
|
|
|
|
|
def ocCmd = getOcCmd()
|
|
|
|
|
sh "${ocCmd} delete ${target.type}/${target.name} -n ${namespace}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def ocBuild(namespace, name, config) {
|
2016-12-07 23:28:53 +00:00
|
|
|
|
def ocCmd = getOcCmd()
|
|
|
|
|
|
2016-12-15 16:35:06 +00:00
|
|
|
|
config.delete.each { target -> ocDelete(namespace, target) }
|
|
|
|
|
|
|
|
|
|
config.templates.each { template -> ocApplyTemplate(namespace, template) }
|
2016-12-06 12:39:45 +00:00
|
|
|
|
|
2016-12-15 16:35:06 +00:00
|
|
|
|
sh "${ocCmd} start-build ${name} -w -n ${namespace}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def ocTag(isNamespace, isName, sourceTag, targetTag) {
|
|
|
|
|
sh "${ocCmd} tag ${isNamespace}/${isName}:${sourceTag} ${isNamespace}/${isName}:${targetTag}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def ocDeploy(namespace, name, config) {
|
|
|
|
|
def replicas = getReplicas(namespace, name)
|
|
|
|
|
|
|
|
|
|
config.delete.each { target -> ocDelete(namespace, target) }
|
|
|
|
|
|
|
|
|
|
config.templates.each { template ->
|
|
|
|
|
def manifest = template.manifest
|
|
|
|
|
def parameters = template.parameters.clone()
|
|
|
|
|
if (replicas) {
|
|
|
|
|
parameters["REPLICAS"] = replicas
|
|
|
|
|
}
|
|
|
|
|
ocApplyTemplate(namespace, manifest, parameters)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sh "${ocCmd} rollout latest dc/${name} -n ${namespace}"
|
|
|
|
|
sh "${ocCmd} rollout status dc/${name} -n ${namespace}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node() {
|
2016-12-09 15:48:52 +00:00
|
|
|
|
stage("Checkout") {
|
2016-12-07 22:21:17 +00:00
|
|
|
|
git "https://github.com/omallo/ruby-ex.git"
|
2016-12-09 15:48:52 +00:00
|
|
|
|
}
|
2016-12-09 15:41:36 +00:00
|
|
|
|
|
2016-12-09 15:48:52 +00:00
|
|
|
|
def config = parseYaml(readFile("deployment/config.yaml"))
|
2016-12-09 14:55:28 +00:00
|
|
|
|
|
2016-12-09 15:48:52 +00:00
|
|
|
|
stage("Build") {
|
2016-12-15 16:35:06 +00:00
|
|
|
|
ocBuild(namespace: "rubex-dev", name: "frontend", config: config.dev.build.frontend)
|
2016-12-05 14:32:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 15:48:52 +00:00
|
|
|
|
stage("Deploy to DEV") {
|
2016-12-15 16:35:06 +00:00
|
|
|
|
ocTag(isNamespace: "rubex-dev", isName: "frontend", sourceTag: "latest", targetTag: "dev")
|
|
|
|
|
ocDeploy(namespace: "rubex-dev", name: "frontend", config: config.dev.deployment.frontend)
|
2016-12-05 14:32:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def isPromoteToTest = false
|
2016-12-09 13:33:03 +00:00
|
|
|
|
stage("Promote to TEST?") {
|
|
|
|
|
isPromoteToTest = input(message: "Promotion", parameters: [booleanParam(defaultValue: false, name: "Promote to TEST?")])
|
2016-12-05 14:32:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isPromoteToTest) {
|
2016-12-09 13:33:03 +00:00
|
|
|
|
stage("Deploy to TEST") {
|
2016-12-15 16:35:06 +00:00
|
|
|
|
ocTag(isNamespace: "rubex-dev", isName: "frontend", sourceTag: "dev", targetTag: "test")
|
|
|
|
|
ocDeploy(namespace: "rubex-test", name: "frontend", config: config.test.deployment.frontend)
|
2016-12-05 14:32:22 +00:00
|
|
|
|
}
|
2016-12-09 15:48:52 +00:00
|
|
|
|
}
|
2016-12-05 14:32:22 +00:00
|
|
|
|
}
|