This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Jenkinsfile
65 lines (52 loc) · 1.52 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
def distributedTasks = [:]
stage("Building Distributed Tasks") {
jsTask {
checkout scm
sh 'yarn install'
distributedTasks << distributed('test', 3)
distributedTasks << distributed('lint', 3)
distributedTasks << distributed('build', 3)
}
}
stage("Run Distributed Tasks") {
parallel distributedTasks
}
def jsTask(Closure cl) {
node {
withEnv(["HOME=${workspace}"]) {
docker.image('node:latest').inside('--tmpfs /.config', cl)
}
}
}
def distributed(String target, int bins) {
def jobs = splitJobs(target, bins)
def tasks = [:]
jobs.eachWithIndex { jobRun, i ->
def list = jobRun.join(',')
def title = "${target} - ${i}"
tasks[title] = {
jsTask {
stage(title) {
checkout scm
sh 'yarn install'
sh "npx nx run-many --target=${target} --projects=${list} --parallel"
}
}
}
}
return tasks
}
def splitJobs(String target, int bins) {
def String baseSha = env.CHANGE_ID ? 'origin/master' : 'origin/master~1'
def String raw
raw = sh(script: "npx nx print-affected --base=${baseSha} --target=${target}", returnStdout: true)
def data = readJSON(text: raw)
def tasks = data['tasks'].collect { it['target']['project'] }
if (tasks.size() == 0) {
return tasks
}
// this has to happen because Math.ceil is not allowed by jenkins sandbox (╯°□°)╯︵ ┻━┻
def c = sh(script: "echo \$(( ${tasks.size()} / ${bins} ))", returnStdout: true).toInteger()
def split = tasks.collate(c)
return split
}