forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orchestrator.Jenkinsfile
207 lines (185 loc) · 7.85 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
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
#!/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
*/
def label = "kyma-${UUID.randomUUID().toString()}"
appVersion = "0.3." + env.BUILD_NUMBER
/*
Projects that are built when changed.
IMPORTANT NOTE: Projects trigger jobs and therefore are expected to have a job defined with the same name.
*/
projects = [
"docs",
"components/api-controller",
"components/binding-usage-controller",
"components/configurations-generator",
"components/environments",
"components/istio-webhook",
"components/helm-broker",
"components/remote-environment-broker",
"components/application-connector",
"components/gateway",
"components/installer",
"components/connector-service",
"components/ui-api-layer",
"components/event-bus",
"tools/alpine-net",
"tools/watch-pods",
"tools/stability-checker",
"tools/etcd-backup",
"tests/test-logging-monitoring",
"tests/acceptance",
"tests/ui-api-layer-acceptance-tests",
"tests/application-connector-tests",
"tests/gateway-tests",
"tests/test-environments",
"tests/kubeless-test-client",
"tests/api-controller-acceptance-tests",
"tests/connector-service-tests",
"tests/event-bus",
"governance"
]
/*
Projects that are NOT built when changed, but do trigger the kyma integration job.
*/
additionalProjects = ["resources","cluster","installation"]
/*
project jobs to run are stored here to be sent into the parallel block outside the node executor.
*/
jobs = [:]
/*
If true, Kyma integration will run at the end.
NOTE: This is set automaticlly based on the changes detected.
*/
runIntegration = false
properties([
buildDiscarder(logRotator(numToKeepStr: '10')),
disableConcurrentBuilds()
])
podTemplate(label: label) {
node(label) {
try {
timestamps {
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()
changes = changedProjects()
runIntegration = changes.size() > 0
if (changes.size() == 1 && changes[0] == "governance") {
runIntegration = false
}
}
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: "kyma/"+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
}
// trigger Kyma integration when changes are made to installation charts/code or resources
if (runIntegration) {
stage('launch Kyma integration') {
build job: 'kyma/integration',
wait: false,
parameters: [
string(name:'GIT_REVISION', value: "$commitID"),
string(name:'GIT_BRANCH', value: "${env.BRANCH_NAME}"),
string(name:'APP_VERSION', value: "$appVersion")
]
}
}
/* -------- 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() {
res = []
def allProjects = projects + additionalProjects
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++) {
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 (projects[i] == "governance" && allChanges[j].endsWith(".md") && !res.contains(projects[i])) {
res.add(projects[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
}