This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Jenkinsfile
440 lines (381 loc) · 19.9 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Credentials Required:
*
* github-automation-user
* github-automation-email
* github-user-auth-token
* dh_trigger_token_openshift_spark
* dh_trigger_token_sti_scala
* dh_trigger_token_sti_pysprk
* dh_trigger_token_sti_pysprk_36
* dh_trigger_token_sti_java
* dh_trigger_token_sti_rspark
* dh_trigger_token_oc_proxy
* dh_trigger_token_oshinko_webui
* dh_trigger_token_oshinko_cli
*
*/
""" Parameters required for the build. """
Map<String, String> BUILD_PARAMETERS = [
"SPARK_VERSION": SPARK_VERSION,
"OSHINKO_VERSION": OSHINKO_VERSION,
"cliReleaseNotes": cliReleaseNotes,
"stiReleaseSummary": stiReleaseSummary,
"githubRepositories": githubRepositories,
"dockerhubRepositories": dockerhubRepositories,
"autobuildStageRetryCount": autobuildStageRetryCount,
"repoWatchInterval": repoWatchInterval,
"watchRetryCount": watchRetryCount,
"githubRepoOwner": githubRepoOwner,
"dockerhubRepoOwner": dockerhubRepoOwner,
"stiMergeToBranch": stiMergeToBranch,
]
private void prepareWorkspace() {
// wipeout workspace
deleteDir()
sh('concreate --version')
}
private void repoCtrl() { repoCtrl(-1) }
// Note stage is not a jenkins stage, but an actual step within the repoctrl script
private void repoCtrl(int stage){
String additionalArgs = " "
if (stage >= 0) { additionalArgs += " -s ${stage} " as String }
tmpdir = sh returnStdout: true, script: 'echo `mktemp -d`'
tmpdir = tmpdir.trim()
try {
GString args = "${GH_REPO_OWNER} ${GH_REPO} ${GH_AUTH_TOKEN} ${tmpdir} ${OSHINKO_VERSION} " +
"${CURRENT_PROJECT} ${GH_USER} ${GH_USER_EMAIL}"
sh("${REPO_CTRL}${additionalArgs}${args}")
} catch (err) {
throw err
} finally {
// Delete tmpdir
dir(tmpdir){ deleteDir() }
}
}
private void watchAutoBuildStage(String sourceTag, String credentialsId){
watchAutoBuildStage(sourceTag, "", credentialsId)
}
private void watchAutoBuildStage(String sourceTag, String sourceBranch, String credentialsId){
stage("watch-autobuilds: ${CURRENT_PROJECT}") {
retry(STAGE_RETRY_COUNT as Integer) {
withCredentials([string(credentialsId: "${credentialsId}", variable: 'TRIGGER_TOKEN')]) {
String additionalArgs = ""
if(sourceBranch){ additionalArgs += " -b ${sourceBranch}" as String }
if(sourceTag){ additionalArgs += " -t ${sourceTag}" as String }
try {
sh("${BUILD_WATCHER} ${DH_REPO_OWNER}/${DH_REPO} ${TRIGGER_TOKEN} " +
"-r ${RETRY_COUNT} " +
"-i ${INTERVAL}" +
"${additionalArgs}")
} catch (err) {
sleep(time: 5, unit: 'SECONDS')
throw err
}
}
}
}
}
private void validateParameters(Map<String, String> ghRepos, Map<String, String> dhRepos, Map<String, String> parameters) {
parameters.each{ p, value ->
if(!value){
error "${p} parameter not specified"
}
}
String[] ghProjects = ['openshift-spark', 'oshinko-cli', 'oshinko-webui', 'oshinko-s2i', 'oc-proxy']
for(String project in ghProjects){
if(!ghRepos.containsKey(project)){
error "${project} key was not specified for github repository parameter."
}
}
String[] dhProjects = ['openshift-spark', 'oshinko-cli', 'oshinko-webui',
'oc-proxy', 'oshinko-s2i-scala', 'oshinko-s2i-pyspark',
'oshinko-s2i-java', 'oshinko-s2i-pyspark-36', 'oshinko-s2i-r-spark']
for(String project in dhProjects){
if(!dhRepos.containsKey(project)){
error "${project} key was not specified for dockerhub repository parameter."
}
}
}
private Map<String, String> parseReposParameter(String data){
if(!data){
error "Repos parameter is empty, please re-do the build with all required parameters."
}
echo data
String repos = "[${data.trim()}]" as String
repos = repos.replace("\n", ", ")
repos = repos.replaceAll (/([\w-]+)/) { m -> "\"${m[0]}\"" }
echo repos
return evaluate(repos) as Map<String, String>
}
Closure<String> createTag = { version -> "v${version}" as String }
String GH_REPO_OWNER = params.githubRepoOwner
String DH_REPO_OWNER = params.dockerhubRepoOwner
String STI_MERGE_TO_BRANCH = params.stiMergeToBranch
String STI_PR_TITLE = params.stiPrTitle
String STI_PR_BODY = params.stiPrBody
String DOCKERHUB_ENDPOINT = "https://hub.docker.com/r"
String GITHUB_ENDPOINT = "https://github.com"
String GH_RELEASE_DIR = "src/github.com/${GH_REPO_OWNER}/oshinko-release" as String
String STI_CI_CONTEXTS = params.stiIntegrationContexts
String STI_BASE_BRANCH = "release${OSHINKO_VERSION}" as String
String OSHINKO_CLI_JOB = params.oshinkoCliJobName
// External Scripts used
String BUILD_WATCHER = "./watch_builds.py"
String REPO_CTRL = "./util/bash_scripts/repo_ctrl.sh"
String REL_FILE_GENERATOR = "./create_release_file.py"
String REL_GENERATOR = "./git_release.py"
String PR_AND_MERGE_HANDLER = "./git_create_pr.py"
Map<String, String> GH_REPOS = parseReposParameter(params.githubRepositories as String)
Map<String, String> DH_REPOS = parseReposParameter(params.dockerhubRepositories as String)
int AUTOBUILD_STAGE_RETRY_COUNT = params.autobuildStageRetryCount as Integer
int WATCH_INTERVAL_DEFAULT = params.repoWatchInterval as Integer
int WATCH_RETRY_COUNT_DEFAULT = params.watchRetryCount as Integer
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'radly-qe-github',
usernameVariable: 'GH_USER', passwordVariable: 'GH_AUTH_TOKEN'],
string(credentialsId: 'github-automation-email', variable: 'GH_USER_EMAIL')]) {
withEnv(["REPO_CTRL=${REPO_CTRL}", "DH_REPO_OWNER=${DH_REPO_OWNER}",
"GH_REPO_OWNER=${GH_REPO_OWNER}", "GH_RELEASE_DIR=${GH_RELEASE_DIR}",
"REPO_CTRL=${REPO_CTRL}", "BUILD_WATCHER=${BUILD_WATCHER}",
"RETRY_COUNT=${WATCH_RETRY_COUNT_DEFAULT}", "INTERVAL=${WATCH_INTERVAL_DEFAULT}",
"STAGE_RETRY_COUNT=${AUTOBUILD_STAGE_RETRY_COUNT}",
"OSHINKO_CLI_JOB=${OSHINKO_CLI_JOB}"]){
validateParameters(GH_REPOS, DH_REPOS, BUILD_PARAMETERS)
String stageOptions = params.stageOptions
String[] stageOptionsList = stageOptions.split(',')
sh "pwd"
sh "ls -lat"
// Openshift Spark Stages
String osProject = "openshift-spark"
withEnv(["CURRENT_PROJECT=${osProject}", "GH_REPO=${GH_REPOS.get(osProject)}"]){
String sourceTag
String sourceBranch
if('openshift-spark-github-tag-push' in stageOptionsList){
echo "Starting openshift-spark-github-tag-push stage...."
stage("update: ${CURRENT_PROJECT}") {
echo "Updating ${CURRENT_PROJECT}...."
String tmpdir = sh returnStdout: true, script: 'echo `mktemp -d`'
tmpdir = tmpdir.trim()
try {
GString args = "${GH_REPO_OWNER} ${GH_REPO} ${GH_AUTH_TOKEN} ${tmpdir} ${SPARK_VERSION} " +
"${CURRENT_PROJECT} ${GH_USER} ${GH_USER_EMAIL}"
sh(REPO_CTRL + " ${args}")
ghInfoFile = "${tmpdir}/project_report/gh_info.txt"
def exists = fileExists ghInfoFile
if (exists) {
String file = readFile file: ghInfoFile
String[] tagAndBranch = file.split()
sourceTag = tagAndBranch[0]
sourceBranch = tagAndBranch[1]
if (!sourceBranch || !sourceTag) {
throw GroovyRuntimeException("Source tag and/or branch were not successfully extracted from openshift-spark repo.")
}
} else {
throw GroovyRuntimeException('File containing github tag/branch name for openshift-spark repo not found.')
}
} catch (err) {
throw err
} finally {
// Delete tmpdir
dir(tmpdir){ deleteDir() }
}
}
}
if('openshift-spark-watch-autobuild' in stageOptionsList){
echo "Starting openshift-spark-watch-autobuild stage...."
withEnv(["DH_REPO=${DH_REPOS.get(CURRENT_PROJECT)}"]){
watchAutoBuildStage(sourceTag, sourceBranch, 'dh_trigger_token_openshift_spark')
}
}
}
// Oshinko Cli Stages
String cliProject = "oshinko-cli"
withEnv(["CURRENT_PROJECT=${cliProject}", "GH_REPO=${GH_REPOS.get(cliProject)}"]) {
if('oshinko-cli-github-create-release' in stageOptionsList){
echo "Starting oshinko-cli-github-create-release stage...."
stage("update: ${CURRENT_PROJECT}") {
String cliJob = "${OSHINKO_CLI_JOB}" as String
String tmpdir = sh returnStdout: true, script: 'echo `mktemp -d`'
tmpdir = tmpdir.trim()
try {
build job: cliJob, parameters: [string(name: 'VERSION', value: "v${OSHINKO_VERSION}")], propagate: true, wait: true
copyArtifacts(projectName: cliJob, target: tmpdir)
String artifactsDir = "${tmpdir}/src/github.com/${GH_REPO_OWNER}/${GH_REPO}/bin" as String
String releaseBody = sh returnStdout: true, script: "echo `mktemp release_notes_XXXXXXXXX`"
String releaseFile = sh returnStdout: true, script: "echo `mktemp release_XXXXXXXXX`"
releaseBody = releaseBody.trim()
releaseFile = releaseFile.trim()
writeFile file: releaseBody, text: params.cliReleaseNotes
cwd = pwd()
// Create the release file
sh("${REL_FILE_GENERATOR} ${cwd}/${releaseBody} ${OSHINKO_VERSION} " +
"${cwd}/${releaseFile} -a ${artifactsDir}")
// Generate release
sh("${REL_GENERATOR} ${GH_REPO_OWNER}/${GH_REPO} " +
"-c ${cwd}/${releaseFile} -a ${GH_AUTH_TOKEN} -u ${GH_USER}")
sh("rm ${releaseFile}")
sh("rm ${releaseBody}")
} finally {
dir(tmpdir) { deleteDir() }
}
}
}
if('oshinko-cli-watch-autobuild' in stageOptionsList){
echo "Starting oshinko-cli-watch-autobuild stage...."
withEnv(["DH_REPO=${DH_REPOS.get(CURRENT_PROJECT)}"]) {
String sourceTag = createTag(OSHINKO_VERSION)
watchAutoBuildStage(sourceTag, 'dh_trigger_token_oshinko_cli')
}
}
}
// Oshinko Webui Stages
String webuiProject = "oshinko-webui"
withEnv(["CURRENT_PROJECT=${webuiProject}", "GH_REPO=${GH_REPOS.get(webuiProject)}"]) {
if('oshinko-webui-github-tag-push' in stageOptionsList){
echo "Starting oshinko-webui-github-tag-push stage...."
stage("update: ${CURRENT_PROJECT}") {
echo "Updating ${CURRENT_PROJECT}...."
repoCtrl()
}
}
if('oshinko-webui-watch-autobuild' in stageOptionsList){
echo "Starting oshinko-webui-watch-autobuild stage...."
withEnv(["DH_REPO=${DH_REPOS.get(CURRENT_PROJECT)}"]) {
String sourceTag = createTag(OSHINKO_VERSION)
watchAutoBuildStage(sourceTag, 'dh_trigger_token_oshinko_cli')
}
}
}
// Oshinko S2I Stages
String stiProject = "oshinko-s2i"
withEnv(["CURRENT_PROJECT=${stiProject}", "GH_REPO=${GH_REPOS.get(stiProject)}"]){
if('oshinko-s2i-github-release-branch' in stageOptionsList){
echo "Starting oshinko-s2i-github-release-branch stage...."
stage("create release branch: ${CURRENT_PROJECT}") {
echo "Creating release branch for ${CURRENT_PROJECT}...."
repoCtrl(0)
}
}
if('oshinko-s2i-github-create-and-merge-pr' in stageOptionsList){
stage("create-merge-pull-request: ${CURRENT_PROJECT}") {
echo "Starting oshinko-s2i-github-create-and-merge-pr stage...."
echo "Creating pr to watch then performing merge for ${CURRENT_PROJECT}..."
String ghRepo = GH_REPOS.get(CURRENT_PROJECT)
sh("${PR_AND_MERGE_HANDLER} " +
"${GH_REPO_OWNER}/${ghRepo} " +
"${GH_AUTH_TOKEN} ${OSHINKO_VERSION} " +
"${GH_USER} ${STI_BASE_BRANCH} " +
"-hd ${STI_MERGE_TO_BRANCH} " +
"-i ${INTERVAL} " +
"-r ${RETRY_COUNT} " +
"-s ${STI_CI_CONTEXTS} " +
"-t \"${STI_PR_TITLE}\" " +
"-b \"${STI_PR_BODY}\"")
}
}
if('oshinko-s2i-github-tag-push' in stageOptionsList){
echo "Starting oshinko-s2i-github-tag-push stage...."
stage("create-tag: ${CURRENT_PROJECT}") {
echo "Creating tag for ${CURRENT_PROJECT}..."
repoCtrl(1)
}
}
if('oshinko-s2i-watch-autobuild' in stageOptionsList){
echo "Starting oshinko-s2i-watch-autobuild stage...."
stage("watch-autobuilds: ${CURRENT_PROJECT}"){
String sourceTag = createTag(OSHINKO_VERSION)
parallel (
'java-spark': {
withEnv(["DH_REPO=${DH_REPOS.get("oshinko-s2i-java")}"]) {
watchAutoBuildStage(sourceTag, 'dh_trigger_token_sti_java')
}
},
'py-spark': {
withEnv(["DH_REPO=${DH_REPOS.get("oshinko-s2i-pyspark")}"]) {
watchAutoBuildStage(sourceTag, 'dh_trigger_token_sti_pysprk')
}
},
'scala-spark': {
withEnv(["DH_REPO=${DH_REPOS.get("oshinko-s2i-scala")}"]) {
watchAutoBuildStage(sourceTag, 'dh_trigger_token_sti_scala')
}
},
'py-spark-36': {
withEnv(["DH_REPO=${DH_REPOS.get("oshinko-s2i-pyspark-36")}"]) {
watchAutoBuildStage(sourceTag, 'dh_trigger_token_sti_pysprk_36')
}
},
'r-spark': {
withEnv(["DH_REPO=${DH_REPOS.get("oshinko-s2i-r-spark")}"]) {
watchAutoBuildStage(sourceTag, 'dh_trigger_token_sti_rspark')
}
}
)
}
}
if('oshinko-s2i-github-create-release' in stageOptionsList){
echo "Starting oshinko-s2i-github-create-release stage...."
stage("creating release: ${CURRENT_PROJECT}") {
echo "Creating a release for ${CURRENT_PROJECT}..."
// Read template rel notes
String template = readFile "sti-release-body-template.txt"
// Get body from param
String relSummary = params.stiReleaseSummary
// Replace values and write results to tmp file
String cliRepo = GH_REPOS.get("oshinko-cli")
String dhRepoScala = DH_REPOS.get("oshinko-s2i-scala")
String dhRepoPyspark = DH_REPOS.get("oshinko-s2i-pyspark")
String dhRepoJava = DH_REPOS.get("oshinko-s2i-java")
String dhRepoPyspark36 = DH_REPOS.get("oshinko-s2i-pyspark-36")
String dhRepoRSpark = DH_REPOS.get("oshinko-s2i-r-spark")
String scalaLn = "${DOCKERHUB_ENDPOINT}/${DH_REPO_OWNER}/${dhRepoScala}" as String
String pysparkLn = "${DOCKERHUB_ENDPOINT}/${DH_REPO_OWNER}/${dhRepoPyspark}" as String
String javaLn = "${DOCKERHUB_ENDPOINT}/${DH_REPO_OWNER}/${dhRepoJava}" as String
String pyspark36ln = "${DOCKERHUB_ENDPOINT}/${DH_REPO_OWNER}/${dhRepoPyspark36}" as String
String rsparkLn = "${DOCKERHUB_ENDPOINT}/${DH_REPO_OWNER}/${dhRepoRSpark}" as String
String oshinkoCliLn = "${GITHUB_ENDPOINT}/${GH_REPO_OWNER}/${cliRepo}/releases/tag/v${OSHINKO_VERSION}" as String
template = template.replace("<<<SCALA_SPARK_LINK>>>", "[here](${scalaLn})")
template = template.replace("<<<PYSPARK_LINK>>>", "[here](${pysparkLn})")
template = template.replace("<<<JAVA_SPARK_LINK>>>", "[here](${javaLn})")
template = template.replace("<<<PYSPARK_PY36_LINK>>>", "[here](${pyspark36ln})")
template = template.replace("<<<R_SPARK_LINK>>>", "[here](${rsparkLn})")
template = template.replace("<<<CLI_LINK>>>", "[here](${oshinkoCliLn})")
template = template.replace('<<<OSHINKO_VERSION>>>', OSHINKO_VERSION)
template = template.replace('<<<SPARK_VERSION>>>', SPARK_VERSION)
template = template.replace('<<<RELEASE_NOTES_BODY>>>', relSummary)
// create a release file, pass in path to tmp file
String releaseBody = sh returnStdout: true, script: "echo `mktemp release_notes_XXXXXXXXX`"
String releaseFile = sh returnStdout: true, script: "echo `mktemp release_XXXXXXXXX`"
releaseBody = releaseBody.trim()
releaseFile = releaseFile.trim()
writeFile file: releaseBody, text: template
String cwd = pwd()
// Create the release file
sh("${REL_FILE_GENERATOR} ${cwd}/${releaseBody} ${OSHINKO_VERSION} ${cwd}/${releaseFile}")
// Generate release
sh("${REL_GENERATOR} ${GH_REPO_OWNER}/${GH_REPO} -c ${cwd}/${releaseFile} -a ${GH_AUTH_TOKEN} -u ${GH_USER}")
sh("rm ${releaseFile}")
sh("rm ${releaseBody}")
}
}
}
// OC Proxy Stages
String ocproxyProject = "oc-proxy"
withEnv(["CURRENT_PROJECT=${ocproxyProject}", "GH_REPO=${GH_REPOS.get(ocproxyProject)}"]) {
if('oc-proxy-github-tag-push' in stageOptionsList){
echo "Starting oc-proxy-github-tag-push stage...."
stage("update: ${CURRENT_PROJECT}") {
echo "Updating ${CURRENT_PROJECT}...."
repoCtrl()
}
}
if('oc-proxy-watch-autobuild' in stageOptionsList){
echo "Starting oc-proxy-watch-autobuild stage...."
withEnv(["DH_REPO=${DH_REPOS.get(CURRENT_PROJECT)}"]) {
String sourceTag = createTag(OSHINKO_VERSION)
watchAutoBuildStage(sourceTag, 'dh_trigger_token_oc_proxy')
}
}
}
}
}