forked from kyma-project/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorchestrator.Jenkinsfile
181 lines (160 loc) · 6.96 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
#!/usr/bin/env groovy
import groovy.json.JsonOutput
/*
Monorepo root orchestrator: This Jenkinsfile runs the Jenkinsfiles of all subprojects based on the changes made.
- 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.
- passes info of:
- revision
- branch
- current app version
*/
def label = "kyma-${UUID.randomUUID().toString()}"
appVersion = "0.1." + 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 = [
"http-db-service",
"event-email-service",
"tests/http-db-service",
"call-ec",
"governance",
"monitoring-custom-metrics",
"example-tracing"
]
/*
Projects that are NOT built when changed, but do trigger the tests.
*/
additionalProjects = ["event-subscription/lambda"]
/*
project jobs to run are stored here to be sent into the parallel block outside the node executor.
*/
jobs = [:]
runTests = false
properties([
buildDiscarder(logRotator(numToKeepStr: '10')),
])
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()
changes = changedProjects()
runTests = changes.size() > 0
if (changes.size() == 1 && changes[0] == "governance") {
runTests = 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: "examples/"+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
}
if (runTests) {
stage('run tests for the examples') {
// convert changes to JSON string to pass on
changes = JsonOutput.toJson(changes)
build job: 'examples/tests/examples',
wait: true,
parameters: [
string(name:'GIT_REVISION', value: "$commitID"),
string(name:'GIT_BRANCH', value: "${env.BRANCH_NAME}"),
string(name:'APP_VERSION', value: "$appVersion"),
string(name:'CHANGED_EXAMPLES', value: "$changes")
]
}
}
/* -------- Helper Functions -------- */
/**
* Provides a list with the projects that have changes within the given projects list.
*/
String[] changedProjects() {
res = []
def allProjects = projects + additionalProjects
echo "Looking for changes in the following projects: $projects."
// get all changes
allChanges = changeset().split("\n")
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 (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
*/
@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
}