Enhanced the config to drive the whole deployment

This commit is contained in:
Ovidio Mallo 2016-12-15 17:35:06 +01:00
parent 914354169e
commit ec68bfe46f
2 changed files with 77 additions and 22 deletions

View File

@ -1,6 +1,23 @@
dev:
params:
REPLICAS: 1
build:
frontend:
templates:
- manifest: manifests/build.yaml
deployment:
frontend:
delete:
- type: pvc
name: frontend
templates:
- manifest: manifests/app.yaml
parameters:
ENV: test
REPLICAS: 1
test:
params:
REPLICAS: 2
deployment:
frontend:
templates:
- manifest: manifests/app.yaml
parameters:
ENV: test
REPLICAS: 2

View File

@ -4,10 +4,9 @@ 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"
}
def getReplicasOrDefault(deploymentConfig, project, defaultReplicas) {
def getReplicas(namespace, name) {
def ocCmd = getOcCmd()
def replicas = sh(script: "${ocCmd} get dc ${deploymentConfig} --template='{{ .spec.replicas }}' -n ${project} || true", returnStdout: true).trim()
return replicas ?: defaultReplicas
return sh(script: "${ocCmd} get dc ${name} --template='{{ .spec.replicas }}' -n ${namespace} || true", returnStdout: true).trim()
}
@NonCPS
@ -15,12 +14,58 @@ def parseYaml(content) {
return new Yaml().load(content)
}
node() {
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) {
def ocCmd = getOcCmd()
def buildManifest = "deployment/manifests/build.yaml"
def appManifest = "deployment/manifests/app.yaml"
config.delete.each { target -> ocDelete(namespace, target) }
config.templates.each { template -> ocApplyTemplate(namespace, template) }
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() {
stage("Checkout") {
git "https://github.com/omallo/ruby-ex.git"
}
@ -28,16 +73,12 @@ node() {
def config = parseYaml(readFile("deployment/config.yaml"))
stage("Build") {
sh "${ocCmd} process -f ${buildManifest} -n rubex-dev | ${ocCmd} apply -f - -n rubex-dev"
sh "${ocCmd} start-build frontend -w -n rubex-dev"
ocBuild(namespace: "rubex-dev", name: "frontend", config: config.dev.build.frontend)
}
stage("Deploy to DEV") {
def replicas = getReplicasOrDefault("frontend", "rubex-dev", config.dev.params.REPLICAS)
sh "${ocCmd} process -f ${appManifest} -v ENV=dev -v REPLICAS=${replicas} -n rubex-dev | ${ocCmd} apply -f - -n rubex-dev"
sh "${ocCmd} tag rubex-dev/frontend:latest rubex-dev/frontend:dev"
sh "${ocCmd} rollout latest dc/frontend -n rubex-dev"
sh "${ocCmd} rollout status dc/frontend -n rubex-dev"
ocTag(isNamespace: "rubex-dev", isName: "frontend", sourceTag: "latest", targetTag: "dev")
ocDeploy(namespace: "rubex-dev", name: "frontend", config: config.dev.deployment.frontend)
}
def isPromoteToTest = false
@ -47,11 +88,8 @@ node() {
if (isPromoteToTest) {
stage("Deploy to TEST") {
def replicas = getReplicasOrDefault("frontend", "rubex-test", config.test.params.REPLICAS)
sh "${ocCmd} process -f ${appManifest} -v ENV=test -v REPLICAS=${replicas} -n rubex-test | ${ocCmd} apply -f - -n rubex-test"
sh "${ocCmd} tag rubex-dev/frontend:dev rubex-dev/frontend:test"
sh "${ocCmd} rollout latest dc/frontend -n rubex-test"
sh "${ocCmd} rollout status dc/frontend -n rubex-test"
ocTag(isNamespace: "rubex-dev", isName: "frontend", sourceTag: "dev", targetTag: "test")
ocDeploy(namespace: "rubex-test", name: "frontend", config: config.test.deployment.frontend)
}
}
}