This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
javascript.groovy
445 lines (413 loc) · 13.4 KB
/
javascript.groovy
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
def getSource (os) {
run(os, 'git config --global core.autocrlf input')
checkout scm
}
// Helper to receive the value if it's not empty, or return a default value
def defVal (value, defaultValue) {
if (value == null || value == []) {
return defaultValue
} else {
if (value instanceof java.util.LinkedHashMap) {
return value + defaultValue
} else {
return value
}
}
}
// Installs custom node_modules
def installCustomModules (os, modules) {
if (modules != []) {
def modulesAsString = modules.collect {
it.key + "@" + it.value
}.join(" ")
run(os, 'npm install ' + modulesAsString)
}
}
def isWindows (os) {
return os == 'windows'
}
// Cross-platform sh/bat function
def run(os, cmd) {
if (isWindows(os)) {
bat(cmd)
} else {
sh(cmd)
}
}
// Same as `run` but with returnStdout
def runReturnStdout(os, cmd) {
if (isWindows(os)) {
return bat(script: cmd, returnStdout: true).trim()
} else {
return sh(script: cmd, returnStdout: true).trim()
}
}
// Same as `run` but with returnStatus
def runReturnStatus(os, cmd) {
if (isWindows(os)) {
return bat(script: cmd, returnStatus: true)
} else {
return sh(script: cmd, returnStatus: true)
}
}
// Checks if package.json contains a certain script
def packageHasScript(os, script) {
def unix = !isWindows(os)
def catCmd = unix ? 'cat' : 'type'
def grepCmd = unix ? 'grep' : 'FINDSTR'
def statusCode = runReturnStatus(os, catCmd + ' package.json | ' + grepCmd + ' ' + script)
return statusCode == 0
}
// Function to wrap calls that needs to cleanup after themselves
def postClean (f) {
ws { timeout(time: 1, unit: 'HOURS') {
try {
f()
} catch (err) {
throw err
} finally {
cleanWs()
}
}}
}
// Installs dependencies + custom_modules, can turn off/on scripts
def installDependencies (os, wantedNpmVersion, customModules, ignoreScripts = false) {
// TODO currently ignored, should be configured on the worker rather than pipeline
// currentVersion = run('npm --version')
// if (currentVersion != wantedNpmVersion) {
// run('npm install -g npm@' + wantedNpmVersion)
// }
if (isWindows(os)) {
bat 'npm config set msvs_version 2015 --global'
bat 'npm config set python c:\\python27\\python.exe --global'
}
if (ignoreScripts) {
run(os, 'npm install --ignore-scripts')
} else {
run(os, 'npm install')
}
installCustomModules(os, customModules)
}
// Function to wrap calls that might make step unstable rather than failing
// Used to run the tests because if tests fail, should be marked as unstable (tests failing)
// rather than failing (something went seriously wrong)
// TODO doesn't actually set the build to unstable but instead marks it as FAILING
// as a Jenkins bug prevents us from just marking one stage/node as unstable
// https://issues.jenkins-ci.org/browse/JENKINS-39203
def markUnstableIfFail (name, context, f) {
// should be something like ci/jenkins/windows/11.0.1/test:node
def ciContext = 'ci/jenkins/' + context
githubNotify(
description: name + ' in progress',
status: 'PENDING',
context: ciContext
)
try {
f()
githubNotify(
description: name + ' passed',
status: 'SUCCESS',
context: ciContext
)
} catch (err) {
githubNotify(
description: name + ' failed',
status: 'FAILURE',
context: ciContext
)
// throw err
currentBuild.result = 'UNSTABLE'
println err
}
// Uncomment once JENKINS-39203 is fixed
// try {
// f()
// } catch (err) {
// println err
// }
}
def collectTestResults (f) {
try {
f()
} catch (err) {
throw err
} finally {
junit allowEmptyResults: true, testResults: 'junit-report-*.xml'
}
}
// Runs common tests for a platform
def runTests (os, nodejsVersions) {
def hasNodeTests = false
def hasBrowserTests = false
def hasWebWorkerTests = false
// Need to allocate a node to fetch the stash
// TODO should refactor this to be able to do without a node
node(label: 'linux') {
def data = readVariableStash('variables')
hasNodeTests = data.hasNodeTests
hasBrowserTests = data.hasBrowserTests
hasWebWorkerTests = data.hasWebWorkerTests
}
def depsStash = 'deps-' + os + '-' + nodejsVersions[0]
if (!hasNodeTests && !hasBrowserTests && !hasWebWorkerTests) {
println "Found no tests"
currentBuild.result = 'FAILURE'
throw new Exception("Found no tests")
}
def steps = [:]
if (hasNodeTests) {
for (nodejsVersion in nodejsVersions) {
def version = nodejsVersion
def stepName = os + ':' + version + ' test:node'
def depsVersionStash = 'deps-' + os + '-' + version
def context = os + '/' + version + '/test:node'
steps[stepName] = {node(label: os) { postClean {
getSource(os)
retry (5) {
unstash depsVersionStash
}
nodejs(version) {
collectTestResults { markUnstableIfFail 'node tests', context, {
run(os, 'npm run test:node')
}}
}
}}}
}
}
if (hasBrowserTests) {
steps[os + ' test:browser'] = {node(label: os) { postClean {
getSource(os)
retry (5) {
unstash depsStash
}
nodejs(nodejsVersions[0]) {
def testCmd = 'npm run test:browser'
def context = os + '/test:browser'
if (os == 'linux') {
wrap([$class: 'Xvfb', parallelBuild: true, autoDisplayName: true]) {
collectTestResults { markUnstableIfFail 'browser tests', context, {
run(os, testCmd)
}}
}
} else {
collectTestResults { markUnstableIfFail 'browser tests', context, {
run(os, testCmd)
}}
}
}
}}}
}
if (hasWebWorkerTests) {
steps[os + ' test:webworker'] = {node(label: os) { postClean {
getSource(os)
retry (5) {
unstash depsStash
}
nodejs(nodejsVersions[0]) {
def testCmd = 'npm run test:webworker'
def context = os + '/test:webworker'
if (os == 'linux') {
wrap([$class: 'Xvfb', parallelBuild: true, autoDisplayName: true]) {
collectTestResults { markUnstableIfFail 'webworker tests', context, {
run(os, testCmd)
}}
}
} else {
collectTestResults { markUnstableIfFail 'webworker tests', context, {
run(os, testCmd)
}}
}
}
}}}
}
return steps
}
// Utility function to use with writeVariableStash
def createInitialJSONData () {
return readJSON(text: '{}')
}
def writeVariableStash (name, jsonData) {
writeJSON(file: name + '.jenkins.json', json: jsonData)
stash(name: 'json-' + name, includes: name + '.jenkins.json')
}
def readVariableStash (name) {
unstash('json-' + name)
return readJSON(file: name + '.jenkins.json')
}
def readVariableStash (name, variable) {
def data = readVariableStash(name)
return data[variable]
}
def call(opts = []) {
// Which NodeJS versions to use
def defaultNodeVersions = [
'10.11.0'
]
// Which OSes to test on
def osToTests = [
'macos',
'windows',
'linux'
]
def yarnVersion = '1.9.4'
def yarnPath = './node_modules/.bin/yarn --registry="https://registry.npmjs.com"'
def yarnInstallRetries = 3
def npmVersion = '6.4.1'
def nodejsVersions = defVal(opts['nodejs_versions'], defaultNodeVersions)
def customModules = defVal(opts['node_modules'], [])
// TODO should be automatically infered
def coverageEnabled = defVal(opts['coverage'], true)
// TODO linting should be infered as well
pipeline {
agent none
environment {
CI = true
}
options {
ansiColor('xterm')
// Enables retrying of failing stages
preserveStashes(buildCount: 20)
// TODO only shows up in classic UI, not Blue Ocean :(
timestamps()
}
stages {
// TODO currently run on each platform due to cross-platform issues, but
// should be able to run once on linux and shared on platforms
// stage('Fetch Source') {
// steps {
// script {
// def fetchSteps = [:]
// for (os in osToTests) {
// def currentOS = os
// fetchSteps[currentOS] = {
// node(label: currentOS) { postClean {
// run(currentOS, 'git config --global core.autocrlf input')
// checkout scm
// stash name: sourceStash, excludes: 'node_modules/**', useDefaultExcludes: false
// }}
// }
// }
// parallel(fetchSteps)
// }
// }
// }
stage('Check available scripts') {
steps {
script {
def os = 'linux'
node(label: os) { postClean {
getSource(os)
jsonData = createInitialJSONData()
jsonData.hasNodeTests = packageHasScript(os, 'test:node')
jsonData.hasBrowserTests = packageHasScript(os, 'test:browser')
jsonData.hasWebWorkerTests = packageHasScript(os, 'test:webworker')
jsonData.hasLinting = packageHasScript(os, 'lint')
jsonData.hasCoverage = packageHasScript(os, 'coverage')
writeVariableStash('variables', jsonData)
}}
}
}
}
// TODO should be able to run `npm install --ignore-scripts` on one
// platform (linux) and just post-install on each platform + version
// but npm behaves differently on each platform
stage('Install Dependencies') { steps { script {
def depsSteps = [:]
for (os in osToTests) {
for (nodejsVersion in nodejsVersions) {
def stepName = os + ' - ' + nodejsVersion
def depsStash = 'deps-' + os + '-' + nodejsVersion
def version = nodejsVersion
def currentOS = os
depsSteps[stepName] = {retry (5) { node(label: currentOS) { ws { postClean {
getSource(currentOS)
nodejs(version) {
if (isWindows(currentOS)) {
bat 'npm config set msvs_version 2015 --global'
}
installDependencies(currentOS, npmVersion, customModules)
stash name: depsStash, includes: 'node_modules/**', useDefaultExcludes: false
}
}}}}}
}
}
parallel depsSteps
}}}
stage('Checks') {
steps {
script {
def hasLinting = false
node(label: 'linux') { postClean {
def data = readVariableStash('variables')
hasLinting = data.hasLinting
}}
def os = 'linux'
def checksSteps = [:]
if (hasLinting) {
checksSteps['codelint'] = { node(label: os) { postClean {
getSource(os)
unstash 'deps-linux-' + nodejsVersions[0]
nodejs(nodejsVersions[0]) {
markUnstableIfFail 'code linting', 'codelint', {
run(os, 'npm run lint')
}
}
}}}
}
checksSteps['commitlint'] = { node(label: os) { postClean {
getSource(os)
nodejs(nodejsVersions[0]) {
sh 'npm install --no-lockfile @commitlint/config-conventional @commitlint/cli'
def commit = runReturnStdout(os, "git rev-parse remotes/origin/$BRANCH_NAME")
run(os, 'git remote set-branches origin master && git fetch')
markUnstableIfFail 'commit linting', 'commitlint', {
run(os, "./node_modules/.bin/commitlint --extends=@commitlint/config-conventional --from=remotes/origin/master --to=$commit")
}
}
}}}
parallel checksSteps
}
}
}
stage('Tests') {
steps {
script {
def linux = runTests('linux', nodejsVersions)
def macos = runTests('macos', nodejsVersions)
def windows = runTests('windows', nodejsVersions)
parallel(linux + macos + windows)
}
}
}
stage('Coverage') {
steps {
script {
def os = 'linux'
node(label: os) { postClean {
def data = readVariableStash('variables')
if (!data.hasCoverage) {
echo "This repository does not support code coverage"
return
}
getSource(os)
unstash 'deps-linux-' + nodejsVersions[0]
nodejs(nodejsVersions[0]) {
def repo = runReturnStdout(os, "git remote get-url origin | cut -d '/' -f 4,5 | cut -d '.' -f 1")
withCredentials([
string(
credentialsId: 'codecov-test-access-code',
variable: 'CODECOV_ACCESS_TOKEN'
)]) {
def codecovToken = runReturnStdout(os, "curl --silent \"https://codecov.io/api/gh/$repo?access_token=\$CODECOV_ACCESS_TOKEN\" | node -e \"let data = '';process.stdin.on('data', (d) => data = data + d.toString());process.stdin.on('end', () => console.log(JSON.parse(data).repo.upload_token));\"")
withEnv(["CODECOV_TOKEN=$codecovToken", "COVERAGE=true"]) {
markUnstableIfFail 'coverage', 'coverage', {
run(os, 'npm run coverage -- -u -p codecov')
}
}
}
}
}}}}
}
}
}
}