-
Notifications
You must be signed in to change notification settings - Fork 48
/
Jenkinsfile
528 lines (468 loc) · 14 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!groovy
pipeline {
agent any
parameters {
string(name: 'CIVIHR_BUILDNAME', defaultValue: "civihr-dev_$BRANCH_NAME", description: 'CiviHR site name')
booleanParam(name: 'DESTROY_SITE', defaultValue: true, description: 'Destroy built site after build finish')
}
environment {
WEBROOT = "/opt/buildkit/build/${params.CIVIHR_BUILDNAME}"
DRUPAL_SITES_ALL = "$WEBROOT/sites/all"
DRUPAL_MODULES_ROOT = "$DRUPAL_SITES_ALL/modules"
DRUPAL_THEMES_ROOT = "$DRUPAL_SITES_ALL/themes"
CIVICRM_EXT_ROOT = "$DRUPAL_MODULES_ROOT/civicrm/tools/extensions"
WEBURL = "http://jenkins.compucorp.co.uk:8900"
ADMIN_PASS = credentials('CVHR_ADMIN_PASS')
KARMA_TESTS_REPORT_FOLDER = "reports/js-karma"
PHPUNIT_TESTS_REPORT_FOLDER = "reports/phpunit"
}
stages {
stage('Pre-tasks execution') {
steps {
sendBuildStartNotification()
// Print all Environment variables
sh 'printenv | sort'
// Update buildkit
sh "cd /opt/buildkit && git pull"
// Destroy existing site
sh "civibuild destroy ${params.CIVIHR_BUILDNAME} || true"
// Test build tools
sh 'amp test'
// Cleanup old Karma test reports
sh "rm -f $WORKSPACE/$KARMA_TESTS_REPORT_FOLDER/* || true"
// Cleanup old PHPUnit test reports
sh "rm -f $WORKSPACE/$PHPUNIT_TESTS_REPORT_FOLDER/* || true"
}
}
stage('Build site') {
steps {
script {
// Build site with CV Buildkit
sh "civibuild create ${params.CIVIHR_BUILDNAME} --type drupal-clean --civi-ver 5.3.1 --url $WEBURL --admin-pass $ADMIN_PASS"
// Get target and PR branches name
def prBranch = env.CHANGE_BRANCH
def envBranch = env.CHANGE_TARGET ? env.CHANGE_TARGET : env.BRANCH_NAME
if (prBranch != null && prBranch.startsWith("hotfix-")) {
envBranch = 'master'
}
// Clone CiviHR
cloneCiviHRRepositories(envBranch)
if (prBranch) {
checkoutPrBranchInCiviHRRepos(prBranch)
mergeEnvBranchInAllRepos(envBranch)
}
applyCoreForkPatch()
// The JS tests use the cv tool to find the path of an extension.
// For it to work, the extensions have to be installed on the site
installCiviHRExtensions()
}
}
}
stage('Run tests') {
parallel {
stage('Test PHP') {
steps {
script {
for (item in mapToList(listCivihrExtensions())) {
def extension = item.value
if (extension.hasPHPTests) {
testPHPUnit(extension)
}
}
}
}
post {
always {
step([
$class: 'XUnitBuilder',
thresholds: [
[
$class: 'FailedThreshold',
failureNewThreshold: '0',
failureThreshold: '0',
unstableNewThreshold: '0',
unstableThreshold: '0'
]
],
tools: [
[
$class: 'JUnitType',
pattern: env.PHPUNIT_TESTS_REPORT_FOLDER + '/*.xml'
]
]
])
}
}
}
stage('Test JS') {
steps {
script {
installNodePackages();
// HRCore is the extension where the JS tests are ran from
def hrcore = listCivihrExtensions().hrcore;
// This is necessary to avoid an additional loop
// in each extension folder to read the XML.
// After each test we move the reports to this folder
sh "mkdir -p $WORKSPACE/$KARMA_TESTS_REPORT_FOLDER"
for (item in mapToList(listCivihrExtensions())) {
def extension = item.value
if (extension.hasJSTests) {
testJS(hrcore.folder, extension)
}
}
}
}
post {
always {
step([
$class: 'XUnitBuilder',
thresholds: [
[
$class: 'FailedThreshold',
failureNewThreshold: '0',
failureThreshold: '0',
unstableNewThreshold: '0',
unstableThreshold: '0'
]
],
tools: [
[
$class: 'JUnitType',
pattern: env.KARMA_TESTS_REPORT_FOLDER + '/*.xml'
]
]
])
}
}
}
}
}
}
post {
always {
// Destroy built site
script {
if (params.DESTROY_SITE == true) {
echo 'Destroying built site...'
sh "civibuild destroy ${params.CIVIHR_BUILDNAME} || true"
}
}
}
success {
sendBuildSuccessNotification()
}
failure {
sendBuildFailureNotification()
}
}
}
/*
* Sends a notification when the build starts
*/
def sendBuildStartNotification() {
def msgHipChat = 'Building ' + getBuildTargetLink('hipchat') + '. ' + getReportLink('hipchat')
def msgSlack = 'Building ' + getBuildTargetLink('slack') + '. ' + getReportLink('slack')
sendHipchatNotification('YELLOW', msgHipChat)
sendSlackNotification('warning', msgSlack)
}
/*
* Sends a notification when the build is completed successfully
*/
def sendBuildSuccessNotification() {
def msgHipChat = getBuildTargetLink('hipchat') + ' built successfully. Time: $BUILD_DURATION. ' + getReportLink('hipchat')
def msgSlack = getBuildTargetLink('slack') + ' built successfully. Time: ' + getBuildDuration(currentBuild) + '. ' + getReportLink('slack')
sendHipchatNotification('GREEN', msgHipChat)
sendSlackNotification('good', msgSlack)
}
/*
* Sends a notification when the build fails
*/
def sendBuildFailureNotification() {
def msgHipChat = 'Failed to build ' + getBuildTargetLink('hipchat') + '. Time: $BUILD_DURATION. No. of failed tests: ${TEST_COUNTS,var=\"fail\"}. ' + getReportLink('hipchat')
def msgSlack = 'Failed to build ' + getBuildTargetLink('slack') + '. Time: ' + getBuildDuration(currentBuild) + '. ' + getReportLink('slack')
sendHipchatNotification('RED', msgHipChat)
sendSlackNotification('danger', msgSlack)
}
/*
* Sends a notification to Hipchat
*/
def sendHipchatNotification(String color, String message) {
hipchatSend color: color, message: message, notify: true
}
/*
* Sends a notification to Slack
*/
def sendSlackNotification(String color, String message) {
slackSend color: color, message: message, notify: true
}
/*
* Returns the build duration without the "and counting" suffix
*/
def getBuildDuration(build) {
return build.durationString.replace(' and counting', '')
}
/*
* Returns a link to what is being built. If it's a PR, then it's a link to the pull request itself.
* If it's a branch, then it's a link in the format http://github.com/org/repo/tree/branch
*/
def getBuildTargetLink(String client) {
def link = ''
def forPR = buildIsForAPullRequest()
switch (client) {
case 'hipchat':
link = forPR ? "<a href=\"${env.CHANGE_URL}\">\"${env.CHANGE_TITLE}\"</a>" : '<a href="' + getRepositoryUrlForBuildBranch() + '">"' + env.BRANCH_NAME + '"</a>'
break;
case 'slack':
link = forPR ? "<${env.CHANGE_URL}|${env.CHANGE_TITLE}>" : '<' + getRepositoryUrlForBuildBranch() + '|' + env.BRANCH_NAME + '>'
break;
}
return link
}
/*
* Returns true if this build as triggered by a Pull Request.
*/
def buildIsForAPullRequest() {
return env.CHANGE_URL != null
}
/*
* Returns a URL pointing to branch currently being built
*/
def getRepositoryUrlForBuildBranch() {
def repositoryURL = env.GIT_URL
repositoryURL = repositoryURL.replace('.git', '')
return repositoryURL + '/tree/' + env.BRANCH_NAME
}
/*
* Returns the Blue Ocean build report URL for the current job
*/
def getReportLink(String client) {
def link = ''
switch (client) {
case 'hipchat':
link = 'Click <a href="$BLUE_OCEAN_URL">here</a> to see the build report'
break
case 'slack':
link = "Click <${env.RUN_DISPLAY_URL}|here> to see the build report"
break
}
return link
}
def cloneCiviHRRepositories(String envBranch) {
for (repo in listCivihrGitRepoPath()) {
sh """
git clone ${repo.url} ${repo.folder}
cd ${repo.folder}
git checkout $envBranch || true
"""
}
}
def checkoutPrBranchInCiviHRRepos(String branch) {
echo 'Checking out CiviHR repos..'
for (repo in listCivihrGitRepoPath()) {
sh """
cd ${repo.folder}
git checkout ${branch} || true
"""
}
}
def mergeEnvBranchInAllRepos(String envBranch) {
echo 'Merging env branch'
for (repo in listCivihrGitRepoPath()) {
sh """
cd ${repo.folder}
git merge origin/${envBranch} --no-edit || true
"""
}
}
/*
* Execute PHPUnit testing
* params: extension
*/
def testPHPUnit(java.util.LinkedHashMap extension) {
echo "PHPUnit testing: ${extension.name}"
sh """
cd $CIVICRM_EXT_ROOT/civihr/${extension.folder}
phpunit4 --testsuite="Unit Tests" --log-junit $WORKSPACE/$PHPUNIT_TESTS_REPORT_FOLDER/result-phpunit_${extension.folder}.xml
"""
}
/*
* Install Node packages in all of CiviHR extensions
*/
def installNodePackages() {
sh """
cd $CIVICRM_EXT_ROOT/civihr/
yarn || true
"""
}
/*
* Execute JS Testing
* params: hrcoreFolder
* params: extension
*/
def testJS(hrcoreFolder, java.util.LinkedHashMap extension) {
echo "JS Testing ${extension.name}"
// We cannot change, using CLI arguments, the place where
// karma stores the junit XML report, so the last command
// here copies the XML from the extension folder to the
// workspace, where Jenkins will read it
sh """
cd $CIVICRM_EXT_ROOT/civihr/${hrcoreFolder}
npx gulp test --ext ${extension.folder} --reporters junit,progress || true
cd $CIVICRM_EXT_ROOT/civihr/${extension.folder}
mv test-reports/*.xml $WORKSPACE/$KARMA_TESTS_REPORT_FOLDER/ || true
"""
}
/*
* Get a list of CiviHR repositories
* Note that these are NOT all the repositories used by CiviHR, but only
* those necessary to run the Unit Tests for the CiviHR repo
*/
def listCivihrGitRepoPath() {
return [
[
'url': 'https://github.com/compucorp/civihr.git',
'folder': "$CIVICRM_EXT_ROOT/civihr"
],
[
'url': 'https://github.com/compucorp/civihr-tasks-assignments.git',
'folder': "$CIVICRM_EXT_ROOT/civihr_tasks"
],
// These are not really dependencies for the tests, but both the shoreditch
// and the styleguide installation is hardcoded in drush-install.sh
// file and if the code cannot be found in the site, the installation will
// fail
[
'url': 'https://github.com/compucorp/org.civicrm.shoreditch.git',
'folder': "$CIVICRM_EXT_ROOT/org.civicrm.shoreditch"
],
[
'url': 'https://github.com/compucorp/org.civicrm.styleguide.git',
'folder': "$CIVICRM_EXT_ROOT/org.civicrm.styleguide"
]
]
}
/*
* Get a list of enabled CiviHR extensions
*/
def listCivihrExtensions() {
return [
hrjobroles: [
name: 'Job Roles',
folder: 'com.civicrm.hrjobroles',
hasJSTests: true,
hasPHPTests: true
],
contactaccessrights: [
name: 'Contacts Access Rights',
folder: 'contactaccessrights',
hasJSTests: true,
hasPHPTests: true
],
contactsummary: [
name: 'Contacts Summary',
folder: 'contactsummary',
hasJSTests: true,
hasPHPTests: false
],
hrjobcontract: [
name: 'Job Contracts',
folder: 'hrjobcontract',
hasJSTests: true,
hasPHPTests: true
],
hrreport: [
name: 'Reports',
folder: 'hrreport',
hasJSTests: false,
hasPHPTests: false
],
hrui: [
name: 'HR UI',
folder: 'hrui',
hasJSTests: false,
hasPHPTests: false
],
hrvisa: [
name: 'HR Visa',
folder: 'hrvisa',
hasJSTests: false,
hasPHPTests: true
],
reqangular: [
name: 'Reqangular',
folder: 'org.civicrm.reqangular',
hasJSTests: true,
hasPHPTests: false
],
hrcore: [
name: 'HRCore',
folder: 'uk.co.compucorp.civicrm.hrcore',
hasJSTests: false,
hasPHPTests: true
],
hrleaveandabsences: [
name: 'Leave and Absences',
folder: 'uk.co.compucorp.civicrm.hrleaveandabsences',
hasJSTests: true,
hasPHPTests: true
],
hrsampledata: [
name: 'Sample Data',
folder: 'uk.co.compucorp.civicrm.hrsampledata',
hasJSTests: false,
hasPHPTests: true
],
hremergency: [
name: 'Emergency Contacts ',
folder: 'org.civicrm.hremergency',
hasJSTests: false,
hasPHPTests: true
],
bootstrapcivihr: [
name: 'Bootstrap CiviHR',
folder: 'org.civicrm.bootstrapcivihr',
hasJSTests: false,
hasPHPTests: false
],
hrcontactactionsmenu: [
name: 'Contact Actions Menu',
folder: 'uk.co.compucorp.civicrm.hrcontactactionsmenu',
hasJSTests: false,
hasJSPackages: false,
hasPHPTests: true
]
]
}
/*
* Converts a Hashmap to a List
* This is mainly for supporting looping through the list of
* extensions returned by listCivihrExtensions()
* See this for more details:
* https://stackoverflow.com/questions/40159258/impossibility-to-iterate-over-a-map-using-groovy-within-jenkins-pipeline#40166064
*/
@NonCPS def mapToList(map) {
def list = []
for (def entry in map) {
list.add(new java.util.AbstractMap.SimpleImmutableEntry(entry.key, entry.value))
}
list
}
/*
* Installs the CiviHR extensions in the build site
*/
def installCiviHRExtensions() {
sh """
cd $CIVICRM_EXT_ROOT/civihr
drush cvapi extension.refresh
./bin/drush-install.sh
"""
}
/**
* Applies changes to CiviCRM from the Compucorp fork
*/
def applyCoreForkPatch() {
sh """
cd ${CIVICRM_EXT_ROOT}/civihr
./bin/apply-core-fork-patch.sh
"""
}