-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
403 lines (378 loc) · 16.4 KB
/
Copy pathJenkinsfile
File metadata and controls
403 lines (378 loc) · 16.4 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
def DOCKER_TAG
def MA_BRANCH
def SLACK_URL = "https://hooks.slack.com/services/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"
def icon = ":heavy_check_mark:"
/*
pathmind-webapp pipeline
The pipeline is made up of following steps
1. Git clone and setup
2. Build and local tests
3. Publish Docker and Helm
4. Optionally deploy to production and test
*/
/*
Build a docker image
*/
def buildDockerImage(image_name, dockerfile, basedir) {
echo "Building the pathmind Docker Image"
sh "docker build -t ${image_name} -f ${basedir}/${dockerfile} ${basedir}/"
}
/*
Build a docker image
*/
def buildDockerImageMA(image_name, dockerfile, basedir, docker_tag) {
echo "Building the pathmind Docker Image"
//Dont quit if there is an error
sh """
set +x
docker image ls | grep pathmind-ma | awk '{print \$3}' | xargs -I {} docker rmi {} -f
docker build -t ${image_name} \
-f ${basedir}/${dockerfile} \
--build-arg S3BUCKET='${docker_tag}-training-static-files.pathmind.com' \
--build-arg AWS_ACCESS_KEY_ID=`kubectl get secret awsaccesskey -o=jsonpath='{.data.AWS_ACCESS_KEY_ID}' | base64 --decode` \
--build-arg AWS_SECRET_ACCESS_KEY=`kubectl get secret awssecretaccesskey -o=jsonpath='{.data.AWS_SECRET_ACCESS_KEY}' | base64 --decode` ${basedir}/ \
--build-arg NATIVERL_FOLDER='nativerl/1_8_1'
"""
}
/*
Publish a docker image
*/
def publishDockerImage(image_name, DOCKER_TAG) {
echo "Logging to aws ecr"
sh "aws ecr get-login --no-include-email --region us-east-1 | sh"
echo "Tagging and pushing the pathmind Docker Image"
sh "docker tag ${image_name} ${DOCKER_REG}/${image_name}:${DOCKER_TAG}"
sh "docker push ${DOCKER_REG}/${image_name}:${DOCKER_TAG}"
}
/*
run migrations
*/
def runMigrations(namespace) {
sh """
set +x
export DB_URL=`kubectl get secret dburl -o=jsonpath='{.data.DB_URL}' -n ${namespace} | base64 --decode; echo`
cd ${WORKSPACE}/pathmind-database
mvn liquibase:update
"""
}
/*
Create db backup
*/
def backupDb(identifier) {
echo "Testing db backup"
sh """
aws rds create-db-snapshot --db-instance-identifier ${identifier} --db-snapshot-identifier ${identifier}-`date '+%Y%m%d'`-${env.BUILD_NUMBER}
timeout 1800 bash -c "while ! aws rds describe-db-snapshots --db-snapshot-identifier ${identifier}-`date '+%Y%m%d'`-${env.BUILD_NUMBER} | jq -r '.[][].Status' | grep available; do sleep 5; done"
"""
}
/*
Flaky tests
*/
def flakyTests() {
sh """
ls -1 ${WORKSPACE}/pathmind-bdd-tests/target/site/serenity/SERENITY-JUNIT*xml | xargs -I {} /usr/bin/xml2json.py -t xml2json {} -o {}.json
mkdir -p ${WORKSPACE}/pathmind-bdd-tests/target/site/serenity/out
/usr/bin/flaky.py --input ${WORKSPACE}/pathmind-bdd-tests/target/site/serenity/
cd ${WORKSPACE}/pathmind-bdd-tests/target/site/serenity/out
ls -1 SERENITY-JUNIT*xml.json | xargs -I {} /usr/bin/xml2json.py {} -t json2xml -o {}.xml
ls -1 SERENITY-JUNIT*xml.json.xml | xargs -I {} sh -c 'mv {} `echo {} | sed "s@.json.xml@@g"`'
"""
}
/*
This is the main pipeline section with the stages of the CI/CD
*/
pipeline {
options {
// Build auto timeout
timeout(time: 120, unit: 'MINUTES')
disableConcurrentBuilds()
buildDiscarder(logRotator(daysToKeepStr: '15', artifactDaysToKeepStr: '15'))
}
// Some global default variables
environment {
IMAGE_NAME = 'pathmind'
DOCKER_REG = "839270835622.dkr.ecr.us-east-1.amazonaws.com"
DEPLOY_PROD = false
}
parameters {
string(name: 'GIT_BRANCH', defaultValue: 'test', description: 'Git branch to build')
booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: false, description: 'If build and tests are good, proceed and deploy to production without manual approval')
}
//all is built and run from the master
agent { node { label 'master' } }
// Pipeline stages
stages {
stage('Git clone and setup') {
when {
anyOf {
environment name: 'GIT_BRANCH', value: 'dev'
environment name: 'GIT_BRANCH', value: 'test'
environment name: 'GIT_BRANCH', value: 'staging'
environment name: 'GIT_BRANCH', value: 'prod'
}
}
steps {
echo "Notifying slack"
sh "set +x; curl -X POST -H 'Content-type: application/json' --data '{\"text\":\":building_construction: Starting Jenkins Job\nBranch: ${env.BRANCH_NAME}\nUrl: ${env.RUN_DISPLAY_URL}\"}' ${SLACK_URL}"
script {
DOCKER_TAG = "dev"
if (env.BRANCH_NAME == 'prod') {
DOCKER_TAG = "prod"
}
if (env.BRANCH_NAME == 'dev') {
DOCKER_TAG = "dev"
}
if (env.BRANCH_NAME == 'test') {
DOCKER_TAG = "test"
}
if (env.BRANCH_NAME == 'staging') {
DOCKER_TAG = "staging"
}
}
echo "Check out code"
checkout scm
// Validate kubectl
sh "kubectl cluster-info"
// Helm version
sh "helm version"
//clean docker
//sh "docker system prune -a -f"
// Define a unique name for the tests container and helm release
script {
branch = GIT_BRANCH.replaceAll('/', '-').replaceAll('\\*', '-')
PATHMIND_ID = "${IMAGE_NAME}-${DOCKER_TAG}-${branch}"
echo "Global pathmind Id set to ${PATHMIND_ID}"
}
}
}
stage('Build Docker Images') {
when {
anyOf {
environment name: 'GIT_BRANCH', value: 'dev'
environment name: 'GIT_BRANCH', value: 'test'
environment name: 'GIT_BRANCH', value: 'staging'
environment name: 'GIT_BRANCH', value: 'prod'
}
}
parallel {
stage('Build pathmind image') {
steps {
buildDockerImage("base", "Dockerfile-cache", "${WORKSPACE}")
buildDockerImage("${IMAGE_NAME}", "Dockerfile", "${WORKSPACE}")
}
}
stage('Build trainer image') {
steps {
buildDockerImage("trainer", "Dockerfile", "${WORKSPACE}/infra/images/trainer/")
}
}
stage('Build rl_training image') {
steps {
buildDockerImage("rl_training", "Dockerfile", "${WORKSPACE}/infra/images/rl_training/")
}
}
stage('Build policy deployer image') {
steps {
buildDockerImage("policy-deployer", "Dockerfile", "${WORKSPACE}/infra/images/policy-deployer/")
}
}
stage('Build pathmind-ma image') {
steps {
script {
MA_BRANCH = env.BRANCH_NAME
}
sh "rm -rf ${WORKSPACE}/nativerl || true"
sh "git clone https://foo:${env.GH_PAT}@github.com/SkymindIO/nativerl.git ${WORKSPACE}/nativerl"
sh "cd ${WORKSPACE}/nativerl && git checkout ${MA_BRANCH}"
buildDockerImageMA("pathmind-ma", "Dockerfile", "${WORKSPACE}/nativerl/nativerl-analyzer", "${env.BRANCH_NAME}")
}
}
}
}
stage('Publish Docker Images') {
when {
anyOf {
environment name: 'GIT_BRANCH', value: 'dev'
environment name: 'GIT_BRANCH', value: 'test'
environment name: 'GIT_BRANCH', value: 'staging'
environment name: 'GIT_BRANCH', value: 'prod'
}
}
parallel {
stage('Publish pathmind image') {
steps {
publishDockerImage("${IMAGE_NAME}", "${DOCKER_TAG}")
}
}
stage('Publish trainer image') {
steps {
publishDockerImage("trainer", "${DOCKER_TAG}")
}
}
stage('Publish rl_training image') {
steps {
publishDockerImage("rl_training", "${DOCKER_TAG}")
}
}
stage('Publish policy-deployer image') {
steps {
publishDockerImage("policy-deployer", "${DOCKER_TAG}")
}
}
stage('Publish pathmind-ma image') {
steps {
publishDockerImage("pathmind-ma", "${DOCKER_TAG}")
}
}
}
}
stage('Deploying helm charts') {
when {
anyOf {
environment name: 'GIT_BRANCH', value: 'dev'
environment name: 'GIT_BRANCH', value: 'test'
environment name: 'GIT_BRANCH', value: 'staging'
}
}
parallel {
stage('Deploying pathmind') {
steps {
script {
echo "Running db migrations"
sh "cd ${WORKSPACE} && mvn clean install"
}
runMigrations("${DOCKER_TAG}")
script {
echo "Updating helm chart"
sh "set +x; bash ${WORKSPACE}/infra/scripts/canary_deploy.sh ${DOCKER_TAG} ${DOCKER_TAG} ${WORKSPACE}"
sh "sleep 90"
echo "Deploying puctuator helm chart"
sh "helm upgrade --install pathmind-punctuator ${WORKSPACE}/infra/helm/pathmind -f ${WORKSPACE}/infra/helm/pathmind/values_${DOCKER_TAG}-punctuator.yaml -n ${DOCKER_TAG}"
echo "Deploying updater helm chart"
sh "helm upgrade --install pathmind-updater ${WORKSPACE}/infra/helm/pathmind -f ${WORKSPACE}/infra/helm/pathmind/values_${DOCKER_TAG}-updater.yaml -n ${DOCKER_TAG}"
echo "Wait for webapp to be available"
sh "timeout 300 bash -c 'while ! curl http://pathmind-${DOCKER_TAG}; do sleep 5; done'"
sh "timeout 300 bash -c 'while ! curl http://pathmind-slot-${DOCKER_TAG}; do sleep 5; done'"
}
}
}
stage('Deploying trainer') {
steps {
script {
sh "helm upgrade --install trainer ${WORKSPACE}/infra/helm/trainer -f ${WORKSPACE}/infra/helm/trainer/values_${DOCKER_TAG}.yaml -n ${DOCKER_TAG}"
}
}
}
stage('Deploying pathmind-api') {
steps {
script {
sh "helm upgrade --install pathmind-api ${WORKSPACE}/infra/helm/pathmind-api -f ${WORKSPACE}/infra/helm/pathmind-api/values_${DOCKER_TAG}-api.yaml -n ${DOCKER_TAG}"
}
}
}
stage('Deploying policy-deployer') {
steps {
script {
sh "helm upgrade --install policy-deployer ${WORKSPACE}/infra/helm/policy-deployer -f ${WORKSPACE}/infra/helm/policy-deployer/values_${DOCKER_TAG}.yaml -n ${DOCKER_TAG}"
}
}
}
stage('Deploying pathmind-ma') {
steps {
script {
sh "helm upgrade --install pathmind-ma ${WORKSPACE}/infra/helm/pathmind-ma -f ${WORKSPACE}/infra/helm/pathmind-ma/values_${DOCKER_TAG}.yaml -n ${DOCKER_TAG}"
}
}
}
}
}
// Waif for user manual approval, or proceed automatically if DEPLOY_TO_PROD is true
stage('Go for Production?') {
when {
allOf {
environment name: 'GIT_BRANCH', value: 'prod'
environment name: 'DEPLOY_TO_PROD', value: 'false'
}
}
steps {
// Prevent any older builds from deploying to production
milestone(1)
input 'Proceed and deploy to Production?'
milestone(2)
script {
DEPLOY_PROD = true
}
}
}
stage('Deploying to Production') {
when {
anyOf {
expression { DEPLOY_PROD == true }
environment name: 'DEPLOY_TO_PROD', value: 'true'
}
}
parallel {
stage('Deploying pathmind') {
steps {
script {
DEPLOY_PROD = true
echo "Running db migrations"
sh "cd ${WORKSPACE} && mvn clean install"
}
backupDb("pathmind-prod")
runMigrations("default")
script {
echo "Updating helm chart"
sh "set +x; bash ${WORKSPACE}/infra/scripts/canary_deploy.sh default ${DOCKER_TAG} ${WORKSPACE}"
sh "sleep 90"
echo "Deploying punctuator helm chart"
sh "helm upgrade --install pathmind-punctuator ${WORKSPACE}/infra/helm/pathmind -f ${WORKSPACE}/infra/helm/pathmind/values_${DOCKER_TAG}-punctuator.yaml"
echo "Deploying updater helm chart"
sh "helm upgrade --install pathmind-updater ${WORKSPACE}/infra/helm/pathmind -f ${WORKSPACE}/infra/helm/pathmind/values_${DOCKER_TAG}-updater.yaml"
}
}
}
stage('Deploying trainer') {
steps {
script {
sh "helm upgrade --install trainer ${WORKSPACE}/infra/helm/trainer -f ${WORKSPACE}/infra/helm/trainer/values_${DOCKER_TAG}.yaml"
}
}
}
stage('Deploying pathmind-api') {
steps {
script {
sh "helm upgrade --install pathmind-api ${WORKSPACE}/infra/helm/pathmind-api -f ${WORKSPACE}/infra/helm/pathmind-api/values_${DOCKER_TAG}-api.yaml"
}
}
}
stage('Deploying policy-deployer') {
steps {
script {
sh "helm upgrade --install policy-deployer ${WORKSPACE}/infra/helm/policy-deployer -f ${WORKSPACE}/infra/helm/policy-deployer/values_${DOCKER_TAG}.yaml"
}
}
}
stage('Deploying pathmind-ma') {
steps {
script {
sh "helm upgrade --install pathmind-ma ${WORKSPACE}/infra/helm/pathmind-ma -f ${WORKSPACE}/infra/helm/pathmind-ma/values_${DOCKER_TAG}.yaml"
}
}
}
}
}
}
post {
always {
echo 'Notifying Slack'
script {
if (currentBuild.result != "SUCCESS") {
icon = ":x:"
}
}
echo "Notifying slack"
sh "set +x; curl -X POST -H 'Content-type: application/json' --data '{\"text\":\"${icon} Jenkins Job Finished\nBranch: ${env.BRANCH_NAME}\nUrl: ${env.RUN_DISPLAY_URL}\nStatus: ${currentBuild.result}\"}' ${SLACK_URL}"
}
}
}