-
Notifications
You must be signed in to change notification settings - Fork 1
/
release.sh
executable file
·492 lines (412 loc) · 15.1 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
#!/usr/bin/env bash
# echo "Your script args ($#) are: $@"
# get required functions and vars
source ./.dbFlow/lib.sh
# set project-settings from build.env if exists
if [[ -e ./build.env ]]; then
source ./build.env
fi
CUR_DIRECTORY=$(pwd)
# Log Location on Server.
LOG_LOCATION=${CUR_DIRECTORY}
LOG_FILENAME=release.log
rm -f "${LOG_LOCATION}/${LOG_FILENAME}"
exec > >( tee >( sed 's/\x1B\[[0-9;]*[JKmsu]//g' >> "${LOG_LOCATION}/${LOG_FILENAME}" ) )
RLS_BUILDBRANCH=${BUILD_BRANCH:-build}
# get branch name
{ #try
current_branch=$(git branch --show-current)
} || { # catch
current_branch="develop"
}
exec 2>&1
notify() {
[[ ${1} = 0 ]] || echo ❌ EXIT "${1}"
# you can notify some external services here,
# ie. Slack webhook, Github commit/PR etc.
}
function check_vars() {
# check require vars from build.env
do_exit="NO"
####
if [[ ${do_exit} == "YES" ]]; then
echo_warning "aborting"
exit 1;
fi
}
function retry() {
local cnt=0;
while [[ $cnt -lt 10 ]]; do
if [[ -f ".git/index.lock" ]]; then
echo -n ".";
sleep 0.5;
((cnt++));
else
"$@"; # do the command params
return; # quit the function
fi
done
echo ".git/index.lock exists and is locked!"
exit 1;
}
usage() {
echo -e "${BWHITE}.dbFlow/release.sh${NC} - creates a release artifact for targetbranch and place it into depot"
echo ""
echo -e "This script will help you to create a release in no time. You only have to specify the target branch. "
echo -e "The source branch will be the current one or the one you selected by option. "
echo -e "If you specify the -b / --build flag, the target branch is cloned into it and the initial release "
echo -e "of the predecessor, as well as the patch of the current and the initial of the current release are built."
echo ""
echo -e "${BWHITE}Usage:${NC}"
echo -e " $0 --source <branch> --target <branch> --version <label> [--build]"
echo ""
echo -e "${BWHITE}Options:${NC}"
echo -e " -h | --help - Show this screen"
echo -e " -d | --debug - Show additionaly output messages"
echo -e " -s | --source <branch> - Optional Source branch (default current)to merge target branch into and determine the files to include in patch"
echo -e " -t | --target <branch> - Required Target branch to reflect the predecessor of the source branch"
echo -e " -g | --gate - Optional Gate branch to free source branch. If this is set, then the source branch will be merged into that"
echo -e " -v | --version <label> - Required label of version this artifact represents (optional when buildflag is submitted)"
echo -e " - Set <label> to major, minor or patch and the next semantic version is calculated automatically"
echo -e " - Set <label> to current to keep the latest semantic version"
echo ""
echo -e " -b | --build - Optional buildflag to create 3 artifact for using as nighlybuilds"
echo -e " -a | --apply <folder> - Optional path to apply the build(s) when buildflag is set"
echo -e " -k | --keep - Optional flag to keep folders in depot path (will be passed to build.sh)"
echo -e " -f | --forceddl - Optional flag to switch off checking for new table-file through git itself."
echo -e " This will run table_ddl scripts when matching table is present in patch mode"
echo ""
echo -e "${BWHITE}Examples:${NC}"
echo -e " $0 --target release --version 1.2.3"
echo -e " $0 --source release --target test --version 1.2.3"
echo -e " $0 --source develop --target master -b"
echo -e " $0 --source develop --gate release --target test --version 2.0.3 --apply ../instances/test"
exit 1
}
log() {
branch=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
echo -e "${YELLOW}OnBranch: ${NC}${PURPLE}$branch${NC} ${YELLOW}>>> ${NC}${BLUE}${1}${NC}"
}
get_next_version() {
local inc_by_type="$1"
local regexp='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)'
if [ -z "$inc_by_type" ]; then
inc_by_type="patch"
fi
local last_version=$(git describe --tags $(git rev-list --tags --max-count=1))
local MAJOR=$(echo $last_version | sed -e "s#$regexp#\1#")
local MINOR=$(echo $last_version | sed -e "s#$regexp#\2#")
local PATCH=$(echo $last_version | sed -e "s#$regexp#\3#")
case "$inc_by_type" in
"major")
((MAJOR += 1))
((MINOR = 0))
((PATCH = 0))
;;
"minor")
((MINOR += 1))
((PATCH = 0))
;;
"patch")
((PATCH += 1))
;;
"current")
((PATCH += 0))
;;
esac
local NEXT_VERSION="$MAJOR.$MINOR.$PATCH"
echo "$NEXT_VERSION"
}
build_release() {
starting_branch=$(git branch --show-current)
version_previous="0.0.0"
version_next="${RLS_VERSION}"
apply_tasks=()
if [[ ${RLS_INC_TYPE} != "-" ]]; then
RLS_INC_TYPE=" - (${RLS_INC_TYPE})"
else
RLS_INC_TYPE=""
fi
log "${BWHITE}Sourcebranch: ${NC}${RLS_SOURCE_BRANCH}"
log "${BWHITE}Gatebranch: ${NC}${RLS_GATE_BRANCH}"
log "${BWHITE}Targetbranch: ${NC}${RLS_TARGET_BRANCH}"
log "${BWHITE}Version: ${NC}${RLS_VERSION}${RLS_INC_TYPE}"
log "${BWHITE}Buildtest: ${NC}${RLS_BUILD}"
log "${BWHITE}Force TableDDL: ${NC}${RLS_FORCE_DDL}"
if [[ ${RLS_BUILD} == 'Y' ]]; then
log "${BWHITE}Buildbranch: ${NC}${RLS_BUILDBRANCH}"
version_next="0.0.1"
fi
# switch to source branch
log "change to Branch: ${RLS_SOURCE_BRANCH}"
retry git checkout "${RLS_SOURCE_BRANCH}"
{ #try
pulled=$(git pull)
} || { # catch
pulled="no"
}
log "Head is $(git rev-parse --short HEAD)"
# merge through gate
if [[ ${RLS_GATE_BRANCH} != ${RLS_SOURCE_BRANCH} ]]; then
log "change to Branch: ${RLS_GATE_BRANCH}"
if git show-ref --quiet refs/heads/"${RLS_GATE_BRANCH}"; then
# exists
retry git checkout "${RLS_GATE_BRANCH}"
{ #try
pulled=$(git pull)
} || { # catch
pulled="no"
}
log "merging changes from $RLS_SOURCE_BRANCH with : --strategy-option theirs"
retry git merge "${RLS_SOURCE_BRANCH}" --strategy-option theirs
retry git push
else
retry git checkout -b "${RLS_GATE_BRANCH}"
retry git push --set-upstream origin ${RLS_GATE_BRANCH}
fi
fi
# switch to target branch
log "change to Branch: ${RLS_TARGET_BRANCH}"
retry git checkout "${RLS_TARGET_BRANCH}"
{ #try
pulled=$(git pull)
} || { # catch
pulled="no"
}
log "Head is $(git rev-parse --short HEAD)"
local flags=()
if [[ ${RLS_FORCE_DDL} != "-" ]]; then
flags+=("${RLS_FORCE_DDL}")
fi
if [[ ${keep} != "-" ]]; then
flags+=("${keep}")
fi
if [[ ${RLS_BUILD} == 'Y' ]]; then
# remove build branch
if git show-ref --quiet refs/heads/"${RLS_BUILDBRANCH}"; then
log "Removing branch ${RLS_BUILDBRANCH}"
retry git branch -D "${RLS_BUILDBRANCH}"
fi
# create a new build branch out of target branch
log "Checkout new Branch ${RLS_BUILDBRANCH}"
retry git checkout -b "${RLS_BUILDBRANCH}"
# build initial patch
log "building initial install ${version_previous} (previous version)"
.dbFlow/build.sh --init --version "${version_previous}" "${flags[@]}"
apply_tasks+=( ".dbFlow/apply.sh --init --version ${version_previous}" )
fi
# following makes only sense when source not the same as target
if [[ $RLS_GATE_BRANCH != "${RLS_TARGET_BRANCH}" ]]; then
# merging target with source
log "merging changes from $RLS_GATE_BRANCH with : --strategy-option theirs"
retry git merge "${RLS_GATE_BRANCH}" --strategy-option theirs
# build diff patch
log "build patch upgrade ${version_next} (current version) ${flags[@]}"
export DBFLOW_RELEASE_IS_RUNNUNG="YES"
.dbFlow/build.sh --patch --version "${version_next}" "${flags[@]}"
unset export DBFLOW_RELEASE_IS_RUNNUNG
build_patch_worked=$?
apply_tasks+=( ".dbFlow/apply.sh --patch --version ${version_next}" )
# when buildtest then test new initial patch
if [[ ${RLS_BUILD} == 'Y' ]]; then
# und den initial Build des aktuellen Stand
log "building initial install $version_next (current version)"
.dbFlow/build.sh --init --version "${version_next}" "${flags[@]}"
apply_tasks+=( ".dbFlow/apply.sh --init --version ${version_next}" )
else
retry git push
if [[ ${RLS_REMOVE_TAG} == "true" ]]; then
log "Removing tag ${version_next}"
retry git tag -d "${version_next}"
retry git push origin --delete "${version_next}"
RLS_TAG_EXISTS="false"
fi
if [[ ${RLS_TAG_EXISTS} == "false" ]]; then
log "creating tag ${version_next}"
# create tag on target branch
retry git tag "${version_next}"
retry git push origin "${version_next}"
fi
fi
fi
local check_branch=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
if [[ $check_branch != "${starting_branch}" ]]; then
retry git checkout "${starting_branch}"
fi
# some summarizings
log "${GREEN}Done${NC}"
log "${GREEN}go to your instance directory where you host $RLS_TARGET_BRANCH and apply the following commands/patches${NC}"
if [[ ${RLS_TOFOLDER} != '-' ]]; then
cd "${RLS_TOFOLDER}" || exit
if [[ -d ".dbFlow" ]]; then
cd ".dbFlow" || exit
{ #try
sub_pulled=$(git pull)
} || { # catch
sub_pulled="no"
}
cd ..
fi
fi
for task in "${apply_tasks[@]}"
do
log "${CYAN}call ${BWHITE}${task}${NC} ${CYAN}from your instance folder${NC}"
if [[ ${RLS_TOFOLDER} != '-' ]]; then
${task}
fi
done
if [[ ${RLS_TOFOLDER} != '-' ]]; then
cd "${CUR_DIRECTORY}" || exit
fi
}
trap '(exit 130)' INT
trap '(exit 143)' TERM
trap 'rc=$?; notify $rc; exit $rc' EXIT
function check_params() {
debug="n" help="n" version="-" source_branch="-" target_branch="-" build="n" apply_folder="-" gate_branch="-" forceddl="-" keep="-"
d=$debug h=$help v=$version s=$source_branch t=$target_branch b=$build a=$apply_folder g=$gate_branch
while getopts_long 'dhv:s:t:g:ba:kf debug help version: source: target: gate: build apply: keep forceddl' OPTKEY "${@}"; do
case ${OPTKEY} in
'd'|'debug')
d=y
;;
'h'|'help')
h=y
;;
'v'|'version')
version="${OPTARG}"
;;
's'|'source')
source_branch="${OPTARG}"
;;
't'|'target')
target_branch="${OPTARG}"
;;
'g'|'gate')
gate_branch="${OPTARG}"
;;
'b'|'build')
build=y
;;
'a'|'apply')
apply_folder="${OPTARG}"
;;
'k'|'keep')
keep="-k"
;;
'f'|'forceddl')
forceddl="--forceddl"
;;
'?')
echo_error "INVALID OPTION -- ${OPTARG}" >&2
usage
;;
':')
echo_error "MISSING ARGUMENT for option -- ${OPTARG}" >&2
usage
;;
*)
echo_error "UNIMPLEMENTED OPTION -- ${OPTKEY}" >&2
usage
;;
esac
done
# help first
if [[ -n $h ]] && [[ $h == "y" ]]; then
usage
fi
# Rule 1: source_branch is current when not given...
if [[ -z $source_branch ]] || [[ $source_branch == "-" ]]; then
RLS_SOURCE_BRANCH=$current_branch
else
RLS_SOURCE_BRANCH=$source_branch
fi
# Rule 2: we always need a target
if [[ -z $target_branch ]] || [[ $target_branch == "-" ]]; then
echo_error "Missing target branch, use --target <target branchname>"
usage
else
RLS_TARGET_BRANCH=$target_branch
fi
# Rule 2.1: target branch must exist
if ! git rev-parse --verify "$RLS_TARGET_BRANCH" >/dev/null 2>&1; then
echo_error "Target branch: ${RLS_TARGET_BRANCH} doesn't exist"
exit 1
fi
# Rule 2.2: target branch must have an upstream
if ! git rev-parse --abbrev-ref "$RLS_TARGET_BRANCH"@{upstream} >/dev/null 2>&1; then
echo_error "Branch $RLS_TARGET_BRANCH has no upstream"
echo_error "You might call: git push --set-upstream origin ${RLS_TARGET_BRANCH}"
exit 1
fi
# Rule 2.3: target and source branch must be different
if [[ "${RLS_TARGET_BRANCH}" == "${RLS_SOURCE_BRANCH}" ]]; then
echo_error "Source and Target Branch must be different!";
exit 1
fi;
# Rule 3: When no build test, we need a version
if [[ $build == "n" ]] && [[ $version == "-" ]]; then
echo_error "Missing version, use --verion 1.2.3 or --build $build"
usage
fi
RLS_INC_TYPE="-"
local lowercase_version=$(toLowerCase "${version}")
if [[ "$lowercase_version" == "current" ]] || [[ "$lowercase_version" == "patch" ]] || [[ "$lowercase_version" == "minor" ]] || [[ "$lowercase_version" == "major" ]]; then
version=$(get_next_version "$lowercase_version")
if [[ $version =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
RLS_INC_TYPE=${lowercase_version}
else
echo_error "Could not determine next version, based on git semantic tag informations. Is there any version tag? There has to be at least one!"
exit 1
fi
fi
RLS_TAG_EXISTS="false"
RLS_REMOVE_TAG="false"
if [[ $(git tag -l "$version") ]]; then
# if this tag points to the same tag as the source commit, then everything should be ok
TAG_COMMIT=$(git rev-parse "$version" 2>/dev/null)
TARGET_COMMIT=$(git rev-parse "$RLS_TARGET_BRANCH" 2>/dev/null)
if [[ "${TAG_COMMIT}" != "${TARGET_COMMIT}" ]]; then
# Lokale Branches ermitteln, die diesen Commit enthalten
TAG_BRANCHES=$(git branch --contains "$TAG_COMMIT")
read -r -p "$(echo -e "${BORANGE}Target Version: ${version} exists already on branch ${TAG_BRANCHES}.${NC}\nPress y to recreate tag on target branch ${RLS_TARGET_BRANCH}, otherwise abort release! (y/n)" ) " -n 1
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
if [[ "$0" = "$BASH_SOURCE" ]]; then
echo_error "Aborted... \nThe version tag: $version is already used. And commit of $version and $RLS_TARGET_BRANCH are not equal. So, please use another one!"
exit 1
else
echo_error "Aborted... \nThe version tag: $version is already used. And commit of $version and $RLS_TARGET_BRANCH are not equal. So, please use another one!"
return 1 # handle exits from shell or function but don't exit interactive shell
fi
fi
RLS_TAG_EXISTS="true"
RLS_REMOVE_TAG="true"
else
# Target Tag exists, so we do not need to create it
RLS_TAG_EXISTS="true"
fi
fi
RLS_VERSION=$version
RLS_BUILD=${build^^}
if [[ ${apply_folder} != '-' ]] && [[ ! -d ${apply_folder} ]]; then
echo_error "Folder to apply to does not exist!"
usage
else
RLS_TOFOLDER=${apply_folder}
fi
if [[ -z $gate_branch ]] || [[ $gate_branch == "-" ]]; then
RLS_GATE_BRANCH=${RLS_SOURCE_BRANCH}
else
RLS_GATE_BRANCH=${gate_branch}
fi
if [[ ${forceddl} != '-' ]]; then
RLS_FORCE_DDL=${forceddl}
fi
}
# valildate arguments
check_params "$@"
# something to remind you, ask to proceed
check_remind_me
# do the work
build_release