forked from y-kkamil/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orchestrator.Jenkinsfile
169 lines (151 loc) · 6.86 KB
/
orchestrator.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
#!/usr/bin/env groovy
/*
Monorepo root orchestrator: This Jenkinsfile runs the Jenkinsfiles of all subprojects based on the changes made and triggers kyma integration.
- checks for changes since last successful build on master and compares to master if on a PR.
- for every changed project, triggers related job async as configured in the seedjob.
- for every changed additional project, triggers the kyma integration job.
- passes info of:
- revision
- branch
- current app version (first 7 characters of git revision)
*/
def label = "console-${UUID.randomUUID().toString()}"
/*
Projects that are built when changed.
IMPORTANT NOTE: Projects trigger jobs and therefore are expected to have a job defined with the same name in the seed job.
*/
projects = [
"core",
"catalog",
"instances",
"brokers",
"content",
"components/react",
"tests",
"lambda",
"governance"
]
/*
project jobs to run are stored here to be sent into the parallel block outside the node executor.
*/
jobs = [:]
properties([
buildDiscarder(logRotator(numToKeepStr: '30')),
])
podTemplate(label: label) {
node(label) {
try {
timestamps {
timeout(time:5, unit:"MINUTES") {
ansiColor('xterm') {
stage("setup") {
checkout scm
// use HEAD of branch as revision, Jenkins does a merge to master commit before starting this script, which will not be available on the jobs triggered below
commitID = sh (
script: "git rev-parse origin/${env.BRANCH_NAME}",
returnStdout: true
).trim()
appVersion = commitID.substring(0,8)
committerEmail = sh (
script: 'git --no-pager show -s --format=\'%ae\'',
returnStdout: true
).trim()
changes = changedProjects(committerEmail)
}
stage('collect projects') {
buildableProjects = changes.intersect(projects) // only projects that have build jobs
echo "Collected the following projects with changes: $buildableProjects..."
for (int i=0; i < buildableProjects.size(); i++) {
def index = i
jobs["${buildableProjects[index]}"] = { ->
build job: "console/"+buildableProjects[index],
wait: true,
parameters: [
string(name:'GIT_REVISION', value: "$commitID"),
string(name:'GIT_BRANCH', value: "${env.BRANCH_NAME}"),
string(name:'APP_VERSION', value: "$appVersion")
]
}
}
}
}
}
}
} catch (ex) {
echo "Got exception: ${ex}"
currentBuild.result = "FAILURE"
def body = "${currentBuild.currentResult} ${env.JOB_NAME}${env.BUILD_DISPLAY_NAME}: on branch: ${env.BRANCH_NAME}. See details: ${env.BUILD_URL}"
emailext body: body, recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: "${currentBuild.currentResult}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
}
}
}
// trigger jobs for projects that have changes, in parallel
stage('build projects') {
parallel jobs
}
/* -------- Helper Functions -------- */
/**
* Provides a list with the projects that have changes within the given projects list.
* If no changes found, all projects will be returned.
*/
String[] changedProjects(String committerEmail) {
res = []
def allProjects = projects
echo "Looking for changes in the following projects: $allProjects."
// get all changes
allChanges = changeset().split("\n")
// if no changes build all projects
if (allChanges.size() == 0) {
echo "No changes found or could not be fetched, triggering all projects."
return allProjects
}
// parse changeset and keep only relevant folders -> match with projects defined
for (int i=0; i < allProjects.size(); i++) {
if (env.BRANCH_NAME == 'master' && committerEmail == "[email protected]" && allProjects[i] == "components/react") {
continue
}
for (int j=0; j < allChanges.size(); j++) {
if (allChanges[j].startsWith(allProjects[i]) && changeIsValidFileType(allChanges[j],allProjects[i]) && !res.contains(allProjects[i])) {
res.add(allProjects[i])
break // already found a change in the current project, no need to continue iterating the changeset
}
if (env.BRANCH_NAME != 'master' && allProjects[i] == "governance" && allChanges[j].endsWith(".md") && !res.contains(allProjects[i])) {
res.add(allProjects[i])
break // already found a change in one of the .md files, no need to continue iterating the changeset
}
}
}
return res
}
boolean changeIsValidFileType(String change, String project){
return !change.endsWith(".md") || "docs".equals(project);
}
/**
* Gets the changes on the Project based on the branch or an empty string if changes could not be fetched.
*/
@NonCPS
String changeset() {
// on branch get changeset comparing with master
if (env.BRANCH_NAME != 'master') {
echo "Fetching changes between origin/${env.BRANCH_NAME} and origin/master."
return sh (script: "git --no-pager diff --name-only origin/master...origin/${env.BRANCH_NAME} | grep -v 'vendor\\|node_modules' || echo ''", returnStdout: true)
}
// on master get changeset since last successful commit
else {
echo "Fetching changes on master since last successful build."
def successfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()
if (successfulBuild) {
def commit = commitHashForBuild(successfulBuild)
return sh (script: "git --no-pager diff --name-only $commit 2> /dev/null | grep -v 'vendor\\|node_modules' || echo ''", returnStdout: true)
}
}
return ""
}
/**
* Gets the commit hash from a Jenkins build object
*/
@NonCPS
def commitHashForBuild(build) {
def scmAction = build?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }
return scmAction?.revision?.hash
}