-
Notifications
You must be signed in to change notification settings - Fork 218
/
Jenkinsfile
348 lines (310 loc) · 12.1 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
String unique_id = org.apache.commons.lang.RandomStringUtils.random(4, false, true)
pipeline {
environment {
versionTag= readFile('./version.conf')
gitBranch = "${env.BRANCH_NAME}"
imageName = "memphis"
repoUrlPrefix = "memphisos"
test_suffix = "test"
}
agent {
label 'memphis-jenkins-big-fleet,'
}
stages {
stage('Login to Docker Hub') {
steps {
withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'DOCKER_HUB_CREDS_USR', passwordVariable: 'DOCKER_HUB_CREDS_PSW')]) {
sh 'docker login -u $DOCKER_HUB_CREDS_USR -p $DOCKER_HUB_CREDS_PSW'
}
}
}
stage('Create memphis namespace in Kubernetes'){
steps {
sh """
minikube start
minikube addons enable gcp-auth --refresh
kubectl config use-context minikube
kubectl create namespace memphis-$unique_id --dry-run=client -o yaml | kubectl apply -f -
gsutil cp gs://memphis-jenkins-backup-bucket/regcred.yaml .
kubectl create sa default -n memphis-$unique_id || true
kubectl apply -f regcred.yaml -n memphis-$unique_id
kubectl patch serviceaccount default -p '{\"imagePullSecrets\": [{\"name\": \"regcred\"}]}' -n memphis-$unique_id
"""
}
}
stage('Build and push docker image to Docker Hub') {
steps {
sh "docker buildx build --push --tag ${repoUrlPrefix}/${imageName}-${gitBranch} --platform linux/amd64,linux/arm64 ."
}
}
stage('Tests - Install/upgrade Memphis cli - BETA') {
when { anyOf { branch 'master'; branch 'qa'}}
steps {
sh """
sudo npm uninstall memphis-dev-cli-beta -g
sudo npm i memphis-dev-cli-beta -g --force
"""
}
}
stage('Tests - Install/upgrade Memphis cli - LATEST') {
when { branch 'latest' }
steps {
sh """
sudo npm uninstall memphis-dev-cli -g
sudo npm i memphis-dev-cli -g
"""
}
}
////////////////////////////////////////
//////////// Docker-Compose ////////////
////////////////////////////////////////
stage('Tests - Docker compose install - Master') {
when { branch 'master' }
steps {
sh "rm -rf memphis-docker"
dir ('memphis-devops'){
git credentialsId: 'main-github', url: 'git@github.com:memphisdev/memphis-devops.git', branch: 'master'
}
sh "docker-compose -f ./memphis-devops/docker/docker-compose-master-tests-broker.yml -p memphis up -d"
}
}
stage('Tests - Docker compose install - QA') {
when { branch 'qa' }
steps {
sh "rm -rf memphis-docker"
dir ('memphis-devops'){
git credentialsId: 'main-github', url: 'git@github.com:memphisdev/memphis-devops.git', branch: 'master'
}
sh "docker-compose -f ./memphis-devops/docker/docker-compose-qa-tests-broker.yml -p memphis up -d"
}
}
stage('Tests - Docker compose install - Latest') {
when { branch 'latest' }
steps {
sh "rm -rf memphis-docker"
dir ('memphis-devops'){
git credentialsId: 'main-github', url: 'git@github.com:memphisdev/memphis-devops.git', branch: 'master'
}
sh "docker-compose -f ./memphis-devops/docker/docker-compose-latest-tests-broker.yml -p memphis up -d"
}
}
stage('Tests - Run e2e tests over Docker') {
steps {
sh "rm -rf memphis-e2e-tests"
dir ('memphis-e2e-tests'){
git credentialsId: 'main-github', url: 'git@github.com:memphisdev/memphis-e2e-tests.git', branch: 'master'
}
sh "npm install --prefix ./memphis-e2e-tests"
sh "node ./memphis-e2e-tests/index.js docker"
}
}
stage('Tests - Remove Docker compose - Master') {
when { branch 'master' }
steps {
sh """
docker-compose -f ./memphis-devops/docker/docker-compose-master-tests-broker.yml -p memphis down
docker volume prune -f
"""
}
}
stage('Tests - Remove Docker compose - QA') {
when { branch 'qa' }
steps {
sh """
docker-compose -f ./memphis-devops/docker/docker-compose-qa-tests-broker.yml -p memphis down
docker volume prune -f
"""
}
}
stage('Tests - Remove Docker compose - Latest') {
when { branch 'latest' }
steps {
sh """
docker-compose -f ./memphis-devops/docker/docker-compose-latest-tests-broker.yml -p memphis down
docker volume prune -f
"""
}
}
////////////////////////////////////////
//////////// Kubernetes ////////////
////////////////////////////////////////
stage('Tests - Install memphis with helm') {
steps {
dir ('memphis-k8s'){
git credentialsId: 'main-github', url: 'git@github.com:memphisdev/memphis-k8s.git', branch: 'master'
sh """
helm install memphis-tests memphis --set memphis.extraEnvironmentVars.enabled=true,memphis.image=${repoUrlPrefix}/${imageName}-${gitBranch},"imagePullSecrets[0].name=regcred" --set-json 'memphis.extraEnvironmentVars.vars=[{"name":"ENV","value":"staging"}]' --create-namespace --namespace memphis-$unique_id --wait
"""
}
}
}
stage('Open port forwarding to memphis service - Minikube') {
steps {
sh """
until kubectl get pods --selector=app.kubernetes.io/name=memphis -o=jsonpath="{.items[*].status.containerStatuses[*].ready}" -n memphis-$unique_id | grep -v "false" ; do sleep 1; done
nohup kubectl port-forward service/memphis 6666:6666 9000:9000 7770:7770 --namespace memphis-$unique_id &
"""
}
}
stage('Tests - Run e2e tests over kubernetes - Minikube') {
steps {
sh """
npm install --prefix ./memphis-e2e-tests
node ./memphis-e2e-tests/index.js kubernetes memphis-$unique_id
"""
}
}
stage('Tests - Uninstall helm') {
steps {
sh """
helm uninstall memphis-tests -n memphis-$unique_id
kubectl delete ns memphis-$unique_id
lsof -i :6666,9000,7770 | grep kubectl | awk '{print \"kill -9 \"\$2}' | sh
"""
}
}
////////////////////////////////////////
//////////// Build & Push ////////////
////////////////////////////////////////
stage('Build and push image to Docker Hub - LATEST') {
when { branch 'latest' }
steps {
sh """
docker buildx build --push --tag ${repoUrlPrefix}/${imageName}:${versionTag} --tag ${repoUrlPrefix}/${imageName} --platform linux/amd64,linux/arm64 .
"""
}
}
//////////////////////////////////////
//////////// K8's Tests ////////////
//////////////////////////////////////
stage('Reset STG-OSS environment') {
when { not {branch 'latest'}}
steps {
sh """gcloud container clusters get-credentials memphis-staging-gke --region europe-west3 --project memphis-k8s-staging"""
catchError(buildResult: 'SUCCESS', message: 'helm uninstall failed because memphis was not deployed to this namespace') {
sh """helm uninstall my-memphis --kubeconfig ~/.kube/config -n memphis"""
}
sh """kubectl get pvc -n memphis | grep -v NAME| awk '{print\$1}' | while read vol; do kubectl delete pvc \$vol -n memphis; done"""
}
}
stage('Push to STG-OSS') {
when { not {branch 'latest'}}
steps {
dir ('memphis-k8s'){
git credentialsId: 'main-github', url: 'git@github.com:memphisdev/memphis-k8s.git', branch: 'master'
sh """
gsutil cp gs://memphis-jenkins-backup-bucket/memphis-staging-oss.yaml .
helm install my-memphis memphis --set memphis.image=${repoUrlPrefix}/${imageName}-${gitBranch} -f ./memphis-staging-oss.yaml --create-namespace --namespace memphis --wait
"""
}
sh "rm -rf memphis-k8s"
}
}
stage('Open port forwarding to memphis service - K8s') {
when { not {branch 'latest'}}
steps {
sh """
until kubectl get pods --selector=app.kubernetes.io/name=memphis -o=jsonpath="{.items[*].status.containerStatuses[*].ready}" -n memphis | grep -v "false" ; do sleep 1; done
nohup kubectl port-forward service/memphis 6666:6666 9000:9000 7770:7770 --namespace memphis &
"""
}
}
stage('Tests - Run e2e tests over kubernetes - K8s') {
when { not {branch 'latest'}}
steps {
sh """
npm install --prefix ./memphis-e2e-tests
node ./memphis-e2e-tests/index.js kubernetes memphis
"""
}
}
stage('Install memphis CLI') {
when { not {branch 'latest'}}
steps {
sh """
sudo npm i memphis-dev-cli -g
"""
}
}
stage('Create staging user') {
when { not {branch 'latest'}}
steps {
withCredentials([string(credentialsId: 'staging_pass', variable: 'staging_pass')]) {
sh """
mem connect -s localhost -u root -p \$(kubectl get secret memphis-creds -n memphis -o jsonpath="{.data.ROOT_PASSWORD}" | base64 --decode)
mem user add -u staging -p $staging_pass
lsof -i :6666,9000 | grep kubectl | awk '{print \"kill -9 \"\$2}' | sh
"""
}
}
}
stage('Tests - remove port-forwarding') {
when { not {branch 'latest'}}
steps {
sh"""
/usr/sbin/lsof -i :6666,9000 | grep kubectl | awk '{print \"kill -9 \"\$2}' | sh
"""
}
}
stage('Tests - Remove used directories') {
when { not {branch 'latest'}}
steps {
sh "rm -rf memphis-e2e-tests"
}
}
//////////////////////////////////////////////////////////
////////////// Checkout to version branch //////////////
//////////////////////////////////////////////////////////
stage('checkout to version branch'){
when { branch 'latest' }
steps {
withCredentials([sshUserPrivateKey(keyFileVariable:'check',credentialsId: 'main-github')]) {
sh """
git reset --hard origin/latest
GIT_SSH_COMMAND='ssh -i $check' git checkout -b ${versionTag}
GIT_SSH_COMMAND='ssh -i $check' git push --set-upstream origin ${versionTag}
"""
}
}
}
stage('Install gh + create new release'){
when { branch 'latest' }
steps {
withCredentials([string(credentialsId: 'gh_token', variable: 'GH_TOKEN')]) {
sh """
sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo -y
sudo dnf install gh -y
gh release create v${versionTag} --generate-notes
"""
}
}
}
}
post {
always {
cleanWs()
}
success {
notifySuccessful()
}
failure {
notifyFailed()
}
}
}
def notifySuccessful() {
emailext (
subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':
Check console output and connection attributes at ${env.BUILD_URL}""",
recipientProviders: [requestor()]
)
}
def notifyFailed() {
emailext (
subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':
Check console output at ${env.BUILD_URL}""",
recipientProviders: [requestor()]
)
}