ruby-ex/deployment/pipeline.groovy

100 lines
2.8 KiB
Groovy
Raw Normal View History

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
def getReplicas(namespace, name) {
2016-12-07 23:28:53 +00:00
def ocCmd = getOcCmd()
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
}
def ocTemplateParametersAsCommandLineOpt(parameters) {
2016-12-15 17:11:58 +00:00
parameters.collect { parameter -> "-v ${parameter}" }.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 17:23:59 +00:00
for (def target : config.delete) {
ocDelete(namespace, target)
}
2016-12-15 17:23:59 +00:00
for (def template : config.templates) {
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() {
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 17:01:04 +00:00
ocBuild("rubex-dev", "frontend", 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 17:02:48 +00:00
ocTag("rubex-dev", "frontend", "latest", "dev")
ocDeploy("rubex-dev", "frontend", 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 17:02:48 +00:00
ocTag("rubex-dev", "frontend", "dev", "test")
ocDeploy("rubex-test", "frontend", 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
}