-
Notifications
You must be signed in to change notification settings - Fork 10
/
Jenkinsfile
116 lines (108 loc) · 3.6 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
def buildAll = false
def affectedModules = []
def affectedList
def goal = "package"
pipeline {
agent any
//disable concurrent build to avoid race conditions and to save resources
options {
disableConcurrentBuilds()
}
//load tools - these should be configured in jenkins global tool configuration
tools {
maven 'MAVEN_3'
jdk 'OPENJDK_11'
}
stages {
//this stage will get all the files that were modified
stage("get diff") {
steps {
script {
def changes = []
if(env.CHANGE_ID) { //check if triggered via Pull Request
echo "Pull Request Trigger"
//get changes via git diff so we can know which module should be built
if (isUnix()) {
changes = sh(returnStdout: true, script: "git --no-pager diff origin/${CHANGE_TARGET} --name-only").trim().split()
}
else {
changes = bat(returnStdout: true, script: "git --no-pager diff origin/${CHANGE_TARGET} --name-only").trim().split()
}
//use compile goal instead of package if the trigger came from Pull Request. we dont want to package our module for every pull request
goal = "compile"
} else if(currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause').size() > 0) { //check if triggered via User 'Build Now'
echo "User Trigger"
buildAll = true
} else { //defaults to Push Trigger
echo "Push Trigger"
//get changes via changelogs so we can know which module should be built
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
def files = new ArrayList(entry.affectedFiles)
for (int k = 0; k < files.size(); k++) {
def file = files[k]
changes.add(file.path)
}
}
}
}
//iterate through changes
changes.each {c ->
if(c.contains("common") || c == "pom.xml") { //if changes includes parent pom.xml and common module, we should build all modules
affectedModules = []
buildAll = true
return
}else {
if(c.indexOf("/") > 1) { //filter all affected module. indexOf("/") means the file is inside a subfolder (module)
affectedModules.add(c.substring(0,c.indexOf("/")))
}
}
}
}
}
}
//this stage will build all if the flag buildAll = true
stage("build all") {
when {
expression {
return buildAll
}
}
steps {
script {
//goal = install | compile
// -T 5 means we can build modules in parallel using 5 Threads, we can scale this
if (isUnix()) {
sh "mvn clean ${goal} -B -DskipTests -Pbuild -T 5"
} else {
bat "mvn clean ${goal} -B -DskipTests -Pbuild -T 5"
}
}
}
}
//this stage will build the affected modules only if affectedModules.size() > 0
stage("build modules") {
when {
expression {
return affectedModules.size() > 0
}
}
steps {
script {
//remove duplicate items and separate them using "," delimeter
affectedList = affectedModules.unique().join(",")
//goal = install | compile
// -T 5 means we can build modules in parallel using 5 Threads, we can scale this
if (isUnix()) {
sh "mvn clean ${goal} -B -pl ${affectedList} -am -DskipTests -Pbuild -T 5"
} else {
bat "mvn clean ${goal} -B -pl ${affectedList} -am -DskipTests -Pbuild -T 5"
}
}
}
}
}
}