forked from rhq-project/rhq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.sh
executable file
·647 lines (534 loc) · 22.3 KB
/
release.sh
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
#!/bin/bash
#========================================================================================
# Usage:
# See usage function.
#
# Description:
# RHQ release script. Supports tagging and branching model described in the community
# wiki:
# http://www.rhq-project.org/display/RHQ/Source+Control
#
# Options:
# See usage function.
#========================================================================================
#include the utility library
source `dirname $0`/rhq_bash.lib
#========================================================================================
# Description: Display usage information then abort the script.
#========================================================================================
usage()
{
USAGE=$(
cat << EOF
USAGE: release.sh OPTIONS
--release-version=version [REQUIRED]
The release version to be tagged by this script.
--release-branch=git_branch [REQUIRED]
Git branch to be used as base for tagging and/or branching.
--release-type=community|enterprise [REQUIRED]
Type of release.
--development-version=version [OPTIONAL]
The version under which development will continue after tagging or branching on the original branch. If development version is not specified then the branch is considered terminal branch and no further development is expected on the original branch.
--test-mode [OPTIONAL, DEFAULT]
Run this script in test mode. Create a test branch from release branch and perform tagging and version updates on this test branch.
--production-mode [OPTIONAL]
Run this script in production mode. Follow the official branching and tagging model.
--mode=test|production [OPTIONAL]
Used to directly set the script mode.
--branch [OPTIONAL]
Branch from release branch before tagging the release version. And updated development version on original branch.
--tag [OPTIONAL, DEFAULT]
Use the release branch to tag the release version. And update development version on the same branch.
--scm-strategy=tag|branch [OPTIONAL]
Used to directly set the scm strategy.
--extra-profile=extra_profile [OPTIONAL]
An extra maven profile to be used for all the maven commands.
--debug=[true|false] [OPTIONAL]
Set maven in debug mode. Default is false; true if option specified without argument.
--workspace=workspace_to_use [OPTIONAL]
Override the workspace used by the script.
--tag-name=tag_name [OPTIONAL]
Override the default tag name generated by this script. Using the default tag name is the preferred alternative. Please use this option only if absolutely needed.
--override-tag=[true|false] [OPTIONAL]
Override the tag if it already exists remotely. Default is false; true if option specified without argument.
EOF
)
EXAMPLE="release.sh --release-type=\"enterprise\" --release-version=\"5.0.0.GA\" --development-version=\"5.0.1-SNAPSHOT\" --release-branch=\"stefan/release_test\" --branch"
abort "$@" "$USAGE" "$EXAMPLE"
}
#========================================================================================
# Description: Validate and parse input arguments
#========================================================================================
parse_and_validate_options()
{
print_function_information $FUNCNAME
RELEASE_VERSION=
DEVELOPMENT_VERSION=
RELEASE_BRANCH=
RELEASE_TYPE="community"
MODE="test"
SCM_STRATEGY="tag"
EXTRA_MAVEN_PROFILE=
DEBUG_MODE=false
OVERRIDE_TAG=false
TERMINAL_BRANCH=false
RELEASE_TAG=
short_options="h"
long_options="help,release-version:,development-version:,release-branch:,release-type:,test-mode,production-mode,mode:,branch,tag,scm-strategy:,extra-profile:,debug::,workspace:,override-tag::,tag-name:"
PROGNAME=${0##*/}
ARGS=$(getopt -s bash --options $short_options --longoptions $long_options --name $PROGNAME -- "$@" )
eval set -- "$ARGS"
while true; do
case $1 in
-h|--help)
usage
;;
--release-version)
shift
RELEASE_VERSION="$1"
shift
;;
--development-version)
shift
DEVELOPMENT_VERSION="$1"
shift
;;
--release-branch)
shift
RELEASE_BRANCH="$1"
shift
;;
--release-type)
shift
RELEASE_TYPE="$1"
shift
;;
--test-mode)
MODE="test"
shift
;;
--production-mode)
MODE="production"
shift
;;
--mode)
shift
MODE="$1"
shift
;;
--tag)
SCM_STRATEGY="tag"
shift
;;
--branch)
SCM_STRATEGY="branch"
shift
;;
--scm-strategy)
shift
SCM_STRATEGY="$1"
shift
;;
--extra-profile)
shift
EXTRA_MAVEN_PROFILE="$1"
shift
;;
--debug)
shift
case "$1" in
true)
DEBUG_MODE=true
shift
;;
false)
DEBUG_MODE=false
shift
;;
"")
DEBUG_MODE=true
shift
;;
*)
DEBUG_MODE=false
shift
;;
esac
;;
--workspace)
shift
WORKSPACE=$1
shift
;;
--tag-name)
shift
RELEASE_TAG=$1
shift
;;
--override-tag)
shift
case "$1" in
true)
OVERRIDE_TAG=true
shift
;;
false)
OVERRIDE_TAG=false
shift
;;
"")
OVERRIDE_TAG=true
shift
;;
*)
OVERRIDE_TAG=false
shift
;;
esac
;;
--)
shift
break
;;
*)
usage
;;
esac
done
if [ -z "$RELEASE_VERSION" ];
then
usage "Release version not specified!"
fi
if [ -z "$DEVELOPMENT_VERSION" ];
then
TERMINAL_BRANCH=true;
fi
if [ -z "$RELEASE_BRANCH" ];
then
usage "Release branch not specified!"
fi
if [ "$RELEASE_TYPE" != "community" ] && [ "$RELEASE_TYPE" != "enterprise" ];
then
usage "Invalid release type: $RELEASE_TYPE (valid release types are 'community' or 'enterprise')"
fi
if [ "$MODE" != "test" ] && [ "$MODE" != "production" ];
then
usage "Invalid script mode: $MODE (valid modes are 'test' or 'production')"
fi
if [ "$SCM_STRATEGY" != "tag" ] && [ "$SCM_STRATEGY" != "branch" ];
then
usage "Invalid scm strategy: $SCM_STRATEGY (valid scm strategies are 'tag' or 'branch')"
fi
print_centered "Script Options"
script_options=( "RELEASE_VERSION" "DEVELOPMENT_VERSION" "RELEASE_BRANCH" "RELEASE_TYPE" \
"MODE" "SCM_STRATEGY" "TERMINAL_BRANCH" "RELEASE_TAG")
print_variables "${script_options[@]}"
}
#========================================================================================
# Description: Set all the local and environment variables required by the script.
#========================================================================================
set_local_and_environment_variables()
{
print_function_information $FUNCNAME
# Set environment variables
MAVEN_OPTS="-Xms512M -Xmx1024M"
[[ $("$JAVA_HOME/bin/java" -version 2>&1 | awk -F '"' '/version/ {print $2}') > "1.8" ]] || MAVEN_OPTS="$MAVEN_OPTS -XX:PermSize=128M -XX:MaxPermSize=256M"
export MAVEN_OPTS
# Set various local variables
if [ -n "$WORKSPACE" ]; then
echo "Running script in a Hudson job."
MAVEN_LOCAL_REPO_DIR="$WORKSPACE/.m2/repository"
if [ ! -d "$MAVEN_LOCAL_REPO_DIR" ]; then
mkdir -p "$MAVEN_LOCAL_REPO_DIR"
fi
MAVEN_SETTINGS_FILE="$WORKSPACE/.m2/settings.xml"
else
MAVEN_LOCAL_REPO_DIR="$HOME/.m2/repository"
MAVEN_SETTINGS_FILE="$HOME/.m2/settings.xml"
fi
MAVEN_ARGS="--settings $MAVEN_SETTINGS_FILE -Dmaven.repo.local=$MAVEN_LOCAL_REPO_DIR --batch-mode --errors"
if [ -n "$EXTRA_MAVEN_PROFILE" ];
then
MAVEN_ARGS="$MAVEN_ARGS --activate-profiles $EXTRA_MAVEN_PROFILE,enterprise,dist,release"
else
MAVEN_ARGS="$MAVEN_ARGS --activate-profiles enterprise,dist,release"
fi
if [ "$RELEASE_TYPE" = "enterprise" ];
then
MAVEN_ARGS="$MAVEN_ARGS -Dexclude-webdav -Pdisable-tags -DTagManager=false -Dfiltered.location=target/filtered-sources/java"
fi
if [ -n "$DEBUG_MODE" ]; then
echo "Maven debug enabled"
MAVEN_ARGS="$MAVEN_ARGS --debug"
fi
if [ -z "$RELEASE_TAG" ]; then
TAG_PREFIX="RHQ"
TAG_VERSION=`echo $RELEASE_VERSION | sed 's/[\.|-]/_/g'`
RELEASE_TAG="${TAG_PREFIX}_${TAG_VERSION}"
fi
# Set the system character encoding to ISO-8859-1 to ensure i18log reads its
# messages and writes its resource bundle properties files in that encoding,
# since that is how the German and French I18NMessage annotation values are
# encoded and the encoding used by i18nlog to read in resource bundle
# property files.
LANG=en_US.iso8859
export LANG
# Print out a summary of the environment.
print_centered "Environment Variables"
environment_variables=( "JAVA_HOME" "M2_HOME" "MAVEN_OPTS" "PATH" "LANG" )
print_variables "${environment_variables[@]}"
print_centered "Local Variables"
local_variables=( "MAVEN_LOCAL_REPO_DIR" "MAVEN_SETTINGS_FILE" "MAVEN_ARGS" \
"RELEASE_TAG" )
print_variables "${local_variables[@]}"
}
#========================================================================================
# Description: Perform version update process and test the outcome by building
# from source.
#========================================================================================
run_release_version_and_tag_process()
{
print_function_information $FUNCNAME
echo "1) Perform a test build before changing version."
mvn clean install $MAVEN_ARGS -Ddbreset
[ "$?" -ne 0 ] && abort "Test build failed. Please see output for details, fix any issues, then try again."
echo "2) Run a maven cleanup to remove all the artifacts from previous build, this cleans module target dirs."
mvn clean $MAVEN_ARGS
[ "$?" -ne 0 ] && abort "Failed to cleanup snbapshot jars produced by test build from module target dirs. Please see above Maven output for details, fix any issues, then try again."
echo "3) Increment version on all poms."
mvn versions:set versions:use-releases -DnewVersion=$RELEASE_VERSION -DallowSnapshots=false -DgenerateBackupPoms=false
[ "$?" -ne 0 ] && abort "Version set failed. Please see output for details, fix any issues, then try again."
# This logic updates the rhq.pom.version value used for independent versioning and cumulative patching with .Z stream.
echo " Updating rhq.pom.version to [$RELEASE_VERSION] in root pom as well.";
sed -i -e "s|\(<rhq.pom.version>\).*\(</rhq.pom.version>\)|\1$RELEASE_VERSION\2|" "pom.xml"
echo "4) Perform a test build with the new version."
mvn clean install $MAVEN_ARGS -DskipTests=true -Ddbsetup-do-not-check-schema=true
[ "$?" -ne 0 ] && abort "Maven build for new version failed. Please see output for details, fix any issues, then try again."
echo "6) Cleanup again after this test build before commiting changes."
mvn clean $MAVEN_ARGS
[ "$?" -ne 0 ] && abort "Failed to cleanup snbapshot jars produced by test build from module target dirs. Please see above Maven output for details, fix any issues, then try again."
echo "7) Commit the change in version; if everything went well so far then this is a good tag."
git diff --exit-code
if [ "$?" -ne 0 ];
then
git add -u
[ "$?" -ne 0 ] && abort "Adding modified files to commit failed."
git commit -m "tag $RELEASE_TAG"
[ "$?" -ne 0 ] && abort "The commit with version modified files failed."
else
echo "No changes detected to commit. There was no effective change in release version."
fi
echo "8) Tag the current source."
if [ $OVERRIDE_TAG == true ];
then
git tag --force "$RELEASE_TAG"
[ "$?" -ne 0 ] && abort "Force tagging failed."
else
git tag "$RELEASE_TAG"
[ "$?" -ne 0 ] && abort "Tagging failed"
fi
if [ "$SCM_STRATEGY" = "tag" ];
then
echo "9) Merge any remote changes into the local branch to be able to push tag and version change. This will fail if the merge process requires manual merges."
git ls-remote --exit-code origin $BUILD_BRANCH
if [ "$?" -eq 0 ];
then
git pull origin "$BUILD_BRANCH"
[ "$?" -ne 0 ] && abort "Merge with remote $BUILD_BRANCH failed."
fi
else
echo "9) DID NOT execute remote changes merge because local branch was created. No need to merge back changes."
fi
echo "10) Everything went well so far, all the changes are now pushed!!!"
git push origin "refs/heads/$BUILD_BRANCH"
[ "$?" -ne 0 ] && abort "$BUILD_BRANCH branch push to origin failed."
git push origin "refs/tags/$RELEASE_TAG"
[ "$?" -ne 0 ] && abort "$BUILD_BRANCH branch push to origin failed."
}
#========================================================================================
# Description: Update the version for the development branch.
#========================================================================================
update_development_version()
{
print_function_information $FUNCNAME
if [ $TERMINAL_BRANCH == true ];
then
echo "Release branch $RELEASE_BRANCH is a terminal branch. Version on release branch no updated for future development!";
return 0;
fi
echo "1) Set version to the current development version"
mvn versions:set versions:use-releases -DnewVersion=$DEVELOPMENT_VERSION -DallowSnapshots=false -DgenerateBackupPoms=false
[ "$?" -ne 0 ] && abort "Version set failed. Please see output for details, fix any issues, then try again."
echo "2) Commit the change in version."
git add -u
[ "$?" -ne 0 ] && abort "Adding modified files to commit failed."
git commit -m "development RHQ_$DEVELOPMENT_VERSION"
[ "$?" -ne 0 ] && abort "The commit with version modified files failed."
echo "3) Merge any remote changes into the local branch to be able to push tag and version change. This will fail if the merge process requires manual merges."
git pull origin "$BUILD_BRANCH"
[ "$?" -ne 0 ] && abort "Merge with remote $BUILD_BRANCH failed."
echo "4) If everything went well so far than means all the changes can be pushed!!!"
git push origin "refs/heads/$BUILD_BRANCH"
[ "$?" -ne 0 ] && abort "$BUILD_BRANCH branch push to origin failed."
}
#========================================================================================
# Description: Perform tag verification process
# 1) if the tag already exists on the remote repo than abort the script
# 2) if the tag exists only locally then delete it, that means there was
# error during the previous run of this script. Tags are committed to
# repo only this script runs successfully.
#========================================================================================
verify_tags()
{
print_function_information $FUNCNAME
# If the specified tag already exists remotely and we're in production mode, then abort. If it exists and
# we're in test mode, delete it
EXISTING_REMOTE_TAG=`git ls-remote --tags origin "$RELEASE_TAG"`
if [ -n "$EXISTING_REMOTE_TAG" ];
then
if [ $OVERRIDE_TAG == true ];
then
echo "A remote tag named $RELEASE_TAG already exists, but will be overriden."
else
abort "A remote tag named $RELEASE_TAG already exists - aborting"
fi
fi
# See if the specified tag already exists locally - if so, delete it (even if in production mode).
# If the tag is just local then there were errors during the last run; no harm in removing it.
EXISTING_LOCAL_TAG=`git tag -l "$RELEASE_TAG"`
if [ -n "$EXISTING_LOCAL_TAG" ];
then
echo "A local tag named $RELEASE_TAG already exists - deleting it..."
git tag -d "$RELEASE_TAG"
[ "$?" -ne 0 ] && abort "Failed to delete local tag ($RELEASE_TAG)."
fi
}
#========================================================================================
# Description: Run the validation process for all the system utilities needed by
# the script. At the end print the version of each utility.
#========================================================================================
validate_system_utilities()
{
print_function_information $FUNCNAME
# TODO: Check that JDK version is < 1.7.
validate_java_6
validate_java_5
validate_maven
validate_git
print_centered "Program Versions"
program_versions=("git --version" "java -version" "mvn --version")
print_program_versions "${program_versions[@]}"
}
#========================================================================================
# Description: Prints the release information.
#========================================================================================
print_release_information()
{
print_function_information $FUNCNAME
echo
print_centered "Release Info"
echo "Version: $RELEASE_VERSION"
echo "Branch URL: https://github.com/rhq-project/rhq/commits/$RELEASE_BRANCH"
echo "Tag URL: https://github.com/rhq-project/rhq/commits/$RELEASE_TAG"
print_centered "="
}
#========================================================================================
# Description: Checkout release branch.
#========================================================================================
checkout_release_branch()
{
print_function_information $FUNCNAME
# Checkout the source from git, assume that the git repo is already cloned
git status >/dev/null 2>&1
GIT_STATUS_EXIT_CODE=$?
# Note, git 1.6 and earlier returns an exit code of 1, rather than 0, if there are any uncommitted changes,
# and git 1.7 returns 0, so we check if the exit code is less than or equal to 1 to determine if current folder
# is truly a git working copy.
if [ "$GIT_STATUS_EXIT_CODE" -le 1 ];
then
echo "Checking out a clean copy of the release branch ($RELEASE_BRANCH)..."
git fetch origin "$RELEASE_BRANCH"
[ "$?" -ne 0 ] && abort "Failed to fetch release branch ($RELEASE_BRANCH)."
git checkout --track "origin/$RELEASE_BRANCH"
if [ "$?" -ne 0 ];
then
git checkout "$RELEASE_BRANCH"
fi
[ "$?" -ne 0 ] && abort "Failed to checkout release branch ($RELEASE_BRANCH)."
git reset --hard "origin/$RELEASE_BRANCH"
[ "$?" -ne 0 ] && abort "Failed to reset release branch ($RELEASE_BRANCH)."
git clean -dxf
[ "$?" -ne 0 ] && abort "Failed to clean release branch ($RELEASE_BRANCH)."
git pull origin $RELEASE_BRANCH
[ "$?" -ne 0 ] && abort "Failed to update release branch ($RELEASE_BRANCH)."
else
echo "Current folder does not appear to be a git working directory ('git status' returned $GIT_STATUS_EXIT_CODE) - removing it so we can freshly clone the repo..."
fi
}
#========================================================================================
# Description: Checkout or create the build branch for the release process.
#========================================================================================
checkout_build_branch_for_release()
{
print_function_information $FUNCNAME
# if this is a test build then create a temporary build branch off of RELEASE_BRANCH. This allows checkins to
# continue in RELEASE_BRANCH without affecting the release plugin work, which will fail if the branch contents
# change before it completes.
if [ "$MODE" = "test" ];
then
BUILD_BRANCH="${RELEASE_BRANCH}-test-build"
# delete the branch if it exists, so we can recreate it fresh
EXISTING_BUILD_BRANCH=`git ls-remote --heads origin "$BUILD_BRANCH"`
if [ -n "$EXISTING_BUILD_BRANCH" ];
then
echo "Deleting remote branch origin/$BUILD_BRANCH"
git branch -D -r "origin/$BUILD_BRANCH"
echo "Deleting local branch $BUILD_BRANCH"
git branch -D "$BUILD_BRANCH"
fi
echo "Creating and checking out local branch $BUILD_BRANCH from $RELEASE_BRANCH"
git checkout -b "$BUILD_BRANCH"
else
if [ "$SCM_STRATEGY" = "tag" ];
then
BUILD_BRANCH="${RELEASE_BRANCH}"
else
BUILD_BRANCH="release-$RELEASE_VERSION"
EXISTING_BUILD_BRANCH=`git ls-remote --heads origin "$BUILD_BRANCH"`
if [ -n "$EXISTING_BUILD_BRANCH" ];
then
abort "Remote repository already contains $BUILD_BRANCH."
fi
echo "Creating and checking out local branch $BUILD_BRANCH from $RELEASE_BRANCH"
git checkout -b "$BUILD_BRANCH"
fi
fi
}
#========================================================================================
# Description: Checkout or create the build branch for updating the development version.
#========================================================================================
checkout_build_branch_for_development()
{
print_function_information $FUNCNAME
if [ "$MODE" = "production" ];
then
if [ "$SCM_STRATEGY" = "branch" ];
then
checkout_release_branch
BUILD_BRANCH="${RELEASE_BRANCH}"
fi
fi
}
############ MAIN SCRIPT ############
parse_and_validate_options $@
set_script_debug_mode $DEBUG_MODE
validate_system_utilities
set_local_and_environment_variables
verify_tags
checkout_release_branch
checkout_build_branch_for_release
run_release_version_and_tag_process
checkout_build_branch_for_development
update_development_version
print_release_information
unset_script_debug_mode $DEBUG_MODE