-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
172 lines (135 loc) · 5.74 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
pipeline {
environment {
PROJECT = "jenkinstest-351122"
APP_NAME = "CN-Spotify"
FE_SVC_NAME = "${APP_NAME}-api-gateway"
CLUSTER = "jenkins-cd"
CLUSTER_ZONE = "europe-central2-a"
IMAGE_TAG = "gcr.io/${PROJECT}/api-gateway:${env.BRANCH_NAME}.${env.BUILD_NUMBER}"
JENKINS_CRED = "JenkinsTest"
}
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
spec:
serviceAccountName: jenkins-sa
containers:
#Services
- name: gcloud
image: gcr.io/cloud-builders/gcloud
command:
- cat
tty: true
- name: kubectl
image: gcr.io/cloud-builders/kubectl
command:
- cat
tty: true
- name: testenv
image: gcr.io/jenkinstest-351122/musics
command:
- cat
tty: true
"""
}
}
stages {
stage('UnitTest') {
steps {
container("gcloud"){
echo 'Strat Testing ...'
sh("gcloud builds submit deploymentDocker.yaml")
}
}
}
stage('Build') { //Builds images and push them to cloud build
steps {
container('gcloud') {
echo 'Starting Image Builds'
dir("app/protobufs/account") {
sh "PYTHONUNBUFFERED=1 gcloud builds submit -t gcr.io/${PROJECT}/account ."
}
dir("app/protobufs/artist"){
sh "PYTHONUNBUFFERED=1 gcloud builds submit -t gcr.io/${PROJECT}/artist ."
}
dir("app/protobufs/api_gateway") {
sh "PYTHONUNBUFFERED=1 gcloud builds submit -t gcr.io/${PROJECT}/api-gateway ."
}
dir("app/protobufs/musics") {
sh "PYTHONUNBUFFERED=1 gcloud builds submit -t gcr.io/${PROJECT}/musics ."
}
echo 'Ended Image Builds'
}
}
}
stage('Deploy Images to GKE with canary') {
when { branch 'canary' }
steps {
container('kubectl') { //Deploy images to GKE
echo "DEPLOY STARTS"
echo "VOU FAZER DEPLOY para o canario"
sh("sed -i.bak 's#gcr.io/gcr.io/api-gateway:1.0.0#${IMAGE_TAG}#' deploymentCanary")
step([$class: 'KubernetesEngineBuilder', namespace:'production', projectId: env.PROJECT, clusterName: env.CLUSTER, zone: env.CLUSTER_ZONE, manifestPattern: 'deploymentCanary.yaml', credentialsId: env.JENKINS_CRED, verifyDeployments: true])
echo "DEPLOY ENDS"
}
}
}
stage('Deploy Images to GKE with master') {
when { branch 'master' }
steps {
container('kubectl') { //Deploy images to GKE
echo "DEPLOY STARTS in Production"
script {
if(sh(script:"kubectl get deploy --namespace production | grep api-gateway", returnStatus: true) == 0) {
echo "Rolling out api.gateway"
sh("kubectl set image deployment api-gateway api-gateway=gcr.io/${PROJECT}/api-gateway:latest --namespace=production ")
sh("kubectl rollout restart deployment api-gateway --namespace=production ")
}
if(sh(script:"kubectl get deploy --namespace production | grep music", returnStatus: true) == 0) {
echo "Rolling out music"
sh("kubectl set image deployment music musics=gcr.io/${PROJECT}/musics:latest --namespace=production ")
sh("kubectl rollout restart deployment music --namespace=production ")
}
if(sh(script:"kubectl get deploy --namespace production | grep artist", returnStatus: true) == 0) {
echo "Rolling out artist"
sh("kubectl set image deployment artist artist=gcr.io/${PROJECT}/artist:latest --namespace=production ")
sh("kubectl rollout restart deployment artist --namespace=production ")
}
if(sh(script:"kubectl get deploy --namespace production | grep account", returnStatus: true) == 0) {
echo "Rolling out account"
sh("kubectl set image deployment account account=gcr.io/${PROJECT}/account:latest --namespace=production ")
sh("kubectl rollout restart deployment account --namespace=production ")
}
if((sh(script:"kubectl get deploy --namespace production | grep api-gateway", returnStatus: true) == 1)
&& (sh(script:"kubectl get deploy --namespace production | grep music", returnStatus: true) == 1)
&& (sh(script:"kubectl get deploy --namespace production | grep artist", returnStatus: true) == 1)
&& (sh(script:"kubectl get deploy --namespace production | grep account", returnStatus: true) == 1)) {
echo "TUDO NOVO"
echo "Deploying into production"
step([$class: 'KubernetesEngineBuilder', namespace:'production', projectId: env.PROJECT, clusterName: env.CLUSTER, zone: env.CLUSTER_ZONE, manifestPattern: 'deploymentProduction.yaml', credentialsId: env.JENKINS_CRED, verifyDeployments: true])
echo "DEPLOY ENDS production"
}
}
}
}
}
stage('Testing Deployments') {
steps {
container('testenv') {
echo 'Strat Testing ...'
dir("app/protobufs/musics") {
sh 'python musicsTest.py'
}
dir("app/protobufs/account") {
sh 'python unitTestAccount.py'
}
dir("app/protobufs/artist") {
sh 'python unitTestArtist.py'
}
}
}
}
}
}