-
Notifications
You must be signed in to change notification settings - Fork 738
/
Copy pathvariables-functions.groovy
1653 lines (1466 loc) · 58.8 KB
/
variables-functions.groovy
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright IBM Corp. and others 2018
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
/*
* Represents a single build spec ie. platform it
*/
class Buildspec {
private List parents;
private my_def;
/*
* Helper function to check if a variable is a map.
* This is needed because .isMap() is not on the default jenkins allow list
*/
private static boolean isMap(x) {
switch (x) {
case Map:
return true
default:
return false
}
}
/*
* If x can be converted to integer, return the result.
* else return x
*/
private static toKey(x) {
def key = x
try {
key = x as int
} catch (def e) { }
return key
}
/* perform a repeated map lookup of a given '.' separated name in my def
* eg. getNestedField('foo.bar.baz') is equivilent to
* my_def['foo']['bar']['baz'], with the added benefit that if any lookup
* along the path fails, null is returned rather than throwing an exception
*/
private getNestedField(String full_name) {
def names = full_name.split("\\.")
def value = my_def
// note we don't really want to find, but we use this as a verson of .each which we can abort
if (names.size() == 0) {
return null
}
names.find { element_name ->
if (value.containsKey(element_name)) {
value = value[element_name]
return false // continue iterating the list
} else {
value = null
return true // abort processing
}
}
return value
}
/*
* look up a scalar field for the first field in this list to have a value:
* if <name> is a map:
* - <name>.<sdk_ver>
* - <name>.all
* else:
* - <name>
* - <parent>.getScalarField()
* with the parents being evaluated in the order they are listed in the yaml file
*/
public getScalarField(name, sdk_ver) {
def sdk_key = toKey(sdk_ver)
def field = getNestedField(name)
if (field == null) {
field = parents.findResult {it.getScalarField(name, sdk_ver)}
}
// Does this entry specify different values for different sdk versions?
if (null != field && isMap(field)) {
// If we have an sdk specific value use that
if (field.containsKey(sdk_key)) {
field = field[sdk_key]
} else {
// else fall back to the "all" key
field = field['all']
}
}
return field
}
/* Get a list of values composed by concatenating the values of the
* following fields in order:
* - <parent>.getVectorField()
* if <name> is a map:
* - <name>.all
* - <name>.sdk
* else:
* - <name>
* with parents being evaluated in the order they are listed in the yaml file.
* - Note: any list elements which are themselves list ar flattened.
*/
public List getVectorField(name, sdk_ver) {
// Yaml will use an integer key if possible, otherwise it falls back to string
def sdk_key = toKey(sdk_ver)
def field_value = []
parents.each { parent ->
field_value += parent.getVectorField(name, sdk_ver);
}
def my_value = getNestedField(name)
if (my_value != null) {
// Do we specify different values for different SDK versions?
if (isMap(my_value)) {
// If there is an "all" definition put that first
if (my_value.containsKey('all')) {
field_value += my_value['all']
}
// If we have an SDK specific value, add that as well
if (my_value.containsKey(sdk_key)) {
def sdk_val = my_value[sdk_key]
// Special case handling for old style variables where
// excluded tests are stored as a map rather than a list
if (isMap(sdk_val)) {
field_value += sdk_val.keySet()
} else {
field_value += sdk_val
}
}
} else {
field_value += my_value
}
}
return field_value.flatten()
}
/* Construct a build spec
* mgr - reference to the BuildspecManager used to cache Buildspecs
* cfg - Collection returned from yaml corresponding to this buildspec.
* Typically this would be VARIABLES[<build_spec_name>]
*/
public Buildspec(mgr, cfg) {
my_def = cfg
parents = []
if (my_def.containsKey('extends')) {
my_def['extends'].each { parent_name ->
parents << mgr.getSpec(parent_name)
}
}
}
}
/*
* Used to cache a map of names -> Builspecs
*/
class BuildspecManager {
private buildspecs
private vars
/*
* vars_ - Collection that the buildspec info should be pulled from
* Typically this would be VARIABLES.
*/
public BuildspecManager(vars_) {
vars = vars_
buildspecs = [:]
}
/*
* Get a Buildspec for a given name.
*/
@NonCPS
public getSpec(name) {
if (!buildspecs.containsKey(name)) {
buildspecs[name] = new Buildspec(this, vars[name])
}
return buildspecs[name]
}
}
def VARIABLES
def buildspec_manager
pipelineFunctions = load 'buildenv/jenkins/common/pipeline-functions.groovy'
/*
* Parses the Jenkins job variables file and populates the VARIABLES collection.
* If a variables file is not set, parse the default variables file.
* The variables file is in YAML format and contains configuration settings
* required to compile and test the OpenJ9 extensions for OpenJDK on multiple
* platforms.
*/
def parse_variables_file() {
// check if a variable file is passed as a Jenkins build parameter
// if not use default configuration settings
VARIABLE_FILE = get_variables_file()
if (!fileExists("${VARIABLE_FILE}")) {
error("Missing variable file: ${VARIABLE_FILE}")
}
echo "Using variables file: ${VARIABLE_FILE}"
VARIABLES = readYaml file: "${VARIABLE_FILE}"
buildspec_manager = new BuildspecManager(VARIABLES)
}
/*
* Fetch the user provided variable file.
*/
def get_variables_file() {
VARIABLE_FILE = params.VARIABLE_FILE ?: "buildenv/jenkins/variables/defaults.yml"
echo "VARIABLE_FILE:'${VARIABLE_FILE}'"
VENDOR_REPO = params.VENDOR_REPO ?: ""
echo "VENDOR_REPO:'${VENDOR_REPO}'"
VENDOR_BRANCH = params.VENDOR_BRANCH ?: ""
echo "VENDOR_BRANCH:'${VENDOR_BRANCH}'"
VENDOR_CREDENTIALS_ID = params.VENDOR_CREDENTIALS_ID ?: ""
echo "VENDOR_CREDENTIALS_ID:'${VENDOR_CREDENTIALS_ID}'"
if (VARIABLE_FILE && VENDOR_REPO && VENDOR_BRANCH) {
if (VENDOR_CREDENTIALS_ID) {
sshagent(credentials:[VENDOR_CREDENTIALS_ID]) {
checkout_file()
}
} else {
checkout_file()
}
}
return VARIABLE_FILE
}
/*
* Check out the user variable file.
*/
def checkout_file() {
sh "git config remote.vendor.url ${VENDOR_REPO}"
sh "git config remote.vendor.fetch +refs/heads/${VENDOR_BRANCH}:refs/remotes/vendor/${VENDOR_BRANCH}"
sh "git fetch vendor"
sh "git checkout vendor/${VENDOR_BRANCH} -- ${VARIABLE_FILE}"
}
/*
* Sets the Git repository URLs and branches for all sources required to build
* the OpenJ9 extensions for OpenJDK.
* Initializes variables to the value passed as build parameters.
* When no values are available as build parameters set them to the values
* available with the variable file otherwise set them to empty strings.
*/
def set_repos_variables(BUILD_SPECS=null) {
// check if passed as Jenkins build parameters
// if not set as params, check the variables file
if (!BUILD_SPECS) {
// set variables (as strings) for a single OpenJDK release build
// fetch OpenJDK repo and branch from variables file as a map
def openjdk_info = get_openjdk_info(SDK_VERSION, [SPEC], false)
OPENJDK_REPO = openjdk_info.get(SPEC).get('REPO')
OPENJDK_BRANCH = openjdk_info.get(SPEC).get('BRANCH')
OPENJDK_SHA = openjdk_info.get(SPEC).get('SHA')
echo "Using OPENJDK_REPO = ${OPENJDK_REPO} OPENJDK_BRANCH = ${OPENJDK_BRANCH} OPENJDK_SHA = ${OPENJDK_SHA}"
// set OpenJ9 and OMR repos, branches and SHAs
set_extensions_variables()
} else {
// set OpenJDK variables (as maps) for a multiple releases build
def build_releases = get_build_releases(BUILD_SPECS)
build_releases.each { release ->
// fetch OpenJDK repo and branch from variables file as a map
def openjdk_info = get_openjdk_info(release, get_build_specs_by_release(release, BUILD_SPECS), true)
def repos = [:]
def branches = [:]
def shas = [:]
openjdk_info.each { build_spec, info_map ->
repos.put(build_spec, info_map['REPO'])
branches.put(build_spec, info_map['BRANCH'])
shas.put(build_spec, info_map['SHA'])
}
OPENJDK_REPO[release] = repos
OPENJDK_BRANCH[release] = branches
OPENJDK_SHA[release] = shas
}
echo "Using OPENJDK_REPO = ${OPENJDK_REPO.toString()} OPENJDK_BRANCH = ${OPENJDK_BRANCH.toString()} OPENJDK_SHA = ${OPENJDK_SHA.toString()}"
// default URL and branch for the OpenJ9 and OMR repositories (no entries in defaults.yml)
EXTENSIONS = ['OpenJ9': ['repo': 'https://github.com/eclipse-openj9/openj9.git', 'branch': 'master'],
'OMR' : ['repo': 'https://github.com/eclipse-openj9/openj9-omr.git', 'branch': 'openj9']]
// set OpenJ9 and OMR repos, branches and SHAs
set_extensions_variables(EXTENSIONS)
}
}
/*
* Initializes OpenJDK repository URL, branch, SHA for given release and returns
* them as a map.
* Build parameters take precedence over custom variables (see variables file).
* Throws an error if repository URL or branch are not provided.
*/
def get_openjdk_info(SDK_VERSION, SPECS, MULTI_RELEASE) {
// map to store git repository information by spec
def openjdk_info = [:]
// fetch values from variables file
def default_openjdk_info = get_value(VARIABLES.openjdk, SDK_VERSION)
// the build expects the following parameters:
// OPENJDK*_REPO, OPENJDK*_BRANCH, OPENJDK*_SHA,
// OPENJDK*_REPO_<spec>, OPENJDK*_BRANCH_<spec>, OPENJDK*_SHA_<spec>
def param_name = "OPENJDK"
if (MULTI_RELEASE) {
// if a wrapper pipeline build, expect parameters names to be prefixed with "OPENJDK<version>_"
param_name += SDK_VERSION
}
for (build_spec in SPECS) {
// default values from variables file
def repo = default_openjdk_info.get('default').get('repoUrl')
def branch = default_openjdk_info.get('default').get('branch')
def sha = ''
def shortBuildSpecName = build_spec
for (entry in default_openjdk_info.entrySet()) {
if ((entry.key != 'default') && entry.key.contains(build_spec)) {
repo = entry.value.get('repoUrl')
branch = entry.value.get('branch')
shortBuildSpecName = entry.key.get(0)
break
}
}
// check build parameters
if (params."${param_name}_REPO") {
// got OPENJDK*_REPO build parameter, overwrite repo
repo = params."${param_name}_REPO"
}
if (params."${param_name}_BRANCH") {
// got OPENJDK*_BRANCH build parameter, overwrite branch
branch = params."${param_name}_BRANCH"
}
if (params."${param_name}_SHA") {
// got OPENJDK*_SHA build parameter, overwrite sha
sha = params."${param_name}_SHA"
}
if (MULTI_RELEASE) {
if (params."${param_name}_REPO_${shortBuildSpecName}") {
// got OPENJDK*_REPO_<spec> build parameter, overwrite repo
repo = params."${param_name}_REPO_${shortBuildSpecName}"
}
if (params."${param_name}_BRANCH_${shortBuildSpecName}") {
// got OPENJDK*_BRANCH_<spec> build parameter, overwrite branch
branch = params."${param_name}_BRANCH_${shortBuildSpecName}"
}
if (params."${param_name}_SHA_${shortBuildSpecName}") {
// got OPENJDK*_SHA_<spec> build parameter, overwrite sha
sha = params."${param_name}_SHA_${shortBuildSpecName}"
}
}
// populate the map
openjdk_info.put(build_spec, ['REPO': convert_url(repo), 'BRANCH': branch, 'SHA': sha])
}
return openjdk_info
}
/*
* Initializes the OpenJ9 and OMR repositories variables with values from
* the variables file if they are not set as build parameters.
* If no values available in the variable file then initialize these variables
* with default values, otherwise set them to empty strings (to avoid
* downstream builds errors - Jenkins build parameters should not be null).
*/
def set_extensions_variables(defaults=null) {
OPENJ9_REPO = params.OPENJ9_REPO
if (!OPENJ9_REPO) {
// set it to the value set into the variable file
OPENJ9_REPO = VARIABLES.openj9_repo
}
if (!OPENJ9_REPO) {
OPENJ9_REPO = ''
if (defaults) {
OPENJ9_REPO = defaults.get('OpenJ9').get('repo')
}
}
OPENJ9_REPO = convert_url(OPENJ9_REPO)
OPENJ9_BRANCH = params.OPENJ9_BRANCH
if (!OPENJ9_BRANCH) {
OPENJ9_BRANCH = VARIABLES.openj9_branch
}
if (!OPENJ9_BRANCH) {
OPENJ9_BRANCH = ''
if (defaults) {
OPENJ9_BRANCH = defaults.get('OpenJ9').get('branch')
}
}
OPENJ9_SHA = params.OPENJ9_SHA
if (!OPENJ9_SHA) {
OPENJ9_SHA = ''
}
OMR_REPO = params.OMR_REPO
if (!OMR_REPO) {
// set it to the value set into the variable file
OMR_REPO = VARIABLES.omr_repo
}
if (!OMR_REPO) {
OMR_REPO = ''
if (defaults) {
OMR_REPO = defaults.get('OMR').get('repo')
}
}
OMR_REPO = convert_url(OMR_REPO)
OMR_BRANCH = params.OMR_BRANCH
if (!OMR_BRANCH) {
// set it to the value set into the variable file
OMR_BRANCH = VARIABLES.omr_branch
}
if (!OMR_BRANCH) {
OMR_BRANCH = ''
if (defaults) {
OMR_BRANCH = defaults.get('OMR').get('branch')
}
}
OMR_SHA = params.OMR_SHA
if (!OMR_SHA) {
OMR_SHA = ''
}
echo "Using OPENJ9_REPO = ${OPENJ9_REPO} OPENJ9_BRANCH = ${OPENJ9_BRANCH} OPENJ9_SHA = ${OPENJ9_SHA}"
echo "Using OMR_REPO = ${OMR_REPO} OMR_BRANCH = ${OMR_BRANCH} OMR_SHA = ${OMR_SHA}"
}
/*
* Returns the value of the element identified by the given key or an empty string
* if the map contains no mapping for the key.
*/
def get_value(MAP, KEY) {
if (MAP != null) {
for (item in MAP) {
if ("${item.key}" == "${KEY}") {
if ((item.value instanceof Map) || (item.value instanceof java.util.List)) {
return item.value
} else {
// stringify value
return "${item.value}"
}
}
}
}
return ''
}
/*
* Returns Jenkins credentials ID required to connect to GitHub using
* the ssh protocol.
* Returns empty string if no values is provided in the variables file (not
* required for public repositories).
*/
def get_git_user_credentials_id() {
return get_user_credentials_id("git")
}
/*
* Returns Jenkins credentials ID required to connect to GitHub using
* API (username & password)
* Returns empty string if no values is provided in the variables file
*/
def get_github_user_credentials_id() {
return get_user_credentials_id("github")
}
/*
* Returns Jenkins credentials ID from the variable file for given key.
*/
def get_user_credentials_id(KEY) {
if (VARIABLES.credentials && VARIABLES.credentials."${KEY}") {
return VARIABLES.credentials."${KEY}"
}
return ''
}
/*
* Sets the NODE variable to the labels identifying nodes by platform suitable
* to run a Jenkins job.
* Fetches the labels from the variables file when the node is not set as build
* parameter.
* The node's labels could be a single label - e.g. label1 - or a boolean
* expression - e.g. label1 && label2 || label3.
*/
def set_node(job_type) {
// fetch labels for given platform/spec
NODE = ''
DOCKER_IMAGE = buildspec_manager.getSpec(SPEC).getScalarField("node_labels.docker_image", SDK_VERSION) ?: ''
for (key in ['NODE', 'LABEL']) {
// check the build parameters
if (params.containsKey(key)) {
NODE = params.get(key)
break
}
}
if (!NODE) {
// fetch from variables file
NODE = buildspec_manager.getSpec(SPEC).getScalarField("node_labels.${job_type}", SDK_VERSION)
if (!NODE) {
error("Missing ${job_type} NODE!")
}
}
}
/*
* Set the RELEASE variable with the value provided in the variables file.
*/
def set_release() {
RELEASE = buildspec_manager.getSpec(SPEC).getScalarField("release", SDK_VERSION)
}
/*
* Set the JDK_FOLDER variable with the value provided in the variables file.
*/
def set_jdk_folder() {
JDK_FOLDER = buildspec_manager.getSpec('misc').getScalarField("jdk_image_dir", SDK_VERSION)
}
/*
* Sets variables for a job that builds the OpenJ9 extensions for OpenJDK for a
* given platform and version.
*/
def set_build_variables() {
set_repos_variables()
def buildspec = buildspec_manager.getSpec(SPEC)
// fetch values per spec and Java version from the variables file
set_release()
set_jdk_folder()
set_build_extra_options()
set_sdk_impl()
// set variables for the build environment configuration
// check job parameters, if not provided default to variables file
BUILD_ENV_VARS = params.BUILD_ENV_VARS
if (!BUILD_ENV_VARS) {
BUILD_ENV_VARS = buildspec.getVectorField("build_env.vars", SDK_VERSION).join(" ")
}
BUILD_ENV_VARS_LIST = []
if (BUILD_ENV_VARS) {
// expect BUILD_ENV_VARS to be a space separated list of environment variables
// e.g. <variable1>=<value1> <variable2>=<value2> ... <variableN>=<valueN>
// convert space separated list to array
BUILD_ENV_VARS_LIST.addAll(BUILD_ENV_VARS.tokenize(' '))
}
EXTRA_BUILD_ENV_VARS = params.EXTRA_BUILD_ENV_VARS
if (EXTRA_BUILD_ENV_VARS) {
BUILD_ENV_VARS_LIST.addAll(EXTRA_BUILD_ENV_VARS.tokenize(' '))
}
BUILD_ENV_CMD = params.BUILD_ENV_CMD
if (!BUILD_ENV_CMD) {
BUILD_ENV_CMD = buildspec.getScalarField("build_env.cmd", SDK_VERSION)
}
if (BUILD_ENV_CMD) {
// BUILD_ENV_CMD is used as preamble command
BUILD_ENV_CMD += ' && '
} else {
BUILD_ENV_CMD = ''
}
echo "Using BUILD_ENV_CMD = ${BUILD_ENV_CMD}, BUILD_ENV_VARS_LIST = ${BUILD_ENV_VARS_LIST.toString()}"
if (VARIABLES.misc.custom_filename) {
customFile = load VARIABLES.misc.custom_filename
} else {
customFile = null
}
}
def set_sdk_variables() {
set_sdk_versions()
DATESTAMP = get_date()
SDK_FILE_EXT = SPEC.contains('zos') ? '.pax' : '.tar.gz'
SDK_FILENAME = get_value(VARIABLES.misc.sdk_filename_template, BUILD_IDENTIFIER) ?: get_value(VARIABLES.misc.sdk_filename_template, 'default')
echo "SDK_FILENAME (before processing):'${SDK_FILENAME}'"
// If filename has unresolved variables at this point, use Groovy Template Binding engine to resolve them
if (SDK_FILENAME.contains('$')) {
set_sdk_impl()
// Add all variables that could be used in a template
def binding = ["SDK_IMPL":SDK_IMPL, "SPEC":SPEC, "SDK_VERSION":SDK_VERSION, "FULL_SDK_VERSION":FULL_SDK_VERSION, "JAVA_RELEASE":JAVA_RELEASE, "JAVA_SUB_RELEASE":JAVA_SUB_RELEASE, "DATESTAMP":DATESTAMP, "SDK_FILE_EXT":SDK_FILE_EXT]
def engine = new groovy.text.SimpleTemplateEngine()
SDK_FILENAME = engine.createTemplate(SDK_FILENAME).make(binding)
}
echo "SDK_FILENAME:'${SDK_FILENAME}'"
TEST_FILENAME = "test-images.tar.gz"
CODE_COVERAGE_FILENAME = "code-coverage-files.tar.gz"
JAVADOC_FILENAME = "OpenJ9-JDK${SDK_VERSION}-Javadoc-${SPEC}-${DATESTAMP}.tar.gz"
JAVADOC_OPENJ9_ONLY_FILENAME = "OpenJ9-JDK${SDK_VERSION}-Javadoc-openj9-${SPEC}-${DATESTAMP}.tar.gz"
DEBUG_IMAGE_FILENAME = "debug-image.tar.gz"
echo "Using SDK_FILENAME = ${SDK_FILENAME}"
echo "Using TEST_FILENAME = ${TEST_FILENAME}"
echo "Using CODE_COVERAGE_FILENAME = ${CODE_COVERAGE_FILENAME}"
echo "Using JAVADOC_FILENAME = ${JAVADOC_FILENAME}"
echo "Using JAVADOC_OPENJ9_ONLY_FILENAME = ${JAVADOC_OPENJ9_ONLY_FILENAME}"
echo "Using DEBUG_IMAGE_FILENAME = ${DEBUG_IMAGE_FILENAME}"
}
def set_sdk_versions() {
/*
* Get full version from tag and replace + with _
* Eg. JDK8
* OPENJDK_TAG := jdk8u272-b08
* FULL_SDK_VERSION = jdk8u272-b08
* JAVA_RELEASE = 8
* JAVS_SUB_RELEASE = 8u272
*
* Eg. JDK11
* OPENJDK_TAG := jdk-11.0.8+10
* FULL_SDK_VERSION = jdk-11.0.8_10
* JAVA_RELEASE = 11.0
* JAVS_SUB_RELEASE = 11.0.8
*
* Eg. Initial feature release
* OPENJDK_TAG := jdk-15+36
* FULL_SDK_VERSION = jdk-15_36
* JAVA_RELEASE = 15.0
* JAVA_SUB_RELEASE = 15.0.0
*/
FULL_SDK_VERSION = sh (
script: "sed -n -e 's/+/_/g; s/^OPENJDK_TAG := //p' < closed/openjdk-tag.gmk",
returnStdout: true
).trim()
def subReleaseStartIndexChar = 'jdk-'
def subReleaseEndIndexChar = '_'
def releaseEndIndexChar = '.'
if (SDK_VERSION.equals('8')) {
subReleaseStartIndexChar = 'jdk'
subReleaseEndIndexChar = '-'
releaseEndIndexChar = 'u'
}
JAVA_SUB_RELEASE = FULL_SDK_VERSION.substring(FULL_SDK_VERSION.indexOf(subReleaseStartIndexChar) + subReleaseStartIndexChar.length(), FULL_SDK_VERSION.lastIndexOf(subReleaseEndIndexChar))
if (!JAVA_SUB_RELEASE.contains('.') && !SDK_VERSION.equals('8')) {
// Must be the initial
JAVA_SUB_RELEASE += ".0.0"
}
JAVA_RELEASE = JAVA_SUB_RELEASE.substring(0,JAVA_SUB_RELEASE.lastIndexOf(releaseEndIndexChar))
echo "FULL_SDK_VERSION:'${FULL_SDK_VERSION}'"
echo "JAVA_RELEASE:'${JAVA_RELEASE}'"
echo "JAVA_SUB_RELEASE:'${JAVA_SUB_RELEASE}'"
}
def get_date() {
//Returns a string for the current date YYYYMMDD-HHMMSS
return sh (
script: 'date +%Y%m%d-%H%M%S',
returnStdout: true
).trim()
}
/*
* Set TESTS_TARGETS, indicating the level of testing.
*/
def set_test_targets() {
// Map of Maps
TESTS = [:]
if (TESTS_TARGETS != 'none') {
for (target in TESTS_TARGETS.replaceAll("\\s","").toLowerCase().tokenize(',')) {
switch (target) {
case ["sanity"]:
TESTS["${target}.functional"] = [:]
TESTS["${target}.openjdk"] = [:]
break
case ["extended"]:
TESTS["${target}.functional"] = [:]
break
default:
if (target.contains('+')) {
def id = target.substring(0, target.indexOf('+'))
TESTS[id] = [:]
TESTS[id]['testFlag'] = target.substring(target.indexOf('+') +1).toUpperCase()
} else {
TESTS[target] = [:]
}
break
}
}
}
echo "TESTS:'${TESTS}'"
set_sdk_impl()
}
def set_sdk_impl() {
// Set SDK Implementation, default to OpenJ9
SDK_IMPL = buildspec.getScalarField("sdk_impl", 'all') ?: 'openj9'
SDK_IMPL_SHORT = buildspec.getScalarField("sdk_impl_short", 'all') ?: 'j9'
echo "SDK_IMPL:'${SDK_IMPL}'"
echo "SDK_IMPL_SHORT:'${SDK_IMPL_SHORT}'"
}
/*
* Set TEST_FLAG for all targets if defined in variable file.
* Set EXCLUDED_TESTS if defined in variable file.
* Set EXTRA_TEST_LABELS map if defined in variable file.
*/
def set_test_misc() {
if (!params.TEST_NODE) {
// Only add extra test labels if the user has not specified a specific TEST_NODE
TESTS.each { id, target ->
target['extraTestLabels'] = buildspec.getVectorField("extra_test_labels", SDK_VERSION).join('&&') ?: ''
if (target['extraTestLabels'].endsWith('&&')){
target['extraTestLabels'] = target['extraTestLabels'].substring(0, target['extraTestLabels'].length() - 2)
}
}
} else {
TESTS.each { id, target ->
target['extraTestLabels'] = ''
}
}
def buildspec = buildspec_manager.getSpec(SPEC)
def excludedTests = buildspec.getVectorField("excluded_tests", SDK_VERSION)
if (excludedTests) {
TESTS.each { id, target ->
if (excludedTests.contains(id)) {
echo "Excluding test target:'${id}'"
TESTS.remove(id)
}
}
}
TESTS.each { id, target ->
flag = buildspec.getScalarField("test_flags", id) ?: ''
if (!target['testFlag']) {
target['testFlag'] = flag
} else if (flag) {
error("Cannot set more than 1 TEST_FLAG:'${target['testFlag']}' & '${flag}'")
}
// Set test param KEEP_REPORTDIR to false unless set true in variable file.
target['keepReportDir'] = buildspec_manager.getSpec('misc').getScalarField("test_keep_reportdir", id) ?: 'false'
target['buildList'] = buildspec.getScalarField("test_build_list", id) ?: ''
}
echo "TESTS:'${TESTS}'"
}
def set_slack_channel() {
SLACK_CHANNEL = params.SLACK_CHANNEL
if (!SLACK_CHANNEL && !params.PERSONAL_BUILD.equalsIgnoreCase('true')) {
SLACK_CHANNEL = VARIABLES.slack_channel
}
}
def set_artifactory_config(id="Nightly") {
set_basic_artifactory_config(id)
if (VARIABLES.artifactory.defaultGeo) {
def repo = ARTIFACTORY_CONFIG['repo']
ARTIFACTORY_CONFIG['uploadDir'] = get_value(VARIABLES.artifactory.uploadDir, id) ?: get_value(VARIABLES.artifactory.uploadDir, 'default')
if (!ARTIFACTORY_CONFIG['uploadDir'].endsWith('/')) {
ARTIFACTORY_CONFIG['uploadDir'] += '/'
}
// If uploadDir has unresolved variables at this point, use Groovy Template Binding engine to resolve them
if (ARTIFACTORY_CONFIG['uploadDir'].contains('$')) {
if (ARTIFACTORY_CONFIG['uploadDir'].contains('SDK_IMPL') && !binding.hasVariable('SDK_IMPL')) {
set_sdk_impl()
}
// Add all variables that could be used in a template
def binding = ["JOB_NAME":JOB_NAME, "BUILD_ID":BUILD_ID, "repo":repo, "SDK_IMPL":SDK_IMPL, "JAVA_RELEASE":JAVA_RELEASE, "JAVA_SUB_RELEASE":JAVA_SUB_RELEASE, "SPEC":SPEC, "DATESTAMP":DATESTAMP]
def engine = new groovy.text.SimpleTemplateEngine()
ARTIFACTORY_CONFIG['uploadDir'] = engine.createTemplate(ARTIFACTORY_CONFIG['uploadDir']).make(binding)
}
ARTIFACTORY_CONFIG[VARIABLES.artifactory.defaultGeo]['uploadBool'] = true
// Determine if we need to upload more than the default server
if (ARTIFACTORY_CONFIG['geos'].size() > 1) {
// What platform did we build on
compilePlatform = get_node_platform(NODE_LABELS)
// See if there are servers with colocated nodes of matching platform
def testNodes = jenkins.model.Jenkins.instance.getLabel('ci.role.test').getNodes()
for (node in testNodes) {
def nodeGeo = get_node_geo(node.getLabelString())
// If we haven't determined yet if we will need to upload to 'nodeGeo'...
if (ARTIFACTORY_CONFIG[nodeGeo] && !ARTIFACTORY_CONFIG[nodeGeo]['uploadBool']) {
def nodePlatform = get_node_platform(node.getLabelString())
// Upload if there is a server at geo where there are machines matching our platform.
if (nodePlatform == compilePlatform && ARTIFACTORY_CONFIG['geos'].contains(nodeGeo)) {
ARTIFACTORY_CONFIG[nodeGeo]['uploadBool'] = true
}
}
}
}
echo "ARTIFACTORY_CONFIG:'${ARTIFACTORY_CONFIG}'"
}
}
def set_basic_artifactory_config(id="Nightly") {
ARTIFACTORY_CONFIG = [:]
echo "Configure Artifactory..."
if (VARIABLES.artifactory.defaultGeo) {
// Allow default geo to be overridden with a param. Used by the Clenaup script to target a specific server.
ARTIFACTORY_CONFIG['defaultGeo'] = params.ARTIFACTORY_GEO ?: VARIABLES.artifactory.defaultGeo
ARTIFACTORY_CONFIG['geos'] = VARIABLES.artifactory.server.keySet()
ARTIFACTORY_CONFIG['repo'] = get_value(VARIABLES.artifactory.repo, id) ?: get_value(VARIABLES.artifactory.repo, 'default')
for (geo in ARTIFACTORY_CONFIG['geos']) {
ARTIFACTORY_CONFIG[geo] = [:]
ARTIFACTORY_CONFIG[geo]['server'] = get_value(VARIABLES.artifactory.server, geo)
def numArtifacts = get_value(VARIABLES.artifactory.numArtifacts, geo)
if (!numArtifacts) {
numArtifacts = get_value(VARIABLES.build_discarder.logs, pipelineFunctions.convert_build_identifier(id))
}
ARTIFACTORY_CONFIG[geo]['numArtifacts'] = numArtifacts.toInteger()
ARTIFACTORY_CONFIG[geo]['daysToKeepArtifacts'] = get_value(VARIABLES.artifactory.daysToKeepArtifacts, geo).toInteger()
ARTIFACTORY_CONFIG[geo]['manualCleanup'] = get_value(VARIABLES.artifactory.manualCleanup, geo)
ARTIFACTORY_CONFIG[geo]['vpn'] = get_value(VARIABLES.artifactory.vpn, geo)
ARTIFACTORY_CONFIG[geo]['buildNamePrefix'] = get_value(VARIABLES.artifactory.buildNamePrefix, geo)
}
/*
* Write out default server values to string variables.
* The upstream job calls job.getBuildVariables() which only returns strings.
* Rather than parsing out the ARTIFACTORY_CONFIG map that is stored as a string,
* we'll write out the values to env here as strings to save work later.
*/
env.ARTIFACTORY_SERVER = ARTIFACTORY_CONFIG[ARTIFACTORY_CONFIG['defaultGeo']]['server']
env.ARTIFACTORY_REPO = ARTIFACTORY_CONFIG['repo']
env.ARTIFACTORY_NUM_ARTIFACTS = ARTIFACTORY_CONFIG[ARTIFACTORY_CONFIG['defaultGeo']]['numArtifacts']
env.ARTIFACTORY_DAYS_TO_KEEP_ARTIFACTS = ARTIFACTORY_CONFIG[ARTIFACTORY_CONFIG['defaultGeo']]['daysToKeepArtifacts']
env.ARTIFACTORY_MANUAL_CLEANUP = ARTIFACTORY_CONFIG[ARTIFACTORY_CONFIG['defaultGeo']]['manualCleanup']
}
}
def get_node_geo(nodeLabels) {
if (nodeLabels.contains('ci.geo.')) {
labelArray = nodeLabels.tokenize()
for (label in labelArray) {
if (label ==~ /ci\.geo\..*/) {
return label.substring(7)
}
}
}
return ''
}
def get_node_platform(nodeLabels) {
/*
* xlinux -> arch=x86 && baseOS=linux
* plinux -> arch=ppc64le && baseOS=linux
* zlinux -> arch=s390x && baseOS=linux
* aix -> baseOS=aix
* windows -> baseOS=windows
* osx -> baseOS=osx
* zos -> baseOS=zos
* aarch64 -> arch=aarch64
*/
def arch = ''
def baseOS = ''
if (nodeLabels.contains('hw.arch.') && nodeLabels.contains('sw.os.')) {
labelArray = nodeLabels.tokenize()
for (label in labelArray) {
switch (label) {
case ~/hw\.arch\.[a-z0-9]+/:
arch = label.substring(8)
//println arch
break
case ~/sw\.os\.[a-z]+/:
baseOS = label.substring(6)
//println baseOS
break
}
}
}
if (!arch || !baseOS) {
echo "WARNING: Unable to determine node arch/os:'${nodeLabels}'"
return ''
}
switch (baseOS) {
case ['aix', 'windows', 'mac', 'zos']:
return baseOS
break
case ['linux', 'ubuntu', 'rhel', 'cent', 'sles'] :
switch (arch) {
case 'x86':
return 'xlinux'
break
case 'ppc64le':
return 'plinux'
break
case 'ppc64':
return 'plinuxBE'
case 's390x':
return 'zlinux'
break
case ['aarch64', 'aarch32']:
return 'alinux'
break
default:
echo "WARNING: Unknown OS:'${baseOS}' for arch:'${arch}'"
}
break
default:
echo "WARNING: Unknown baseOS:'${baseOS}'"
}
return ''
}
def set_restart_timeout() {
RESTART_TIMEOUT = params.RESTART_TIMEOUT
if (!RESTART_TIMEOUT) {
RESTART_TIMEOUT = VARIABLES.restart_timeout.time
}
RESTART_TIMEOUT_UNITS = params.RESTART_TIMEOUT_UNITS
if (!RESTART_TIMEOUT_UNITS) {
RESTART_TIMEOUT_UNITS = VARIABLES.restart_timeout.units
}
}
def set_build_identifier() {
BUILD_IDENTIFIER = params.BUILD_IDENTIFIER
if (!BUILD_IDENTIFIER) {
BUILD_IDENTIFIER = ''
if (params.ghprbPullId && params.ghprbGhRepository) {
// If Pull Request build
BUILD_IDENTIFIER = params.ghprbGhRepository + '#' + params.ghprbPullId
} else if (PERSONAL_BUILD.equalsIgnoreCase('true')) {
// If Personal build
wrap([$class: 'BuildUser']) {
BUILD_IDENTIFIER = "${BUILD_USER_EMAIL}"
}
} else {
echo "WARNING: Can't determine appropriate BUILD_IDENTIFIER automatically. Please add it as a build parameter if you would like it set."
}
}
if (BUILD_IDENTIFIER) {
currentBuild.displayName = "#${BUILD_NUMBER} - ${BUILD_IDENTIFIER}"
}
}
def set_custom_description() {
if (params.CUSTOM_DESCRIPTION) {
def TMP_DESC = (currentBuild.description) ? "<br>" + currentBuild.description : ""
currentBuild.description = CUSTOM_DESCRIPTION + TMP_DESC
}
}
/*
* Adds a String type Parameter
* Takes a Key-Value map which are used as the param name and default value