-
Notifications
You must be signed in to change notification settings - Fork 0
/
brightspot-deploy.rb
executable file
·919 lines (678 loc) · 28.7 KB
/
brightspot-deploy.rb
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
#!/usr/bin/ruby -w
require 'rexml/document'
require 'json'
require 'open-uri'
include REXML
$stdout.sync = true
# Set to true when testing locally to skip the Artifactory upload step.
DEBUG_SKIP_UPLOAD = false
ARTIFACTORY_URL_PREFIX = 'https://artifactory.psdops.com/psddev-releases'
# Represents a Maven artifact, where the path is optional.
class MavenArtifact
attr_accessor :group_id, :artifact_id, :version, :path
def initialize(group_id, artifact_id, version, path)
@group_id = group_id
@artifact_id = artifact_id
@version = version
@path = path
end
def to_s
"#{@group_id}:#{@artifact_id}:#{@version}"
end
end
# Semantic Version, mostly-ish
class SemVersion
attr_accessor :major, :minor, :patch
def initialize(version)
if version.respond_to?("major")
@major = version.major
@minor = version.minor
@patch = version.patch
else
# parse version string
version_parts = version.split(/\./, 3)
if version_parts.length > 0
@major = version_parts[0]
else
@major = '0'
end
if version_parts.length > 1
@minor = version_parts[1]
else
@minor = nil
end
if version_parts.length > 2
@patch = version_parts[2]
else
@patch = nil
end
end
end
def to_s
"#{@major}.#{@minor}.#{@patch}"
end
def major_number
if @major == nil
nil
else
@major.to_i
end
end
def minor_number
if @minor == nil
nil
else
@minor.to_i
end
end
def patch_number
if @patch == nil
nil
else
@patch.to_i
end
end
def is_major_snapshot
@major.include?'-SNAPSHOT'
end
def is_minor_snapshot
@minor.include?'-SNAPSHOT'
end
def is_patch_snapshot
@patch.include?'-SNAPSHOT'
end
def is_snapshot
is_major_snapshot || is_minor_snapshot || is_patch_snapshot
end
end
# Finds the element targeted by the given XPath expression for the pom.xml of
# the given module_path and returns the text value of the first result.
def maven_xpath(module_path, expr)
list = maven_xpath_list(module_path, expr)
list.each do |item|
return item
end
nil
end
# Finds the elements targeted by the given XPath expression for the pom.xml of
# the given module_path and returns the text values as an iterable.
def maven_xpath_list(module_path, expr)
xmlfile = File.new("#{module_path}/pom.xml")
xmldoc = Document.new(xmlfile)
XPath.each(xmldoc, "#{expr}/text()")
end
# Calculates the Maven artifact info (groupId / artifactId / version) for the
# given module_paths. If only one path is given then only one MavenArtifact is
# returned, otherwise an iterable of the results is returned.
def maven_module_info(module_paths)
if module_paths.respond_to?("each")
modules = Array.new
module_paths.each do |module_path|
modules.push(maven_module_info(module_path))
end
modules
else
group_id = maven_xpath(module_paths, "/project/groupId")
if group_id == nil
group_id = maven_xpath(module_paths, "/project/parent/groupId")
end
artifact_id = maven_xpath(module_paths, "/project/artifactId")
version = maven_xpath(module_paths, "/project/version")
MavenArtifact.new(group_id, artifact_id, version, module_paths)
end
end
# Gets the list of modules defined in the pom.xml at the given path. This
# function can optionally recurse down to sub-modules.
def maven_module_list(path, recurse = false)
module_list = Array.new
maven_xpath_list(path, "/project/modules/module").each do |name|
module_list.push(name.to_s)
if recurse
maven_module_list("#{path}/#{name}", true).each do |name2|
module_list.push("#{name}/#{name2}")
end
end
end
module_list
end
# Gets the Maven artifact dependencies defined in the pom.xml at the given path.
# Only those dependencies defined in the dependencyManagement section are
# returned (for now).
def maven_dependencies(path)
deps = Array.new
xmlfile = File.new("#{path}/pom.xml")
xmldoc = Document.new(xmlfile)
XPath.each(xmldoc, "/project/dependencyManagement/dependencies/dependency") do |dep|
group_id = XPath.first(dep, "groupId/text()")
artifact_id = XPath.first(dep, "artifactId/text()")
version = XPath.first(dep, "version/text()")
deps.push(MavenArtifact.new(group_id, artifact_id, version, nil))
end
deps
end
# Gets the Maven artifact plugin dependencies defined in the pom.xml at the
# given path. Only those dependencies defined in the pluginManagement section
# are returned (for now).
def maven_plugins(path)
deps = Array.new
xmlfile = File.new("#{path}/pom.xml")
xmldoc = Document.new(xmlfile)
XPath.each(xmldoc, "/project/build/pluginManagement/plugins/plugin") do |dep|
group_id = XPath.first(dep, "groupId/text()")
artifact_id = XPath.first(dep, "artifactId/text()")
version = XPath.first(dep, "version/text()")
deps.push(MavenArtifact.new(group_id, artifact_id, version, nil))
end
deps
end
# For a given Maven artifact, checks Artifactory to see if the artifact already
# exists by returning the HTTP status response code for the artifact's pom file.
def maven_artifactory_status(maven_artifact)
maven_artifact = maven_module_info(maven_artifact) unless maven_artifact.respond_to?("group_id")
ma = maven_artifact
artifactory_url = "#{ARTIFACTORY_URL_PREFIX}/#{ma.group_id.to_s.gsub(".", "/")}/#{ma.artifact_id}/#{ma.version}/#{ma.artifact_id}-#{ma.version}.pom"
puts "Fetching: " + artifactory_url
artifactory_status = `curl -s -I "#{artifactory_url}" | head -n 1 | cut -d' ' -f2`
puts "Status: " + artifactory_status
artifactory_status
end
# Fetches an array of maven module paths whose versions have not yet been
# deployed to artifactory.
def get_newly_versioned_modules
new_modules = Array.new
all_modules = maven_module_list(".", true)
all_modules.each do |mod|
new_modules.push(mod) unless maven_artifactory_status(mod).strip.eql?("200")
end
new_modules
end
# For a given Git commit range, checks to see if any pom.xml file was modified.
def is_pom_modified(commit_range)
commit_range = commit_range.gsub("...", "..")
modified_files = `git diff-tree -m -r --no-commit-id --name-only #{commit_range}`
# write the modified files out to a file since issuing an inline echo command
# with each as arguments can become too large for the shell to handle.
open('modified_files.out', 'w') { |f|
f.puts "#{modified_files}"
}
modified_file_names = `cat modified_files.out | rev | cut -d/ -f1 | rev | uniq`
modified_file_names.split(/\n+/).index("pom.xml") != nil
end
# For a given Git commit range, finds all the Maven module paths that have
# been modified.
def get_project_diff_list(commit_range)
modified_modules = Array.new
commit_range = commit_range.gsub("...", "..")
modified_files = `git diff-tree -m -r --no-commit-id --name-only #{commit_range}`
# write the modified files out to a file since issuing an inline echo command
# with each as arguments can become too large for the shell to handle.
open('modified_root_paths.out', 'w') { |f|
f.puts "#{modified_files}"
}
modified_root_paths = `cat modified_root_paths.out | cut -d/ -f1 | uniq`
modified_root_paths = modified_root_paths.split(/\n+/)
puts "modified_root_paths: #{modified_root_paths.join(" ")}"
root_modules = maven_module_list(".")
modified_root_paths = modified_root_paths.delete_if { |item| root_modules.index(item) == nil }
puts "modified_root_modules: #{modified_root_paths.join(" ")}"
modified_root_paths.each do |root_path|
modified_modules.push(root_path)
maven_module_list(root_path, true).each do |sub_module|
modified_modules.push("#{root_path}/#{sub_module}")
end
end
modified_modules
end
# Verifies that the dependencies in the BOM match the dependencies in the rest
# of the project.
def verify_bom_dependencies
bom_deps_arr = Array.new
mod_deps_arr = Array.new
# Get the BOM dependencies
maven_dependencies("bom").each do |bom_dep|
bom_deps_arr.push(bom_dep.to_s)
end
# Get each module's artifact information
all_modules = maven_module_list(".", true)
bom_index = all_modules.index('bom')
all_modules.slice!(bom_index) unless bom_index == nil
parent_index = all_modules.index('parent')
all_modules.slice!(parent_index) unless parent_index == nil
grandparent_index = all_modules.index('grandparent')
all_modules.slice!(grandparent_index) unless grandparent_index == nil
all_modules.each do |name|
mod_deps_arr.push(maven_module_info(name).to_s)
end
# Sort the arrays
bom_deps_arr = bom_deps_arr.sort
mod_deps_arr = mod_deps_arr.sort
# Compare the arrays
bom_deps_arr.each do |bom_dep|
#puts "bom: #{bom_dep}"
end
mod_deps_arr.each do |mod_dep|
#puts "mod: #{mod_dep}"
end
bom_extras = bom_deps_arr - mod_deps_arr
mod_extras = mod_deps_arr - bom_deps_arr
if bom_extras.length > 0 || mod_extras.length > 0
bom_deps_error = ""
if bom_extras.length > 0
bom_deps_error += "BOM contains unrecognized dependencies: [" + bom_extras.join(", ") + "]. "
end
if mod_extras.length > 0
bom_deps_error += "BOM is missing dependencies: [" + mod_extras.join(", ") + "]."
end
begin
raise ArgumentError, bom_deps_error
end
end
end
# Verifies that the BOM (and brightspot-parent for plugin) do not contain any
# SNAPSHOT versions for use when publishing a tag release.
def verify_no_release_snapshots
bom_dependencies = maven_dependencies("bom")
parent_plugins = maven_plugins("parent")
bom_snapshots = bom_dependencies.keep_if { |item| item.version.to_s.end_with?("SNAPSHOT") }
parent_snapshots = parent_plugins.keep_if { |item| item.version.to_s.end_with?("SNAPSHOT") }
if bom_snapshots.length > 0 || parent_snapshots.length > 0
release_snapshots_error=""
if bom_snapshots.length > 0
release_snapshots_error += "BOM contains snapshot dependencies: [" + bom_snapshots.join(", ") + "]. "
end
if parent_snapshots.length > 0
release_snapshots_error += "parent contains snapshot plugins: [" + parent_snapshots.join(", ") + "]."
end
begin
raise ArgumentError, release_snapshots_error
end
end
end
def system_stdout(command)
puts "COMMAND: #{command}"
system(command, out: $stdout, err: :out)
end
# One off function that ensures that the express archetype has correct versions
# in it since it's not updated automatically by the normal diff / versioning
# scripts.
def update_archetype_versions
express_archetype_path = "express/archetype/src/main/resources/archetype-resources"
if File.exist?(express_archetype_path)
brightspot_mvn_version = maven_module_info("parent").version.to_s
express_npm_version = JSON.parse(File.read('express/package.json'))['version']
styleguide_npm_version = JSON.parse(File.read('styleguide/package.json'))['version']
system_stdout("sed -i.bak 's|${brightspot-version}|'#{brightspot_mvn_version}'|g' #{express_archetype_path}/pom.xml")
if $? != 0 then raise ArgumentError, "Failed to update archetype pom.xml!" end
system_stdout("sed -i.bak 's|${express-version}|'#{express_npm_version}'|g; s|${styleguide-version}|'#{styleguide_npm_version}'|g' #{express_archetype_path}/package.json")
if $? != 0 then raise ArgumentError, "Failed to update archetype package.json!" end
puts "Updated express archetype dependency versions."
else
puts "Could not find express archetype."
end
end
# Updates all build config files (pom.xml & package.json) to have their new release versions set.
def prepare_release_versions(commit_range, tag_version, pr_version, build_number, lock_snapshots)
system_stdout('git fetch --unshallow')
if commit_range != nil && !commit_range.to_s.strip.empty? && !pr_version.to_s.strip.empty?
module_paths = get_project_diff_list(commit_range)
else
module_paths = Array.new
all_modules = maven_module_list('.', true)
all_modules.each do |all_module|
module_paths.push(all_module.to_s)
end
end
prepare_maven_node_release_versions(module_paths, tag_version, pr_version, build_number, lock_snapshots)
prepare_s3deploy_versions(module_paths)
end
# Update pom.xml release versions
def prepare_maven_node_release_versions(modified_modules, tag_version, pr_version, build_number, lock_snapshots)
modified_artifacts = Set.new
# Always add the root, parent, grandparent, and bom pom artifacts!
modified_artifacts.add(maven_module_info('.'))
modified_artifacts.add(maven_module_info('parent'))
modified_artifacts.add(maven_module_info('grandparent'))
modified_artifacts.add(maven_module_info('bom'))
modified_modules.each do |modified_module|
modified_artifacts.add(maven_module_info(modified_module))
end
all_modules = maven_module_list(".", true)
# Always update the root pom
all_modules.push('.')
bom_deps_hash = Hash.new
if lock_snapshots
latest_tag_version = `git describe --tags --abbrev=0`.to_s.strip
puts "Locking SNAPSHOT versions to release #{latest_tag_version}..."
# Remove leading v
if latest_tag_version.start_with?('v')
latest_tag_version = latest_tag_version[1..-1]
end
bom = Document.new(open("https://artifactory.psdops.com/psddev-releases/com/psddev/brightspot-bom/#{latest_tag_version}/brightspot-bom-#{latest_tag_version}.pom"))
XPath.each(bom, '//dependency') do |bom_dep|
bom_dep_group_id = XPath.first(bom_dep, "groupId/text()")
bom_dep_artifact_id = XPath.first(bom_dep, "artifactId/text()")
bom_dep_version = XPath.first(bom_dep, "version/text()")
bom_deps_hash["#{bom_dep_group_id}:#{bom_dep_artifact_id}"] = bom_dep_version
end
end
all_modules.each do |all_module|
# ---------------------
# Handle Maven Versions
# ---------------------
pom_modified = false
pom = nil
File.open("#{all_module}/pom.xml") do |pom_file|
pom = Document.new(pom_file)
XPath.each(pom, "//dependency | //plugin | //parent | /project") do |dep|
dep_modified = false
group_id = XPath.first(dep, "groupId/text()")
if group_id == nil && dep.name() == 'project'
group_id = XPath.first(dep, "parent/groupId/text()")
end
artifact_id = XPath.first(dep, "artifactId/text()")
version_elmt = XPath.first(dep, "version")
modified_artifacts.each do |modified_artifact|
if group_id == modified_artifact.group_id && artifact_id == modified_artifact.artifact_id && version_elmt != nil
old_version = version_elmt.text
release_version = module_release_version(modified_artifact.path, tag_version, pr_version, nil, false)
if release_version != nil
version_elmt.text = release_version
pom_modified = true
dep_modified = true
puts "Set #{all_module}/pom.xml #{dep.name()} #{group_id}:#{artifact_id}:#{old_version} to version #{release_version}."
end
end
end
# If snapshots need to be locked
if lock_snapshots && !dep_modified && version_elmt != nil && version_elmt.text.end_with?('-SNAPSHOT')
old_version = version_elmt.text
release_version = bom_deps_hash["#{group_id}:#{artifact_id}"]
if release_version != nil
version_elmt.text = release_version
pom_modified = true
puts "Set #{all_module}/pom.xml #{dep.name()} #{group_id}:#{artifact_id}:#{old_version} to version #{release_version}."
end
end
end
end
if pom_modified
formatter = REXML::Formatters::Default.new
File.open("#{all_module}/pom.xml", 'w') do |f|
formatter.write(pom, f)
end
end
# --------------------
# Handle Node Versions
# --------------------
if File.file?("#{all_module}/package.json")
updated_node_module = false
# Update node release versions
if modified_modules.include?(all_module)
new_version = module_release_version(all_module, tag_version, pr_version, build_number, true)
if new_version != nil
package_json = JSON.parse(File.read("#{all_module}/package.json"))
old_version = package_json['version']
package_json['version'] = new_version
updated_node_module = true
puts "Set #{all_module}/package.json version #{old_version} to #{new_version}."
File.open("#{all_module}/package.json", 'w') do |f|
f.write(JSON.pretty_generate(package_json))
end
end
end
# If snapshots need to be locked, just grab the version from this module's
# pom version since it will have just been updated in the Maven step above.
if lock_snapshots && !updated_node_module
package_json = JSON.parse(File.read("#{all_module}/package.json"))
old_version = package_json['version']
if old_version.end_with?('-SNAPSHOT')
File.open("#{all_module}/pom.xml") do |pom_file|
pom = Document.new(pom_file)
new_version = XPath.first(pom, '/project/version/text()')
package_json['version'] = new_version
puts "Set #{all_module}/package.json version #{old_version} to #{new_version}."
File.open("#{all_module}/package.json", 'w') do |f|
f.write(JSON.pretty_generate(package_json))
end
end
end
end
end
end
end
# Calculates the new version number for the maven artifact or node dependency
def module_release_version(module_path, tag_version, pr_version, build_number, is_node_module)
if is_node_module
package_json = JSON.parse(File.read("#{module_path}/package.json"))
old_version = SemVersion.new(package_json['version'])
else
module_artifact = maven_module_info(module_path)
old_version = SemVersion.new(module_artifact.version.to_s)
end
if !tag_version.to_s.strip.empty?
# Remove leading v
if tag_version.start_with?('v')
tag_version = tag_version[1..-1]
end
if module_path == '' || module_path == '.' || module_path == 'bom' || module_path == 'parent' || module_path == 'grandparent'
return tag_version
else
# foo/bar/baz/qux --> foo
root_module_path = module_path.split(/\//, 2).first
commit_count = `git rev-list --count HEAD -- #{root_module_path}`.to_s.strip
commit_hash = `git rev-list HEAD -- #{root_module_path} | head -1`.to_s.strip[0, 6]
return "#{old_version.major_number}.#{old_version.minor_number}.#{commit_count}-x#{commit_hash}"
end
elsif !pr_version.to_s.strip.empty?
if is_node_module
return "#{old_version.major_number}.#{old_version.minor_number}.0-PR#{pr_version}.#{build_number}"
else
return "40.#{pr_version}-SNAPSHOT"
end
else
if is_node_module
return "#{old_version.major_number}.#{old_version.minor_number}.0-SNAPSHOT.#{build_number}"
else
return nil
end
end
end
# If an express version has not been set already (because it wasn't in the list
# of modified modules) then explicitly sets the express/site/pom.xml parent
# to point to the current brightspot-parent (bypassing express-parent) to ensure
# that the s3deploy function builds the express site WAR with the local
# dependencies from this build.
def prepare_s3deploy_versions(modified_modules)
unless modified_modules.include?('express/site')
File.open('parent/pom.xml') do |parent_pom_file|
parent_pom = Document.new(parent_pom_file)
parent_group_id = XPath.first(parent_pom, '/project/groupId/text()')
if parent_group_id == nil
parent_group_id = XPath.first(parent_pom, '/project/parent/groupId/text()')
end
parent_artifact_id = XPath.first(parent_pom, '/project/artifactId/text()')
parent_version = XPath.first(parent_pom, '/project/version/text()')
File.open('express/site/pom.xml') do |express_site_pom_file|
express_site_pom = Document.new(express_site_pom_file)
express_site_group_id_elmt = XPath.first(express_site_pom, '/project/parent/groupId')
express_site_artifact_id_elmt = XPath.first(express_site_pom, '/project/parent/artifactId')
express_site_version_elmt = XPath.first(express_site_pom, '/project/parent/version')
express_site_relative_path_elmt = XPath.first(express_site_pom, '/project/parent/relativePath')
express_site_group_id_elmt.text = parent_group_id
express_site_artifact_id_elmt.text = parent_artifact_id
express_site_version_elmt.text = parent_version
express_site_relative_path_elmt.text = '../../parent/pom.xml'
File.open('express/site/pom.xml', 'w') do |f|
formatter = REXML::Formatters::Default.new
formatter.write(express_site_pom, f)
end
puts "Set express/site/pom.xml parent to #{parent_group_id}:#{parent_artifact_id}:#{parent_version} for S3 Deploy."
end
end
end
end
# Deploys the express/site WAR file to S3 to power the /_deploy servlet.
def s3deploy
system_stdout('mvn -f express/site/pom.xml package'\
' -B'\
' -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn'\
' -Dmaven.test.skip=true')
if $? != 0 then raise ArgumentError, 'Failed to compile the Express Site WAR file for S3 deploy!' end
ENV['DEPLOY_SOURCE_DIR'] = "#{ENV['TRAVIS_BUILD_DIR']}/express/site/target"
system_stdout('git clone https://github.com/perfectsense/travis-s3-deploy.git')
if $? != 0 then raise ArgumentError, 'Failed to clone travis-s3-deploy repo!' end
system_stdout('travis-s3-deploy/deploy.sh')
if $? != 0 then raise ArgumentError, 'Failed to deploy to S3!' end
end
def sonar_goals(phases)
# ENV["SONAR_TOKEN"] ? "org.jacoco:jacoco-maven-plugin:prepare-agent #{phases} sonar:sonar" : phases
phases
end
def deploy
puts "REBUILD: " + (ENV["REBUILD"] || "")
puts "TRAVIS_COMMIT_RANGE: " + (ENV["TRAVIS_COMMIT_RANGE"] || "")
puts "TRAVIS_TAG: " + (ENV["TRAVIS_TAG"] || "")
puts "TRAVIS_REPO_SLUG: " + (ENV["TRAVIS_REPO_SLUG"] || "")
puts "TRAVIS_PULL_REQUEST: " + (ENV["TRAVIS_PULL_REQUEST"] || "")
puts "TRAVIS_PULL_REQUEST_BRANCH: " + (ENV["TRAVIS_PULL_REQUEST_BRANCH"] || "")
puts "TRAVIS_BRANCH: " + (ENV["TRAVIS_BRANCH"] || "")
ENV["MAVEN_OPTS"] = "-Xmx2g"
rebuild = ENV["REBUILD"].to_s.casecmp("true") == 0
commit_range = ENV["TRAVIS_COMMIT_RANGE"]
tag_version = ENV["TRAVIS_TAG"]
pr_version = ENV["TRAVIS_PULL_REQUEST"].to_s.eql?("false") ? '' : ENV["TRAVIS_PULL_REQUEST"]
build_number = ENV["TRAVIS_BUILD_NUMBER"]
if ENV["TRAVIS_REPO_SLUG"].to_s.start_with?("perfectsense/")
if not ENV["TRAVIS_TAG"].to_s.strip.empty?
puts 'Preparing RELEASE version...'
prepare_release_versions(commit_range, tag_version, pr_version, build_number, false)
update_archetype_versions
verify_no_release_snapshots
verify_bom_dependencies
system_stdout('mvn clean install'\
' -B'\
' -Dmaven.test.skip=true'\
' -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn'\
' -Plibrary')
if $? != 0 then raise ArgumentError, 'Failed to install release!' end
newly_versioned_modules = get_newly_versioned_modules
puts "newly_versioned_modules: #{newly_versioned_modules.join(' ')}"
if newly_versioned_modules.length > 0
puts "Deploying #{newly_versioned_modules.length} artifacts to artifactory."
system_stdout("DEPLOY_SKIP_UPLOAD=#{DEBUG_SKIP_UPLOAD}"\
' DEPLOY=true'\
' mvn deploy'\
' -B'\
' -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn'\
' -Dmaven.test.skip=true'\
' -DdeployAtEnd=false'\
" -Dmaven.deploy.skip=#{DEBUG_SKIP_UPLOAD}"\
' --settings=$(dirname $(pwd)/$0)/etc/settings.xml'\
' -Pdeploy'\
" -pl #{newly_versioned_modules.join(",")}")
if $? != 0 then raise ArgumentError, 'Failed to deploy release!' end
s3deploy
else
puts 'Nothing new to deploy to artifactory.'
end
else
if ENV["TRAVIS_PULL_REQUEST"].to_s.eql?('false')
if ENV["TRAVIS_BRANCH"].to_s.eql?('master')
puts 'Deploying SNAPSHOT to Maven repository...'
prepare_release_versions(commit_range, tag_version, pr_version, build_number, false)
update_archetype_versions
verify_bom_dependencies
system_stdout("DEPLOY_SKIP_UPLOAD=#{DEBUG_SKIP_UPLOAD}"\
' DEPLOY=true'\
" mvn #{sonar_goals('deploy')}"\
' -B'\
' -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn'\
' -DdeployAtEnd=false'\
" -Dmaven.deploy.skip=#{DEBUG_SKIP_UPLOAD}"\
' --settings=$(dirname $(pwd)/$0)/etc/settings.xml'\
' -Pdeploy')
if $? != 0 then raise ArgumentError, 'Failed to deploy SNAPSHOT to artifactory!' end
s3deploy
elsif ENV["TRAVIS_BRANCH"].to_s.start_with?("release/")
puts 'Building release branch (no deploy)...'
prepare_release_versions(commit_range, tag_version, pr_version, build_number, false)
update_archetype_versions
verify_bom_dependencies
system_stdout("mvn #{sonar_goals('install')}"\
' -B'\
' -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn')
if $? != 0 then raise ArgumentError, 'Failed to build release branch!' end
else
puts 'Branch is not associated with a PR, nothing to do...'
end
else
if ENV["TRAVIS_PULL_REQUEST_BRANCH"].to_s.start_with?('pr-snapshot/')
modified_modules = get_project_diff_list(commit_range)
puts "modified_modules: #{modified_modules.join(" ")}"
if modified_modules.length > 0
puts 'Preparing pull request snapshot...'
# Only lock snapshots if the PR's destination branch is NOT master.
lock_snapshots = !ENV["TRAVIS_BRANCH"].to_s.eql?('master')
prepare_release_versions(commit_range, tag_version, pr_version, build_number, lock_snapshots)
update_archetype_versions
verify_bom_dependencies
puts 'Installing pull request snapshot...'
system_stdout("mvn #{sonar_goals('clean install')}"\
' -B'\
' -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn'\
' -Plibrary'\
" -pl .,parent,bom,grandparent,#{modified_modules.join(',')}")
if $? != 0 then raise ArgumentError, 'Failed to install pull request snapshot build!' end
puts 'Deploying pull request snapshot...'
system_stdout("DEPLOY_SKIP_UPLOAD=#{DEBUG_SKIP_UPLOAD}"\
' DEPLOY=true'\
' mvn clean deploy'\
' -B'\
' -Dmaven.test.skip=true'\
' -DdeployAtEnd=false'\
" -Dmaven.deploy.skip=#{DEBUG_SKIP_UPLOAD}"\
' --settings=$(dirname $(pwd)/$0)/etc/settings.xml'\
' -Pdeploy'\
" -pl .,parent,bom,grandparent,#{modified_modules.join(',')}")
if $? != 0 then raise ArgumentError, 'Failed to deploy pull request snapshot build!' end
s3deploy
else
puts 'No modules to build...'
end
else
modified_modules = get_project_diff_list(commit_range)
puts "modified_modules: #{modified_modules.join(" ")}"
if modified_modules.length > 0
puts 'Preparing pull request...'
prepare_release_versions(commit_range, tag_version, '', build_number, false)
update_archetype_versions
verify_bom_dependencies
puts 'Building pull request...'
if ENV["TRAVIS_BRANCH"].to_s.eql?('master')
system_stdout(
" mvn #{sonar_goals('install')}"\
' -B'\
' -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn'\
' -Plibrary'\
" -pl .,parent,bom,grandparent,#{modified_modules.join(',')}")
else
puts 'Target branch is not master, building ALL modules instead!'
system_stdout(
" mvn #{sonar_goals('install')}"\
' -B'\
' -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn'\
' -Plibrary')
end
if $? != 0 then raise ArgumentError, 'Failed to build pull request!' end
s3deploy
else
puts 'No modules to build...'
end
end
end
end
end
end
if __FILE__ == $0
deploy
end