-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathJenkinsfile.setup-branch
262 lines (225 loc) · 7.81 KB
/
Jenkinsfile.setup-branch
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_
// Map of executed jobs
// See https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html
// for more options on built job entity
JOBS = [:]
FAILED_STAGES = [:]
UNSTABLE_STAGES = [:]
pipeline {
agent {
label util.avoidFaultyNodes('ubuntu')
}
options {
timeout(time: 360, unit: 'MINUTES')
}
environment {
KOGITO_CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}")
}
stages {
stage('Initialize') {
steps {
script {
currentBuild.displayName = getKogitoVersion()
}
}
}
stage('Init Kogito Runtimes') {
steps {
script {
def buildParams = getDefaultBuildParams()
addDroolsVersionParam(buildParams)
addKogitoVersionParam(buildParams)
buildJob('kogito-runtimes', buildParams)
}
}
post {
failure {
addFailedStage('kogito-runtimes')
}
}
}
stage('Init Kogito Apps') {
steps {
script {
def buildParams = getDefaultBuildParams()
addKogitoVersionParam(buildParams)
buildJob('kogito-apps', buildParams)
}
}
post {
failure {
addFailedStage('kogito-apps')
}
}
}
stage('Init Kogito Examples') {
steps {
script {
def buildParams = getDefaultBuildParams()
addDroolsVersionParam(buildParams)
addKogitoVersionParam(buildParams)
buildJob('kogito-examples', buildParams)
if (isJobSucceeded('kogito-examples') || isJobUnstable('kogito-examples')) {
// Update examples nightly branch
dir('kogito-examples') {
deleteDir()
checkout(githubscm.resolveRepository('incubator-kie-kogito-examples', getGitAuthor(), getBuildBranch(), false, getGitAuthorCredsId()))
String nightlyBranch = "nightly-${getBuildBranch()}"
githubscm.createBranch(nightlyBranch)
githubscm.pushObject('origin', nightlyBranch, getGitAuthorPushCredsId())
}
}
}
}
post {
failure {
addFailedStage('kogito-examples')
}
}
}
stage('Init Docs') {
when {
expression { return isMainStream() }
}
steps {
script {
def buildParams = getDefaultBuildParams()
buildJob('kogito-docs', buildParams)
}
}
post {
failure {
addFailedStage('kogito-docs')
}
}
}
// Launch the nightly to deploy all artifacts from the branch
stage('Launch the nightly') {
when {
expression { return params.DEPLOY }
}
steps {
script {
def buildParams = getDefaultBuildParams()
addBooleanParam(buildParams, 'SKIP_TESTS', true)
build(job: '../nightly/0-kogito-nightly', wait: false, parameters: buildParams, propagate: false)
}
}
}
// Launch the weekly to deploy all artifacts from the branch
stage('Launch the weekly') {
when {
expression { return params.DEPLOY }
}
steps {
script {
def buildParams = getDefaultBuildParams()
addBooleanParam(buildParams, 'SKIP_TESTS', true)
build(job: '../other/0-kogito-weekly', wait: false, parameters: buildParams, propagate: false)
}
}
}
}
post {
unsuccessful {
sendPipelineErrorNotification()
}
}
}
def buildJob(String jobName, List buildParams, String jobKey = jobName, boolean waitForJob = true) {
echo "[${jobKey}] Build ${jobName} with params ${buildParams}"
def job = build(job: "${jobName}", wait: waitForJob, parameters: buildParams, propagate: false)
JOBS[jobKey] = job
// Set Unstable if job did not succeed
if (waitForJob && !isJobSucceeded(jobKey)) {
addUnstableStage(jobKey)
unstable("Job ${jobName} finished with result ${job.result}")
}
return job
}
def getJob(String jobKey) {
return JOBS[jobKey]
}
String getJobUrl(String jobKey) {
echo "getJobUrl for ${jobKey}"
return getJob(jobKey)?.absoluteUrl ?: ''
}
boolean isJobSucceeded(String jobKey) {
return getJob(jobKey)?.result == 'SUCCESS'
}
boolean isJobUnstable(String jobKey) {
return getJob(jobKey)?.result == 'UNSTABLE'
}
void addFailedStage(String jobKey = '') {
FAILED_STAGES.put("${STAGE_NAME}", jobKey)
}
void addUnstableStage(String jobKey = '') {
UNSTABLE_STAGES.put("${STAGE_NAME}", jobKey)
}
void sendPipelineErrorNotification() {
String bodyMsg = "Kogito setup branch job #${BUILD_NUMBER} was: ${currentBuild.currentResult}"
if (FAILED_STAGES.size()) {
bodyMsg += '\nFailed stages: \n- '
bodyMsg += FAILED_STAGES.collect { "${it.key} => ${getJobUrl(it.value)}" }.join('\n- ')
}
bodyMsg += '\n'
if (UNSTABLE_STAGES.size()) {
bodyMsg += '\nUnstable stages: \n- '
bodyMsg += UNSTABLE_STAGES.collect { "${it.key} => ${getJobUrl(it.value)}" }.join('\n- ')
}
bodyMsg += '\n'
bodyMsg += "\nPlease look here: ${BUILD_URL}"
emailext body: bodyMsg, subject: "[${getBuildBranch()}][d] Setup branch",
to: env.KOGITO_CI_EMAIL_TO
}
List getDefaultBuildParams() {
List buildParams = []
addStringParam(buildParams, 'DISPLAY_NAME', getKogitoVersion())
addBooleanParam(buildParams, 'SEND_NOTIFICATION', true)
return buildParams
}
void addDroolsVersionParam(buildParams) {
addStringParam(buildParams, 'DROOLS_VERSION', getDroolsVersion())
}
void addKogitoVersionParam(buildParams) {
addStringParam(buildParams, 'KOGITO_VERSION', getKogitoVersion())
}
void addStringParam(List params, String key, String value) {
params.add(string(name: key, value: value))
}
void addBooleanParam(List params, String key, boolean value) {
params.add(booleanParam(name: key, value: value))
}
String getBuildBranch() {
return env.GIT_BRANCH_NAME
}
String getGitAuthor() {
return env.GIT_AUTHOR
}
String getGitAuthorCredsId() {
return env.GIT_AUTHOR_CREDS_ID
}
String getGitAuthorPushCredsId() {
return env.GIT_AUTHOR_PUSH_CREDS_ID
}
String getDroolsVersion() {
return params.DROOLS_VERSION ?: getVersionFromReleaseBranch(getBuildBranch())
}
String getKogitoVersion() {
return params.KOGITO_VERSION ?: getVersionFromReleaseBranch(getBuildBranch())
}
String getVersionFromReleaseBranch(String releaseBranch, int microVersion = 999, String suffix = 'SNAPSHOT') {
String [] versionSplit = releaseBranch.split("\\.")
if (versionSplit.length == 3
&& versionSplit[0].isNumber()
&& versionSplit[1].isNumber()
&& versionSplit[2] == 'x') {
return "${versionSplit[0]}.${versionSplit[1]}.${microVersion}${suffix ? '-' + suffix : ''}"
} else {
error 'Cannot parse given branch as a release branch, aka [M].[m].x ...'
}
}
boolean isMainStream() {
return env.STREAM == 'main'
}