-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
391 lines (370 loc) · 18.7 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
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
def functionalTestUrl = null
void setBuildStatus(String message, String state, String context) {
step([
$class: "GitHubCommitStatusSetter",
reposSource: [$class: "ManuallyEnteredRepositorySource", url: "https://github.com/paladinarcher/vetsez-ds-demo1-coding-challenge-app"],
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: context],
errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
]);
}
pipeline {
agent {
node {
label 'ruby'
}
}
parameters {
string(name: 'releaseVersion', defaultValue: '', description: 'Provide the release version (leave blank for no release):')
string(name: 'developmentVersion', defaultValue: '', description: 'Provide the next development version (leave blank for no release):')
}
environment {
DATABASE_USER = 'dsbpa'
DATABASE_PASSWORD = 'dsbpa'
DATABASE_SCHEMA = 'ci'
}
stages {
stage('Create Release') {
when {
expression {
return params.releaseVersion != ''
}
}
steps {
//Set release version
sh "mvn org.codehaus.mojo:versions-maven-plugin:2.5:set -DnewVersion=${params.releaseVersion} -DgenerateBackupPoms=false -DprocessAllModules=true"
//Update SNAPSHOT dependencies to their release versions if available
sh "mvn org.codehaus.mojo:versions-maven-plugin:2.5::use-releases -DprocessParent=true"
//Check for any snapshot versions remaining
sh "mvn compile validate"
//Commit changes locally
sh "git commit -a -m \"Releasing version ${params.releaseVersion}\""
//Tag release
sh "git tag -a ${params.releaseVersion} -m \"Release version ${params.releaseVersion}\""
//Set the next dev version
sh "mvn org.codehaus.mojo:versions-maven-plugin:2.5::set -DnewVersion=${params.developmentVersion} -DgenerateBackupPoms=false -DprocessAllModules=true"
//Commit changes locally
sh "git commit -a -m \"Preparing POMs for next development version ${params.developmentVersion}\""
script {
def url = sh(returnStdout: true, script: 'git config remote.origin.url').trim()
def repo = url.substring(url.indexOf('://')+3)
withCredentials([usernamePassword(credentialsId: 'github', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
//Push the branch to the remote
sh "git push https://${GIT_USERNAME}:${GIT_PASSWORD}@${repo} HEAD:${env.BRANCH_NAME}"
//Push the tag to the remote
sh "git push https://${GIT_USERNAME}:${GIT_PASSWORD}@${repo} --tags"
}
}
//Checkout the tag
sh "git checkout tags/${params.releaseVersion}"
}
post {
success {
setBuildStatus("Create Release.", "SUCCESS", "ci/jenkins/createRelease")
}
failure {
setBuildStatus("Create Release.", "FAILURE", "ci/jenkins/createRelease")
}
}
}
stage('Building Application') {
steps {
sh "mvn clean install"
stash name: 'mavenOutput', includes: '**/target/*'
}
// post {
// always {
// junit '**/target/surefire-reports/*.xml'
// }
// }
post {
success {
setBuildStatus("Building Application.", "SUCCESS", "ci/jenkins/buildingApplication")
}
failure {
setBuildStatus("Building Application.", "FAILURE", "ci/jenkins/buildingApplication")
}
}
}
stage('Source Code Analysis') {
steps {
echo "TDB..."
}
post {
success {
setBuildStatus("Source Code Analysis.", "SUCCESS", "ci/jenkins/sourceCodeAnalysis")
}
failure {
setBuildStatus("Source Code Analysis.", "FAILURE", "ci/jenkins/sourceCodeAnalysis")
}
}
}
stage("Build Containers") {
parallel {
stage("Database Migration Container") {
agent {
node {
label 'docker'
}
}
steps {
script {
if (params.releaseVersion != '') {
//Checkout the tag
def url = sh(returnStdout: true, script: 'git config remote.origin.url').trim()
def repo = url.substring(url.indexOf('://')+3)
withCredentials([usernamePassword(credentialsId: 'github', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh "git fetch https://${GIT_USERNAME}:${GIT_PASSWORD}@${repo} +refs/tags/${params.releaseVersion}:refs/tags/${params.releaseVersion}"
sh "git checkout tags/${params.releaseVersion}"
}
}
def DOCKER_REGISTRY_URI = env.DOCKER_REGISTRY_URL
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'docker-registry',
usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
sh 'docker login --password=${PASSWORD} --username=${USERNAME} ${DOCKER_REGISTRY_URI}'
//}
//docker.withRegistry(env.DOCKER_REGISTRY_URL, "docker-registry") { //env.DOCKER_REGISTRY_URL
dbImage = docker.build("paladinarcher/coding-challenge-db-init", "-f Dockerfile.db-init .")
dbImage.push("${env.BRANCH_NAME}-${env.GIT_COMMIT}")
if (params.releaseVersion != '') {
dbImage.push(params.releaseVersion)
}
}
}
}
post {
success {
setBuildStatus("Database Migration Container.", "SUCCESS", "ci/jenkins/databaseMigrationContainer")
}
failure {
setBuildStatus("Database Migration Container.", "FAILURE", "ci/jenkins/databaseMigrationContainer")
}
}
}
stage("Application Container") {
agent {
node {
label 'docker'
}
}
steps {
unstash 'mavenOutput'
script {
def DOCKER_REGISTRY_URI = env.DOCKER_REGISTRY_URL
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'docker-registry',
usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
sh 'docker login --password=${PASSWORD} --username=${USERNAME} ${DOCKER_REGISTRY_URI}'
//docker.withRegistry(env.DOCKER_REGISTRY_URL, "docker-registry") { //env.DOCKER_REGISTRY_URL
image = docker.build("paladinarcher/coding-challenge-app")
image.push("${env.BRANCH_NAME}-${env.GIT_COMMIT}")
if (params.releaseVersion != '') {
image.push(params.releaseVersion)
}
}
}
}
post {
success {
setBuildStatus("Application Container.", "SUCCESS", "ci/jenkins/applicationContainer")
}
failure {
setBuildStatus("Application Container.", "FAILURE", "ci/jenkins/applicationContainer")
}
}
}
}
}
stage("Testing") {
parallel {
stage("Functional") {
stages {
stage('Deploy Functional Test Environment') {
agent {
node {
label 'helm'
}
}
steps {
script {
def releaseName = "ft-${env.BRANCH_NAME.toLowerCase()}"
//Download the Chart
sh "git clone \"https://github.com/paladinarcher/vetsez-ds-demo1-coding-challenge-devops.git\" helmChart"
//Deploy the Chart
sh "helm install -n ${releaseName} --set \"image.tag=${env.BRANCH_NAME}-${env.GIT_COMMIT}\" --set \"initImage.tag=${env.BRANCH_NAME}-${env.GIT_COMMIT}\" --set \"image.pullPolicy=Always\" --set \"initImage.pullPolicy=Always\" --set \"postgresql.persistence.enabled=false\" --namespace development helmChart/k8s/coding-challenge-app"
//Find the Service Port
def count = 0
functionalTestUrl = "http://"
while (functionalTestUrl=="http://" && count < 300) {
functionalTestUrl = sh(returnStdout: true, script: "kubectl get --namespace development services -l app.kubernetes.io/instance=${releaseName} -o jsonpath=\"http://{.items[0].metadata.name}.development.svc.cluster.local:{.items[0].spec.ports[0].port}\"")
if(functionalTestUrl=="http://") { sleep 5 }
count+=10
}
echo "Service is available at ${functionalTestUrl}"
}
}
post {
success {
setBuildStatus("Deploy Functional Test Environment.", "SUCCESS", "ci/jenkins/deployFunctionalTestEnvironment")
}
failure {
setBuildStatus("Deploy Functional Test Environment.", "FAILURE", "ci/jenkins/deployFunctionalTestEnvironment")
}
//Clean up our helm checkout
always {
sh 'rm -rf helmChart'
}
}
}
stage('Functional Test Execution') {
agent {
node {
label 'selenium'
}
}
steps {
script {
try {
dir('test/selenium') {
sh "echo \"Working Directory is \$(pwd)\""
sh 'yarn install'
sh "mkdir -p reports"
sh "export PATH=\$PATH:/usr/bin:\$(pwd)/node_modules/.bin:\$(pwd)/node_modules/chromedriver/lib/chromedriver; selenium-side-runner --base-url ${functionalTestUrl} --server http://localhost:4444/wd/hub --output-directory=reports --output-format=junit -c \"browserName=chrome\" coding-challenge-app.side"
}
} catch (err) {
if (env.BRANCH_NAME != 'master') {
currentBuild.result = 'UNSTABLE'
} else {
throw err
}
}
}
}
post {
success {
setBuildStatus("Functional Test Execution.", "SUCCESS", "ci/jenkins/functionalTestExecution")
}
failure {
setBuildStatus("Functional Test Execution.", "FAILURE", "ci/jenkins/functionalTestExecution")
}
always {
junit '**/test/selenium/reports/*.xml'
}
}
}
}
post {
always {
script {
node('helm') {
sh ("helm delete --purge ft-${env.BRANCH_NAME.toLowerCase()}")
}
}
}
}
}
stage("Performance Testing") {
steps {
echo "TDB..."
}
post {
success {
setBuildStatus("Performance Testing.", "SUCCESS", "ci/jenkins/performanceTesting")
}
failure {
setBuildStatus("Performance Testing.", "FAILURE", "ci/jenkins/performanceTesting")
}
}
}
}
}
stage("Review Instance Deployment") {
when {
changeRequest()
}
agent {
node {
label 'helm'
}
}
steps {
script {
def releaseName = "${env.BRANCH_NAME.toLowerCase()}"
//Download the Chart
sh "git clone \"https://github.com/paladinarcher/vetsez-ds-demo1-coding-challenge-devops.git\" helmChart"
//Deploy the Chart
sh "helm upgrade --install ${releaseName} --set \"image.tag=${env.BRANCH_NAME}-${env.GIT_COMMIT}\" --set \"initImage.tag=${env.BRANCH_NAME}-${env.GIT_COMMIT}\" --set \"image.pullPolicy=Always\" --set \"initImage.pullPolicy=Always\" --set \"postgresql.persistence.enabled=false\" --namespace development helmChart/k8s/coding-challenge-app"
//Wait a few seconds for the external IP to be allocated
def count = 0
def previewUrl = "http://"
while (previewUrl=="http://" && count < 300) {
previewUrl = sh(returnStdout: true, script: "kubectl get --namespace development services -l app.kubernetes.io/instance=${releaseName} -o jsonpath=\"http://{.items[0].status.loadBalancer.ingress[0].hostname}\"")
if(previewUrl=="http://") { sleep 5 }
count+=10
}
if(previewUrl=="http://") {
error("Failed to get a preview URL in ${count} seconds...")
}
//Print out the preview instance URL
echo "Preview instance is available at ${previewUrl}/dsbpa"
//Add the Preview URL to the PR
pullRequest.createStatus(
status: 'success',
context: 'continuous-integration/jenkins/pr-merge/preview',
description: 'Preview Instance',
targetUrl: "${previewUrl}/dsbpa"
)
}
}
post {
success {
setBuildStatus("Review Instance Deployment.", "SUCCESS", "ci/jenkins/reviewInstanceDeployment")
}
failure {
setBuildStatus("Review Instance Deployment.", "FAILURE", "ci/jenkins/reviewInstanceDeployment")
}
//Clean up our helm checkout
always {
sh 'rm -rf helmChart'
}
}
}
stage("Promote to Development") {
when {
not {
changeRequest()
}
}
agent {
node {
label 'docker'
}
}
steps {
script {
def DOCKER_REGISTRY_URI = env.DOCKER_REGISTRY_URL
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'docker-registry',
usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
sh 'docker login --password=${PASSWORD} --username=${USERNAME} ${DOCKER_REGISTRY_URI}'
//docker.withRegistry(env.DOCKER_REGISTRY_URL, "docker-registry") { //env.DOCKER_REGISTRY_URL
image = docker.image("paladinarcher/coding-challenge-app:${env.BRANCH_NAME}-${env.GIT_COMMIT}")
image.pull()
image.push("development-${env.GIT_COMMIT}")
image.push("latest")
initImage = docker.image("paladinarcher/coding-challenge-db-init:${env.BRANCH_NAME}-${env.GIT_COMMIT}")
initImage.pull()
initImage.push("development-${env.GIT_COMMIT}")
initImage.push("latest")
}
}
}
post {
success {
setBuildStatus("Promote to Development.", "SUCCESS", "ci/jenkins/promoteToDevelopment")
}
failure {
setBuildStatus("Promote to Development.", "FAILURE", "ci/jenkins/promoteToDevelopment")
}
}
}
}
}