forked from SpareBank1/monorepo-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
82 lines (60 loc) · 2.06 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
node {
def scmvars = checkout(scm)
def commitHash = scmvars.GIT_COMMIT
def mavenOptions = "-Drevision=${commitHash}"
env.MAVEN_OPTS="${mavenOptions}"
try {
stage('Determine Jenkinsfile to build') {
def sout = sh(returnStdout: true, script: 'git diff --name-only origin/master...HEAD')
def j = findJenkinsfileToRun(sout.split())
if (j.toString() == "${pwd()}/Jenkinsfile") {
println("Building the whole world")
load "Jenkinsfile-build-the-world"
} else {
println("Building ${j.toString()}")
load "${j.toString()}"
}
}
currentBuild.result = 'SUCCESS'
} catch (err) {
println("ERR: ${err}")
currentBuild.result = 'FAILED'
}
}
@NonCPS
def createFilePath(def path) {
if (env['NODE_NAME'].equals("master")) {
File localPath = new File(path)
return new hudson.FilePath(localPath);
} else {
return new hudson.FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), path);
}
}
@NonCPS
def findMostSpecificJenkinsFile(filePath) {
if (filePath.name == "Jenkinsfile" && ! filePath.isDirectory()) {
return filePath;
}
def potentialJenkinsFile=createFilePath("${filePath.getParent().toString()}/Jenkinsfile")
if (potentialJenkinsFile.exists()) {
return potentialJenkinsFile
}
return findMostSpecificJenkinsFile(filePath.getParent())
}
@NonCPS
def findJenkinsfileToRun(paths) {
def foundJenkinsFiles = []
println("Finding most specific Jenkinsfile for changed files:\n${paths.join('\n')}")
for (path in paths) {
def f = createFilePath("${pwd()}/${path}")
def j = findMostSpecificJenkinsFile(f)
foundJenkinsFiles = foundJenkinsFiles << j
}
println("Selecting from:\n${foundJenkinsFiles.join('\n')}")
foundJenkinsFiles.unique()
if (foundJenkinsFiles.size() == 1) {
return foundJenkinsFiles.get(0)
} else {
return "${pwd()}/Jenkinsfile"
}
}