1
+ #! /usr/bin/env bash
2
+
3
+ # ##############################################################
4
+ # DEPLOY SCRIPT #
5
+ # #
6
+ # Script used for creating the specific #
7
+ # tag used for triggering the CI deployment #
8
+ # #
9
+ # End tag should look like this: #
10
+ # internal-all/v1.0.0-1234 #
11
+ # #
12
+ # Prepared by Jasmin Abou Aldan #
13
+ # Copyright (c) 2020 Infinum. All rights reserved. #
14
+ # ##############################################################
15
+
16
+ set -e
17
+ bold=$( tput bold)
18
+ normal=$( tput sgr0)
19
+
20
+ # ########################################################
21
+ # DEPLOY OPTIONS #
22
+ # #
23
+ # Part of script that should be edited by the user. #
24
+ # #
25
+ # You can add/remove/edit targets that you want to use #
26
+ # for the deployment. #
27
+ # #
28
+ # This will generate first part of tag. #
29
+ # ########################################################
30
+
31
+ # # Used for the option select by the user
32
+ # # And for generating first part of the tag.
33
+ # # If option is added to the "Select target"
34
+ # # same option should be added under the
35
+ # # if/else logic for creating the first part.
36
+
37
+ function deploy_options {
38
+
39
+ # Options shown to the user
40
+
41
+ echo
42
+ echo " ###############################################################"
43
+ echo " # DEPLOY TARGET SELECTION #"
44
+ echo " ###############################################################"
45
+ echo
46
+ echo " --------------"
47
+ echo " | TryOutApps |"
48
+ echo " --------------"
49
+ echo
50
+ echo " [0] All"
51
+ echo " [1] Staging"
52
+ echo " [2] UAT"
53
+ echo " [3] Production"
54
+ echo
55
+ echo " =================="
56
+ echo
57
+ echo " ---------------------"
58
+ echo " | APP STORE CONNECT |"
59
+ echo " ---------------------"
60
+ echo
61
+ echo " [4] App Store"
62
+ echo
63
+ read -r -p " Enter number in square brackets: " target_selection
64
+ # erase_lines
65
+
66
+ # Logic for creating first part of the tag.
67
+ # Should be in sync with options shown to the user.
68
+
69
+ if [ ${target_selection} -eq 0 ]; then
70
+ target=" internal-all"
71
+ elif [ ${target_selection} -eq 1 ]; then
72
+ target=" internal-staging"
73
+ elif [ ${target_selection} -eq 2 ]; then
74
+ target=" internal-uat"
75
+ elif [ ${target_selection} -eq 3 ]; then
76
+ target=" internal-production"
77
+ elif [ ${target_selection} -eq 4 ]; then
78
+ target=" appstore"
79
+ else
80
+ echo " Wrong target index. Aborting..."
81
+ exit 4
82
+ fi
83
+ }
84
+
85
+ # ################################
86
+ # MAIN #
87
+ # ################################
88
+
89
+ # Private part of the script...
90
+ #
91
+ # In general, you don't have to edit
92
+ # this part of the script but feel free
93
+ # to edit any part of it as suits your needs.
94
+
95
+ function main {
96
+
97
+ clear
98
+ echo
99
+ echo " ###############################################################"
100
+ echo " # DEPLOY SCRIPT #"
101
+ echo " # #"
102
+ echo " # Copyright (c) 2020 Infinum. #"
103
+ echo " ###############################################################"
104
+ echo
105
+
106
+ # BASE INFO
107
+ # commit, tag, synced head,...
108
+ initial_checkup
109
+
110
+ # CREATE TAG
111
+
112
+ deploy_options
113
+ create_app_version_and_build_number
114
+
115
+ # CREATE CHANGELOG
116
+
117
+ generate_tag_and_changelog
118
+
119
+ # DEPLOY
120
+
121
+ push_tag_and_start_deploy
122
+ }
123
+
124
+
125
+ # ################################
126
+ # HELPERS #
127
+ # ################################
128
+
129
+ function initial_checkup {
130
+
131
+ echo
132
+ echo " ###############################################################"
133
+ echo " # COMMIT CHECK #"
134
+ echo " ###############################################################"
135
+ echo
136
+
137
+ if [ $# -gt 1 ] || [ " $1 " == ' -h' ] || [ " $1 " == ' --help' ] || [ " $1 " == ' help' ]; then
138
+ echo " Usage: $0 [optional-commit-hash]"
139
+ exit 1
140
+ fi
141
+
142
+ if [ $# -eq 1 ]; then
143
+ commit=" $1 "
144
+ else
145
+ commit=` git rev-parse --short HEAD`
146
+ fi
147
+
148
+ commit_message=` git log --format=%B -n 1 ${commit} `
149
+
150
+ if [ $? -ne 0 ]; then
151
+ echo " Failed to get message for commit '$commit '. Aborting."
152
+ exit 2
153
+ fi
154
+
155
+ echo " ---------------------------------------------------------------"
156
+ echo " Targeting commit: ${bold} $commit ${normal} "
157
+ echo " ---------------------------------------------------------------"
158
+ echo " $commit_message "
159
+ echo " ---------------------------------------------------------------"
160
+
161
+ remote_branches=` git branch -r --contains ${commit} `
162
+ if [ $? -ne 0 ] || [ -z " $remote_branches " ]; then
163
+ echo
164
+ echo " Commit '$commit ' not found on any remote branch."
165
+ read -r -p " Do you want to push it? [y/n] " push_to_git
166
+ if [[ ${push_to_git} =~ ^(yes| y| Y) ]] || [ -z ${push_to_git} ]; then
167
+ current_branch=` git rev-parse --abbrev-ref HEAD`
168
+ echo " Pushing..."
169
+ git push origin " $current_branch "
170
+ else
171
+ echo " Aborting."
172
+ exit 3
173
+ fi
174
+ fi
175
+ }
176
+
177
+ function create_app_version_and_build_number {
178
+
179
+ echo
180
+ echo " ###############################################################"
181
+ echo " # APP VERSION #"
182
+ echo " ###############################################################"
183
+ echo
184
+
185
+ # App version number
186
+ last_known_tag=` git describe --abbrev=0 --tags | sed -E ' s/.*v([0-9]\.?[0-9]?\.?[0-9]?)(-.*)?/\1/' `
187
+
188
+ if [ -z " $last_known_tag " ]
189
+ then
190
+ read -r -p " Enter current app version (e.g. 1.0.0): " appversion
191
+ else
192
+ read -r -p " Press enter to use last known version: ${bold} $last_known_tag ${normal} . (or enter different version) " new_version
193
+ [ -z " $new_version " ] && appversion=$last_known_tag || appversion=$new_version
194
+ fi
195
+
196
+ if [[ " $appversion " != [0-9].[0-9].[0-9] ]]
197
+ then
198
+ echo " App version is in wrong format (use M.m.p format, e.g. 1.0.0). Aborting..."
199
+ exit 5
200
+ fi
201
+
202
+ # Build number
203
+
204
+ echo " Getting next build number..."
205
+
206
+ tags_count=` git ls-remote --t --refs -q | grep -o -E ' [[:<:]]\d+(\n|$)' | sort -nr | head -n1`
207
+
208
+ if [ -z " $tags_count " ]; then
209
+ tags_count=0
210
+ else
211
+ tags_count=$(( tags_count+ 1 ))
212
+ fi
213
+
214
+ # Create tag name internal{-TargetX}/vM.m.p-{number of commits}. E.g. internal-all/v1.0.0-1234
215
+ tag=" $target /v$appversion -$tags_count "
216
+
217
+ echo
218
+ echo " Next app version is: ${bold} v$appversion -$tags_count ${normal} "
219
+ sleep 1
220
+ }
221
+
222
+ function generate_tag_and_changelog {
223
+
224
+ echo
225
+ echo " ###############################################################"
226
+ echo " # CHANGELOG #"
227
+ echo " ###############################################################"
228
+ echo
229
+ echo " ------------------------------------------------------------"
230
+ echo " Enter changelog message..."
231
+ echo " ------------------------------------------------------------"
232
+ sleep 1
233
+
234
+ git tag -a " $tag "
235
+ }
236
+
237
+ function push_tag_and_start_deploy {
238
+
239
+ changelog_message=` git show -s --format=%N ${tag} | tail -n +4`
240
+
241
+ echo
242
+ echo " ###############################################################"
243
+ echo " # DEPLOY #"
244
+ echo " ###############################################################"
245
+ echo
246
+ echo " ---------------------------------------------------------------"
247
+ echo " ~ CONFIGURATION ~ "
248
+ echo
249
+ echo " Target: ${bold} $target_selection . $target ${normal} "
250
+ echo " Version: ${bold} v$appversion -$tags_count ${normal} "
251
+ echo " Tag: ${bold} $tag ${normal} "
252
+ echo
253
+ echo " Changelog:"
254
+ echo " ${bold} $changelog_message ${normal} "
255
+ echo " ---------------------------------------------------------------"
256
+ echo
257
+ read -r -p " Is configuration correct for the CI deployment? [y/n] " response
258
+ echo
259
+
260
+ if [[ ${response} =~ ^(no| n| N) ]] || [ -z ${response} ]; then
261
+ git tag -d " $tag "
262
+ echo " Aborting."
263
+ exit 6
264
+ fi
265
+
266
+ # Push if everything is ok!
267
+ if [ $? -eq 0 ]; then
268
+ echo
269
+ echo " ------------------------------------------------------------"
270
+ echo " Tag added. Pushing tags ..."
271
+ echo
272
+ git push origin " $tag "
273
+ echo
274
+ echo " ============================================================"
275
+ echo " DEPLOY TAG SUCCESSFULLY ADDED!"
276
+ echo " CHECK YOUR CI FOR THE BUILD STATUS"
277
+ echo " ============================================================"
278
+ echo
279
+ else
280
+ echo
281
+ echo " ------------------------------------------------------------"
282
+ echo " Failed to add tag. Aborting."
283
+ echo " ------------------------------------------------------------"
284
+ exit 7
285
+ fi
286
+ }
287
+
288
+ # ################################
289
+ # START EVERYTHING #
290
+ # ################################
291
+
292
+ main
0 commit comments