This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathJenkinsfile-build-and-deploy
89 lines (74 loc) · 4.47 KB
/
Jenkinsfile-build-and-deploy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import groovy.json.JsonSlurperClassic
def deployenv = 'artifactory.wikia-inc.com/platform/alpine:3.6-curl'
def kubectl = env["K8S_DEPLOYER_IMAGE"]
node('docker-daemon') {
// serviceVersion is used in k8s deployment name - capital letters are forbidden there
def serviceVersion = params.version.toLowerCase()
def namespace = params.namespace
def cluster = params.cluster ?: "prod"
def context = "kube-${params.datacenter}-${cluster}"
def imageVersion = params.version
def normalizedEnv = params.environment ==~ /sandbox-/ ? "sandbox" : params.environment
def imageExists = false
def imageName = ""
stage('Clone sources') {
checkout scm
println "imageName: ${imageName}"
if (params.crowdin_branch != null) {
def timestamp = System.currentTimeMillis()
imageVersion = "${imageVersion}-${timestamp}"
}
imageName = "artifactory.wikia-inc.com/mobile-wiki:${imageVersion}"
}
withDockerContainer(deployenv){
stage('Check if image already exists') {
println "Using version ${imageVersion}"
def status = sh (script: "curl -u ${env.JENKINS_ARTIFACTORY_USERNAME}:${env.JENKINS_ARTIFACTORY_PASSWORD} -w \"%{http_code}\" -s -I -o /dev/null -XGET \"https://artifactory.wikia-inc.com/artifactory/api/storage/dockerv2-local/mobile-wiki/${imageVersion}\"", returnStdout: true).trim()
if ( status == "200" ){
println "Image ${imageName} already exists"
imageExists = true
}
}
}
stage('Build and push image') {
if ( !imageExists ) {
if (params.crowdin_branch != null) {
sh """#!/bin/sh
./tasks/download-translations.sh /etc/crowdin/crowdin.conf ${params.crowdin_branch}"""
}
sh """#!/bin/sh
./tasks/docker-build.sh ${imageName} ${imageVersion} ${env.GITHUB_TOKEN}"""
}
}
withDockerContainer(deployenv) {
stage('Create k8s descriptor') {
sh """#!/bin/sh
./tasks/k8s-descriptor.sh ${params.environment} ${serviceVersion} ${imageName} ${params.datacenter}"""
}
}
withDockerContainer(kubectl) {
stage('Apply k8s descriptor') {
// create new deployment
sh "kubectl -n prod --context kube-${env.datacenter}-prod --kubeconfig=/config/.kube/config apply -f k8s/k8s-deployment-descriptor-${env.environment}.yaml"
// wait for rolling out the deployment
sh "kubectl -n prod --context kube-${env.datacenter}-prod --kubeconfig=/config/.kube/config rollout status deploy mobile-wiki-${env.environment}-${serviceVersion} -w"
// get service details to have current (old) deployment version
def serviceInfo = sh(script: "kubectl -n prod --context kube-${env.datacenter}-prod --kubeconfig=/config/.kube/config get service -l app=mobile-wiki,env=${env.environment} -o=json", returnStdout: true)
// do not create a variable with json slurper instance, it may cause NotSerializableException
// see: https://stackoverflow.com/a/53756295
def service = new JsonSlurperClassic().parseText(serviceInfo)
def previousVersion = service.items.size() == 1 ? service.items[0].spec.selector.version : null;
// switch service to direct to new deployment
sh "kubectl -n prod --context kube-${env.datacenter}-prod --kubeconfig=/config/.kube/config apply -f k8s/k8s-service-descriptor-${env.environment}.yaml"
// to prevent deleting deployments when redeploying the same version
if (previousVersion && previousVersion != serviceVersion) {
// delete all inactive deployments
sh "kubectl -n prod --context kube-${env.datacenter}-prod --kubeconfig=/config/.kube/config delete deployments -l app=mobile-wiki,env=${env.environment},status=inactive"
// scale old deployment to 1 instance
sh "kubectl -n prod --context kube-${env.datacenter}-prod --kubeconfig=/config/.kube/config scale deployment --replicas=0 -l app=mobile-wiki,env=${env.environment},version=${previousVersion}"
// label old deployment with status=inactive to be able to remove it during next deployment
sh "kubectl -n prod --context kube-${env.datacenter}-prod --kubeconfig=/config/.kube/config label --overwrite deployment -l app=mobile-wiki,env=${env.environment},version=${previousVersion} status=inactive"
}
}
}
}