-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.sh
214 lines (193 loc) · 6.16 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
#!/bin/bash
# ------------------------------------------------------------------------------------------------------------------------------------------
# Filename: index.sh
# Author: Nick (Ngoc Pham)
# Created: 2023-10-23
# Last Updated: 2024-08-30
# Description: Semantic versioning without any track files.
# Version: 1.0.0
# Github: https://github.com/tuanngocptn
# Usage: Read at https://github.com/PacificPromise/semantic-versioning-action
# Notes: This action uses git tag for controlling the version. This action will not change/add/remove any file in your repository.
# ------------------------------------------------------------------------------------------------------------------------------------------
get_stage_prompt() {
title="Action:"
prompt="Choose:"
options=("Increment development environment" "Increment staging environment" "Increment UAT environment" "Increment product environment" "Increment patch version (1.0.xx)" "Increment minor version (1.xx.0)" "Increment major version (xx.0.0)")
echo "$title"
PS3="$prompt "
STAGE=""
select opt in "${options[@]}" "Quit"; do
case "$REPLY" in
1) echo "Chose option: $opt" && increment_tag dev && break ;;
2) echo "Chose option: $opt" && increment_tag stg && break ;;
3) echo "Chose option: $opt" && increment_tag uat && break ;;
4) echo "Chose option: $opt" && increment_tag prd && break ;;
5) echo "Chose option: $opt" && increment_core_tag patch && break ;;
6) echo "Chose option: $opt" && increment_core_tag minor && break ;;
7) echo "Chose option: $opt" && increment_core_tag major && break ;;
$((${#options[@]} + 1)))
echo "Goodbye!"
exit 0
;;
*)
echo "Choose again !"
continue
;;
esac
done
}
create_tag() {
TAG_NAME=$1
{
git tag -a $TAG_NAME -m "Release version $TAG_NAME" &&
git push origin $TAG_NAME
} || {
git tag -d $TAG_NAME
}
}
delete_tag() {
git tag -d "$1"
git push --delete origin "$1"
}
remove_all_tag() {
git fetch --all --tags --force
git push origin --delete $(git tag -l)
git tag -d $(git tag -l)
}
health_check() {
echo "Semantic Version Action (SVA) is Ok"
}
config_github_token() {
if [[ $(git config user.email) == '41898282+github-actions[bot]@users.noreply.github.com'* ]]; then
echo "Already config github token"
else
REMOTE_REPO="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git remote add publisher $REMOTE_REPO || true
git config user.name "GitHub Actions"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
fi
}
split_version() {
TAG_NAME=$1
VERSION_TYPE=$2 # full, all, major, minor, patch, increment_patch, 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))]}
VERSION_ARRAY=($(grep -oE '[0-9]*' <<<"$VERSION"))
case $VERSION_TYPE in
all)
echo ${VERSION}
;;
major)
echo ${VERSION_ARRAY[0]}
;;
minor)
echo ${VERSION_ARRAY[1]}
;;
patch)
echo ${VERSION_ARRAY[2]}
;;
build)
echo ${VERSION_ARRAY[3]}
;;
next_build)
echo $((VERSION_ARRAY[3] + 1))
;;
esac
}
increment_version() {
TAG_NAME=$1
VERSION_TYPE=$2 # full, all, major, minor, patch
VERSION_NAME_PATH=($(grep -oE '[a-z,0-9,-.+]*' <<<"$TAG_NAME"))
ARRAY_SIZE=${#VERSION_NAME_PATH[@]}
VERSION=${VERSION_NAME_PATH[$((ARRAY_SIZE - 1))]}
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]}"
;;
major)
MAJOR_WILL_BUILD=$((VERSION_ARRAY[0] + 1))
echo "${MAJOR_WILL_BUILD}.0.0"
;;
minor)
MINOR_WILL_BUILD=$((VERSION_ARRAY[1] + 1))
echo "${VERSION_ARRAY[0]}.${MINOR_WILL_BUILD}.0"
;;
patch)
PATCH_WILL_BUILD=$((VERSION_ARRAY[2] + 1))
echo "${VERSION_ARRAY[0]}.${VERSION_ARRAY[1]}.${PATCH_WILL_BUILD}"
;;
esac
}
get_previous_tag() {
PREVIOUS_TAG=''
if [[ $OSTYPE == 'darwin'* ]]; then
PREVIOUS_TAG=$(git tag --sort=-version:refname -l | grep 'v\d\+\.\d\+\.\d\+$' | head -n 1 || echo "")
fi
if [[ $OSTYPE == 'linux'* ]]; then
PREVIOUS_TAG=$(git tag --sort=-version:refname -l | grep -P "v\d+\.\d+\.\d+$" | head -n 1 || echo "")
fi
if [[ $OSTYPE == 'msys'* ]]; then
PREVIOUS_TAG=$(git tag --sort=-version:refname -l | grep -P "v\d+\.\d+\.\d+$" | head -n 1 || echo "")
fi
echo $PREVIOUS_TAG
}
get_increment_core_tag() {
VERSION_TYPE=$1
git fetch --all --tags --force
PREVIOUS_TAG=$(get_previous_tag)
if ! [ "$PREVIOUS_TAG" ]; then
echo v0.0.1 # v0.0.1 is init tag
exit 0
fi
NEW_TAG="v$(increment_version $PREVIOUS_TAG $VERSION_TYPE)"
echo $NEW_TAG
}
increment_core_tag() {
VERSION_TYPE=$1
git fetch --all --tags --force
PREVIOUS_TAG=$(get_previous_tag)
if ! [ "$PREVIOUS_TAG" ]; then
create_tag v0.0.1 # v0.0.1 is init tag
exit 0
fi
NEW_TAG="v$(increment_version $PREVIOUS_TAG $VERSION_TYPE)"
create_tag $NEW_TAG
}
increment_tag() {
git fetch --all --tags --force
STAGE=$1
if ! [ "$STAGE" ]; then
PREVIOUS_TAG=$(get_previous_tag)
if ! [ "$PREVIOUS_TAG" ]; then
create_tag v0.0.1 # v0.0.1 is init tag
exit 0
fi
NEW_TAG="v$(increment_version $PREVIOUS_TAG patch)"
create_tag $NEW_TAG
exit 0
else
MAIN_TAG=$(get_previous_tag)
if ! [ "$MAIN_TAG" ]; then
MAIN_TAG=v0.0.1 # v0.0.1 is init tag
create_tag $MAIN_TAG
fi
MAIN_TAG_INCREMENT_PATCH=$(increment_version $MAIN_TAG patch)
STAGE_TAG_LATEST=$(git tag --sort=-version:refname -l "v${MAIN_TAG_INCREMENT_PATCH}-${STAGE}+*" | head -n 1)
if [[ $STAGE == 'prd'* ]]; then # Check previous production tag
STAGE_TAG_LATEST=$(git tag --sort=-version:refname -l "v[0-9]*.[0-9]*.[0-9]*-${STAGE}+[0-9]*" | head -n 1)
fi
STAGE_BUILD_NUMBER=1
if [ "$STAGE_TAG_LATEST" ]; then
STAGE_BUILD_NUMBER=$(split_version $STAGE_TAG_LATEST next_build)
fi
NEW_TAG="v${MAIN_TAG_INCREMENT_PATCH}-${STAGE}+${STAGE_BUILD_NUMBER}"
create_tag $NEW_TAG
exit 0
fi
}