-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.sh
226 lines (211 loc) · 6.99 KB
/
index.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
get_funny_idiom() {
RANDOM_FUNNY_IDIOMS_GIST_URL=https://gist.githubusercontent.com/tuanngocptn/9fabb1979e5b29eca84bce28c6b8d080/raw/random-idioms-vi.sh
RESULT="$(bash -c "$(curl -fsSL ${RANDOM_FUNNY_IDIOMS_GIST_URL})")"
echo $RESULT
}
get_message_information() {
TAG_SOURCE=$(git describe --tags)
AUTHOR=$(git show -s --format='%an')
VERSION=($(grep -oE '[a-z]{3,4}|[0-9]{1,3}' <<<"$TAG_NAME"))
VERSION_APPNAME="${VERSION[0]}-${VERSION[1]}"
VERSION_ENV=$(split_version $TAG_SOURCE environment)
VERSION_MAJOR=$(split_version $TAG_SOURCE major)
VERSION_MINOR=$(split_version $TAG_SOURCE minor)
VERSION_PATCHE=$(split_version $TAG_SOURCE patche)
VERSION_BUILD_NUMBER=$(split_version $TAG_SOURCE build)
VERSION_FULL=$(split_version $TAG_SOURCE full)
TAG_SOURCE="${VERSION_APPNAME}-${VERSION_ENV}-$VERSION_FULL+$VERSION_BUILD_NUMBER"
MESSAGE_INFO="\n- App: Carz $1.\n- Môi trường: $VERSION_ENV.\n- Phiên bản: $VERSION_FULL.\n- Bản build số: $VERSION_BUILD_NUMBER.\n- Thằng bấm nút: ${AUTHOR}."
echo $MESSAGE_INFO
}
send_telegram() {
curl --location "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
--header 'Content-Type: application/json' \
--data '{
"chat_id": -1001837632261,
"reply_to_message_id": 196,
"text": "'"$1"'"
}'
}
send_slack() {
curl --location "https://hooks.slack.com/services/$SLACK_BOT_SERVICE_ID" \
--header 'Content-Type: application/json' \
--data '{"text":"'"$1"'"}'
}
get_stage_prompt() {
title="Chon moi truong build:"
prompt="Lua chon:"
options=("Development" "Staging" "Production")
echo "$title"
PS3="$prompt "
STAGE=""
select opt in "${options[@]}" "Thoat"; do
case "$REPLY" in
1) echo "Lua chon build $opt" && STAGE="development" && break ;;
2) echo "Lua chon build $opt" && STAGE="staging" && break ;;
3) echo "Lua chon build $opt" && STAGE="production" && break ;;
$((${#options[@]} + 1)))
echo "Goodbye!"
exit 0
;;
*)
echo "Sai lua chon. chon lai."
continue
;;
esac
done
}
# create_tag() {
# TAG_NAME=$1
# echo "New release for $TAG_NAME at $(date '+%d/%m/%Y %H:%M:%S')" >>release.txt
# CURRENT_BRANCH=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
# HEAD_HASH="$(git rev-parse --short HEAD)"
# {
# git add release.txt &&
# git commit -m "LC-0/build: Release $TAG_NAME" &&
# git push origin $CURRENT_BRANCH &&
# git tag -a $TAG_NAME -m "New release for $TAG_NAME" &&
# git push origin $TAG_NAME
# } || {
# git reset --soft $HEAD_HASH
# git checkout $HEAD_HASH release.txt
# }
# }
create_tag() {
TAG_NAME=$1
{
git tag -a $TAG_NAME -m "New release for $TAG_NAME" &&
git push origin $TAG_NAME
} || {
git tag -d $TAG_NAME
}
}
init_version_tag() {
PREFIX=''
if [ "${1}" ]; then
PREFIX="$1/"
fi
create_tag "${PREFIX}development/v1.0.1+0"
create_tag "${PREFIX}staging/v1.0.1+0"
create_tag "${PREFIX}production/v1.0.0+0"
}
delete_tag() {
git tag -d "$1"
git push --delete origin "$1"
}
check_tag_format() {
git fetch
TAG_SOURCE=$(git describe --tags)
REGEX_MATCH_TAGS_BUILD="(development|staging|production)\/((v|\.|\+)[0-9]*){4}"
if [ "${PREFIX}" ]; then
REGEX_MATCH_TAGS_BUILD="^.*${PREFIX}\/${REGEX_MATCH_TAGS_BUILD}$"
else
REGEX_MATCH_TAGS_BUILD="^.*${REGEX_MATCH_TAGS_BUILD}$"
fi
if [[ "$TAG_SOURCE" =~ $REGEX_MATCH_TAGS_BUILD ]]; then
echo "INFO: Tag name '$TAG_SOURCE' is the right format tag name for building the new version."
else
exit "ERROR: Tag name '$TAG_SOURCE' is NOT the right format tag name for building the new version."
fi
}
remove_all_tag() {
git fetch
git push origin --delete $(git tag -l)
git tag -d $(git tag -l)
}
is_cleanup() {
git tag -l -n "*merc-carz-dev-*" -n "merc-*carz-pro-*" -n "merc-*carz-stg-*" --sort=creatordate --format "%(refname:short)" | tail -2
TAG_NAME_TAIL_2=$(git tag -l -n "*merc-carz-dev-*" -n "merc-*carz-pro-*" -n "merc-*carz-stg-*" --sort=creatordate --format "%(refname:short)" | tail -2)
TAG_NAME_TAIL_2=(${TAG_NAME_TAIL_2//\n/ })
FILE_CHANGE=$(git diff ${TAG_NAME_TAIL_2[0]} ${TAG_NAME_TAIL_2[1]} --stat --name-only)
IS_CLEAN=false
if [[ "$FILE_CHANGE" == *"package.json"* || "$FILE_CHANGE" == *"Podfile"* ]]; then
IS_CLEAN=true
fi
}
split_version() {
TAG_NAME=$1
VERSION_TYPE=$2 # full, all, major, minor, patche, increment_patche, build, increment_build
VERSION_NAME_PATH=($(grep -oE '[a-z,0-9,-.+]*' <<<"$TAG_NAME"))
ARRAY_SIZE=${#VERSION_NAME_PATH[@]}
VERSION=${VERSION_NAME_PATH[$((ARRAY_SIZE - 1))]}
ENVIRONMENT=${VERSION_NAME_PATH[$((ARRAY_SIZE - 2))]}
VERSION_ARRAY=($(grep -oE '[0-9]*' <<<"$VERSION"))
case $VERSION_TYPE in
all)
echo ${VERSION}
;;
full)
echo "${VERSION_ARRAY[0]}.${VERSION_ARRAY[1]}.${VERSION_ARRAY[2]}"
;;
increment_patche)
PATCHE_WILL_BUILD=$((VERSION_ARRAY[2] + 1))
echo "${VERSION_ARRAY[0]}.${VERSION_ARRAY[1]}.${PATCHE_WILL_BUILD}"
;;
major)
echo ${VERSION_ARRAY[0]}
;;
minor)
echo ${VERSION_ARRAY[1]}
;;
patche)
echo ${VERSION_ARRAY[2]}
;;
build)
echo ${VERSION_ARRAY[3]}
;;
increment_build)
echo ${VERSION_ARRAY[3]} + 1 | bc
;;
environment)
echo ${ENVIRONMENT}
;;
esac
}
increment_build_number() {
STAGE=$2
if [[ ! "$STAGE" ]]; then
exit "Missing STAGE environment"
fi
PREFIX=''
if [[ "$1" ]]; then
PREFIX="$1/"
fi
PRO_TAG=$(git tag --sort=-version:refname -l "${PREFIX}production/*" | head -n 1)
NEW_TAG=''
if [[ "$STAGE" == "production" ]]; then
PRO_TAG_FULL=$(split_version $PRO_TAG full)
PRO_BUILD_NUMBER=$(split_version $PRO_TAG build)
PRO_BUILD_NUMBER_INCREMENT=$((PRO_BUILD_NUMBER + 1))
NEW_TAG="${PREFIX}production/v${PRO_TAG_FULL}+${PRO_BUILD_NUMBER_INCREMENT}"
else
STAGE_TAG=$(git tag --sort=-version:refname -l "${PREFIX}${STAGE}/*" | head -n 1)
STAGE_TAG_FULL=$(split_version $PRO_TAG increment_patche)
STAGE_TAG_LATEST=$(git tag --sort=-version:refname -l "${PREFIX}${STAGE}/v${STAGE_TAG_FULL}+*" | head -n 1)
STAGE_BUILD_NUMBER=1
if [[ $STAGE_TAG_LATEST ]]; then
STAGE_BUILD_NUMBER=$(split_version $STAGE_TAG_LATEST increment_build)
fi
NEW_TAG="${PREFIX}${STAGE}/v${STAGE_TAG_FULL}+${STAGE_BUILD_NUMBER}"
fi
create_tag $NEW_TAG
}
get_id_lc_task() {
TAG_NAME=$(git describe --tags)
delimiter="/"
RESULT=$(echo $TAG_NAME | cut -d "$delimiter" -f 2)
TAG_ENV_NAME_TAIL_2=$(git tag -l -n "$1/${RESULT}/*" --sort=creatordate --format "%(refname:short)" | tail -2)
eval "TAG_ENV_NAME_TAIL_2=($TAG_ENV_NAME_TAIL_2)"
LOGS=$(git log --oneline ${TAG_ENV_NAME_TAIL_2[0]}...${TAG_ENV_NAME_TAIL_2[1]})
LOGS=$(echo $LOGS | grep --only-matching --extended-regexp 'LC-[0-9]*')
LOGS=$(echo "$LOGS" | awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s, ",$i)}')
LOGS="${LOGS//LC-0, /}"
# Trim string
LOGS="${LOGS%"${LOGS##*[![:space:]]}"}"
# Remove commas if it is the last character
if [[ "${LOGS: -1}" == "," ]]; then
# Remove the comma
LOGS="${LOGS%,}"
fi
echo $LOGS
}