-
Notifications
You must be signed in to change notification settings - Fork 22
/
Jenkinsfile
128 lines (124 loc) · 5.66 KB
/
Jenkinsfile
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
pipeline {
agent any
environment {
IMAGE_NAME_PROD = 'airqo-analytics-api'
IMAGE_NAME_STAGE = 'airqo-stage-analytics-api'
}
stages {
stage("Start"){
steps {
slackSend (color: '#1976d2', message: "BUILD STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
stage("Checkout code") {
steps {
checkout scm
}
}
stage("Build for Staging"){
when {branch "staging"}
steps {
echo "Docker Build"
sh """
docker build -t ${REGISTRY_URI}/${PROJECT_ID}/${IMAGE_NAME_STAGE}:"latest" -f "src/analytics/Dockerfile.stage.txt" .
"""
withCredentials([file(credentialsId: "${GCR_CONFIG}", variable: 'GC_KEY')]){
sh "cat '$GC_KEY' | docker login -u _json_key --password-stdin ${REGISTRY_URI}"
sh "gcloud auth activate-service-account --key-file='$GC_KEY'"
sh "gcloud auth configure-docker"
echo "Pushing image to GCR"
sh """
gcloud container images add-tag ${REGISTRY_URI}/${PROJECT_ID}/${IMAGE_NAME_STAGE} ${REGISTRY_URI}/${PROJECT_ID}/${IMAGE_NAME_STAGE}:"v${env.BUILD_NUMBER}"
docker push ${REGISTRY_URI}/${PROJECT_ID}/${IMAGE_NAME_STAGE}:"latest"
"""
}
}
post{
success{
echo "Build and Push Successfully"
}
failure{
echo "Build and Push Failed"
slackSend (color: '#FF0000', message: "FAILED TO BUILD AND PUSH IMAGE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}
stage("Build for Production"){
when {branch "master"}
steps {
echo "Docker Build"
sh """
docker build -t ${REGISTRY_URI}/${PROJECT_ID}/${IMAGE_NAME_PROD}:"latest" -f "src/analytics/Dockerfile" .
"""
withCredentials([file(credentialsId: "${GCR_CONFIG}", variable: 'GC_KEY')]){
sh "cat '$GC_KEY' | docker login -u _json_key --password-stdin ${REGISTRY_URI}"
sh "gcloud auth activate-service-account --key-file='$GC_KEY'"
sh "gcloud auth configure-docker"
echo "Pushing image to GCR"
sh """
gcloud container images add-tag ${REGISTRY_URI}/${PROJECT_ID}/${IMAGE_NAME_PROD} ${REGISTRY_URI}/${PROJECT_ID}/${IMAGE_NAME_PROD}:"v${env.BUILD_NUMBER}"
docker push ${REGISTRY_URI}/${PROJECT_ID}/${IMAGE_NAME_PROD}:"latest"
"""
}
}
post{
success{
echo "Build and Push Successfully"
}
failure{
echo "Build and Push Failed"
echo 'analytics service deployed successfully'
slackSend (color: '#FF0000', message: "FAILED TO BUILD AND PUSH IMAGE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}
stage("Deploy to Staging"){
when {branch "staging"}
steps {
withKubeConfig([credentialsId: "${K8S_CONFIG}"]) {
sh """
kubectl apply -f 'k8s/stage-analytics-api.yaml'
kubectl rollout restart deployment/${IMAGE_NAME_STAGE} -n staging
"""
}
}
post{
success{
echo "Successfully deployed to staging"
}
failure{
echo "Failed deploying to staging"
echo 'analytics service deployed successfully'
slackSend (color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}
stage("Deploy to Production"){
when {branch "master"}
steps {
withKubeConfig([credentialsId: "${K8S_CONFIG}"]) {
sh """
kubectl apply -f 'k8s/prod-analytics-api.yaml'
kubectl rollout restart deployment/${IMAGE_NAME_PROD} -n production
"""
}
}
post{
success{
echo "Successfully deployed to production"
}
failure{
echo "Failed deploying to production"
echo 'analytics service deployed successfully'
slackSend (color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}
stage('Final'){
steps {
echo 'analytics service deployed successfully'
slackSend (color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}
}