-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-deploy.sh
executable file
·269 lines (221 loc) · 8.27 KB
/
app-deploy.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
#!/usr/bin/env bash
source ./.deploy-options.sh
###############################################################
# DEPLOY SCRIPT #
# #
# Script used for creating the specific #
# tag used for triggering the CI deployment #
# #
# End tag should look like this: #
# internal-all/v1.0.0-1234 #
# #
# Prepared by Jasmin Abou Aldan #
# Copyright (c) 2020 Infinum. All rights reserved. #
###############################################################
# Use global variables at your own risk as this can be overridden in the future.
set -e
bold=$(tput bold)
normal=$(tput sgr0)
#################################
# MAIN #
#################################
# Private part of the script...
#
# In general, you don't have to edit
# this part of the script but feel free
# to edit any part of it as suits your needs.
function main {
# BASE INFO
# commit, tag, synced head,...
initial_checkup
# CREATE TAG
deploy_options
create_app_version_and_build_number
# CREATE CHANGELOG
generate_tag_and_changelog
# DEPLOY
push_tag_and_start_deploy
}
#################################
# HELPERS #
#################################
function initial_checkup {
echo
echo "###############################################################"
echo "# COMMIT CHECK #"
echo "###############################################################"
echo
if [ $# -gt 1 ] || [ "$1" == '-h' ] || [ "$1" == '--help' ] || [ "$1" == 'help' ]; then
echo "Usage: $0 [optional-commit-hash]"
exit 1
fi
if [ $# -eq 1 ]; then
commit="$1"
else
commit=`git rev-parse --short HEAD`
fi
commit_message=`git log --format=%B -n 1 ${commit}`
if [ $? -ne 0 ]; then
echo "Failed to get message for commit '$commit'. Aborting."
exit 2
fi
echo "---------------------------------------------------------------"
echo "Targeting commit: ${bold}$commit${normal}"
echo "---------------------------------------------------------------"
echo "$commit_message"
echo "---------------------------------------------------------------"
remote_branches=`git branch -r --contains ${commit}`
if [ $? -ne 0 ] || [ -z "$remote_branches" ]; then
echo
echo "Commit '$commit' not found on any remote branch."
if $enable_automatic_commit_push ; then
current_branch=`git rev-parse --abbrev-ref HEAD`
echo "Pushing..."
git push origin "$current_branch"
else
read -r -p "Do you want to push it? [y/n] " push_to_git
if [[ ${push_to_git} =~ ^(yes|y|Y) ]] || [ -z ${push_to_git} ]; then
current_branch=`git rev-parse --abbrev-ref HEAD`
echo "Pushing..."
git push origin "$current_branch"
else
echo "Aborting."
exit 3
fi
fi
fi
}
function create_app_version_and_build_number {
echo
echo "###############################################################"
echo "# APP VERSION #"
echo "###############################################################"
echo
# App version number
last_known_tag=`git describe --abbrev=0 --tags | sed -E 's/.*v([0-9]*\.?[0-9]*\.?[0-9]*)(-.*)?/\1/'`
if [ -z "$last_known_tag" ]
then
read -r -p "Enter current app version (e.g. 1.0.0): " appversion
else
read -r -p "Press enter to use last known version: ${bold}$last_known_tag${normal}. (or enter different version) " new_version
[ -z "$new_version" ] && appversion=$last_known_tag || appversion=$new_version
fi
if ! [[ "$appversion" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]
then
echo "App version is in wrong format (use M.m.p format, e.g. 1.0.0). Aborting..."
exit 5
fi
# Build number
echo "Getting next build number..."
tags_count=`git ls-remote --t --refs -q | grep -o -E '[[:<:]]\d+(\n|$)' | sort -nr | head -n1`
if [ -z "$tags_count" ]; then
tags_count=0
else
tags_count=$((tags_count+1))
fi
# Create tag name internal{-TargetX}/vM.m.p-{number of commits}. E.g. internal-all/v1.0.0-1234
tag="$target/v$appversion-$tags_count"
echo
echo "Next app version is: ${bold}v$appversion-$tags_count${normal}"
sleep 1
}
function generate_tag_and_changelog {
echo
echo "###############################################################"
echo "# CHANGELOG #"
echo "###############################################################"
echo
echo "------------------------------------------------------------"
echo "Enter changelog message..."
echo "------------------------------------------------------------"
sleep 1
git tag -a "$tag"
}
function push_tag_and_start_deploy {
changelog_message=`git show -s --format=%N ${tag} | tail -n +4`
if ! $enable_final_confirmation ; then
push_tag
fi
echo
echo "###############################################################"
echo "# DEPLOY #"
echo "###############################################################"
echo
echo "---------------------------------------------------------------"
echo " ~ CONFIGURATION ~ "
echo
echo "Target: ${bold}$target_selection. $target${normal}"
echo "Version: ${bold}v$appversion-$tags_count${normal}"
echo "Tag: ${bold}$tag${normal}"
echo
echo "Changelog:"
echo "${bold}$changelog_message${normal}"
echo "---------------------------------------------------------------"
echo
read -r -p "Is configuration correct for the CI deployment? [y/n] " response
echo
if [[ ${response} =~ ^(no|n|N) ]] || [ -z ${response} ]; then
git tag -d "$tag"
echo "Aborting."
exit 6
fi
push_tag
}
function push_tag {
# Push if everything is ok!
if [ $? -eq 0 ]; then
echo
echo "------------------------------------------------------------"
echo "Tag added. Pushing tags ..."
echo
git push origin "$tag"
echo
echo "============================================================"
echo "DEPLOY TAG SUCCESSFULLY ADDED!"
echo "CHECK YOUR CI FOR THE BUILD STATUS"
echo "============================================================"
echo
exit 0
else
echo
echo "------------------------------------------------------------"
echo "Failed to add tag. Aborting."
echo "------------------------------------------------------------"
exit 7
fi
}
#################################
# UPDATE #
#################################
function script_auto_update {
echo
echo "Please wait until main script is finished with updating..."
echo
echo "Fetching new data..."
mkdir .app_deploy_tmp
git clone --quiet https://github.com/infinum/app-deploy-script.git .app_deploy_tmp
echo "Updating..."
cat .app_deploy_tmp/app-deploy.sh > /usr/local/bin/app-deploy
echo "Cleaning temporary files..."
rm -rf .app_deploy_tmp
echo "Updating finished!"
exit 0
}
#################################
# START EVERYTHING #
#################################
if $use_automatic_console_clean ; then
clear
fi
echo
echo "###############################################################"
echo "# DEPLOY SCRIPT #"
echo "# #"
echo "# Copyright (c) 2020 Infinum. #"
echo "###############################################################"
echo
if ! [ "$1" == '--update' ] ; then
main
else
script_auto_update
fi