-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy-build.sh
executable file
·360 lines (327 loc) · 11.1 KB
/
deploy-build.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
#!/bin/bash
###############################################################################
# Include common functions...
# -----------------------------------------------------------------------------
TOP=$(dirname $0)
source "${TOP}/common-functions.sh"
# -----------------------------------------------------------------------------
# USAGE prints help and exits the script with error code from provided parameter
# Parameters:
# $1 - error code to be used as return code from the script
# -----------------------------------------------------------------------------
function USAGE
{
echo ""
echo "Usage: $CMD repo-dir version [commands]"
echo ""
echo "repo-dir path to git repository with library sources"
echo ""
echo "version is version to be published to repositories"
echo " Only X.Y.Z format is accepted"
echo ""
echo "commands commands performed with the version:"
echo ""
echo " prepare prepare files for deployment"
echo " push push changes to git"
echo " deploy deploy changes to public repository"
echo " merge merges changes to master branch"
echo ""
echo " if no command is used, then script will"
echo " execute commands in following order:"
echo " 'prepare', 'push', 'deploy' and 'merge'"
echo ""
echo "options are:"
echo " -v0 turn off all prints to stdout"
echo " -v1 print only basic log about build progress"
echo " -v2 print full build log with rich debug info"
echo " -h | --help print this help information"
echo ""
echo " --merge-mode mode Mode of the merge step. Possible values are 'merge' or 'rebase'."
echo " If not specified, 'rebase' is used."
echo ""
echo " -dm target | --do-more-target target"
echo " specifies target for do-more.sh deployment"
echo " If specified, then only target will be executed"
echo ""
echo " --any-branch specifies that deployment is possible from"
echo " any branch. Be careful with this option."
echo ""
echo " --allow-warnings forces deployment even if some step reported"
echo " an ignorable warning."
echo ""
exit $1
}
###############################################################################
# Config
DEPLOY_INFO=".limedeploy"
MASTER_BRANCH="master"
DEV_BRANCH="develop"
# Runtime global vars
GIT_VALIDATE_DEVELOPMENT_BRANCH=1
GIT_SKIP_TAGS=0
GIT_ONLY_TAGS=0
STANDARD_BRANCH=0
COMMANDS=()
VERSION=''
REPO_DIR=''
DO_MORE_TARGET=''
ALLOW_WARNINGS=0
MERGE_MODE='rebase'
# -----------------------------------------------------------------------------
# Validate whether git branch is 'develop'
# -----------------------------------------------------------------------------
function VALIDATE_GIT_STATUS
{
LOG "----- Validating git status..."
PUSH_DIR "${REPO_DIR}"
####
local GIT_CURRENT_CHANGES=`git status -s`
if [ ! -z "$GIT_CURRENT_CHANGES" ]; then
FAILURE "Git status must be clean."
fi
local GIT_CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
if [ x$GIT_VALIDATE_DEVELOPMENT_BRANCH == x1 ]; then
if [ "$GIT_CURRENT_BRANCH" != ${DEV_BRANCH} ]; then
FAILURE "You have to be at '${DEV_BRANCH}' git branch."
fi
STANDARD_BRANCH=1
else
WARNING "Going to publish '${VERSION}' from non-standard branch '${GIT_CURRENT_BRANCH}'"
STANDARD_BRANCH=0
fi
git fetch origin
####
POP_DIR
}
# -----------------------------------------------------------------------------
# Loads '.limedeploy' file from {REPO_DIR}
# -----------------------------------------------------------------------------
function LOAD_DEPLOY_INFO_FILE
{
DEBUG_LOG "Loading ${DEPLOY_INFO} info file..."
PUSH_DIR "${REPO_DIR}"
####
if [ -f ${DEPLOY_INFO} ]; then
source ${DEPLOY_INFO}
# validate variables imported from .limedeploy
if [ -z "${DEPLOY_MODE}" ]; then
FAILURE "File `${DEPLOY_INFO}` found, but doesn't contain required properties."
fi
if [ -z "$DEPLOY_VERSIONING_FILES" ]; then
if [ -z "$NO_DEPLOY_VERSIONING_FILES" ]; then
FAILURE "DEPLOY_VERSIONING_FILES or NO_DEPLOY_VERSIONING_FILES variable must be set in '${DEPLOY_INFO}'."
fi
fi
else
FAILURE "Repository doesn't contain '${DEPLOY_INFO}' file."
fi
local MODE_IMPL="${TOP}/do-${DEPLOY_MODE}.sh"
if [ ! -f "${MODE_IMPL}" ]; then
FAILURE "There's no deployment script for '${DEPLOY_MODE}' mode."
fi
source "${MODE_IMPL}"
# Prepare tag message
DO_PREPARE_TAG_MESSAGE ${VERSION}
if [ -z "${DEPLOY_TAG_MESSAGE}" ]; then
WARNING "'${DEPLOY_MODE}' mode script failed to prepare message for TAG"
else
DEPLOY_TAG_MESSAGE="Version ${VERSION}"
fi
# Adjust main / master branch
if [ -z "$DEPLOY_MAIN_BRANCH" ]; then
MASTER_BRANCH='master'
else
MASTER_BRANCH=$DEPLOY_MAIN_BRANCH
fi
####
POP_DIR
}
# -----------------------------------------------------------------------------
# Executes "DO_DEPLOY" or custom function with given parameters
# -----------------------------------------------------------------------------
function EXECUTE_DO_DEPLOY
{
if [ -z "$DO_DEPLOY_CUSTOM_FUNCTION" ]; then
DO_DEPLOY "$@"
else
$DO_DEPLOY_CUSTOM_FUNCTION "$@"
fi
}
# -----------------------------------------------------------------------------
# Patches all files to required version string and creates tagged commit with
# this change.
# -----------------------------------------------------------------------------
function PATCH_VERSIONING_FILES
{
PUSH_DIR "${REPO_DIR}"
####
LOG "----- Patching files to ${VERSION}..."
if [ -z "$NO_DEPLOY_VERSIONING_FILES" ]; then
for (( i=0; i<${#DEPLOY_VERSIONING_FILES[@]}; i++ ));
do
local patch_info="${DEPLOY_VERSIONING_FILES[$i]}"
local files=(${patch_info//,/ })
local template="${files[0]}"
local target="${files[1]}"
local template_file="${REPO_DIR}/$template"
local target_file="${REPO_DIR}/$target"
if [ ! -f "$template_file" ]; then
FAILURE "Template file not found: $template_file"
fi
if [ ! -f "$target_file" ]; then
FAILURE "Target should exist: $target_file"
fi
LOG " + ${target}"
sed -e "s/%DEPLOY_VERSION%/$VERSION/g" "$template_file" > "$target_file"
git add "$target_file"
done
LOG "----- Commiting versioning files..."
git commit -m "Deployment: Update versioning file[s] to ${VERSION}"
else
FAILURE "Looks like this library has no versioning files defined."
fi
LOG "----- Tagging version ${VERSION}..."
git tag -a ${VERSION} -m "${DEPLOY_TAG_MESSAGE}"
####
POP_DIR
}
# -----------------------------------------------------------------------------
# Checks whether version already exists (has tag in git) and if not then
# prepares versioning files.
# -----------------------------------------------------------------------------
function PREPARE_VERSIONING_FILES
{
local CURRENT_TAGS=(`git tag -l`)
local TAG
local SKIP_CREATE=0
for TAG in ${CURRENT_TAGS[@]}; do
if [ "$TAG" == ${VERSION} ]; then
WARNING "Version '${VERSION}' is already tagged. Skipping files creation."
return
fi
done
PATCH_VERSIONING_FILES
}
# -----------------------------------------------------------------------------
# Prepares local files which contains version string, then commits those
# files with appropriate tag and pushes everything to the remote git repository
# -----------------------------------------------------------------------------
function PUSH_VERSIONING_FILES
{
PUSH_DIR "${REPO_DIR}"
###
LOG "----- Pushing changes to git..."
git push --follow-tags
####
POP_DIR
}
# -----------------------------------------------------------------------------
# Merges recent changes to the 'master' branch
# -----------------------------------------------------------------------------
function MERGE_TO_MASTER
{
PUSH_DIR "${REPO_DIR}"
####
LOG "----- Merging to '${MASTER_BRANCH}'..."
git fetch origin
git checkout ${MASTER_BRANCH}
git ${MERGE_MODE} origin/${DEV_BRANCH}
git push
git checkout ${DEV_BRANCH}
####
POP_DIR
}
###############################################################################
# Script's main execution starts here...
# -----------------------------------------------------------------------------
PINDEX=0
while [[ $# -gt 0 ]]
do
opt="$1"
case "$opt" in
-h | --help)
USAGE 0
;;
-v*)
SET_VERBOSE_LEVEL_FROM_SWITCH $opt
;;
--any-branch)
GIT_VALIDATE_DEVELOPMENT_BRANCH=0
;;
prepare | push | deploy | merge)
COMMANDS+=("$opt")
;;
-dm | --do-more-target)
DO_MORE_TARGET="$2"
shift
;;
--allow-warnings)
ALLOW_WARNINGS=1
;;
--merge-mode)
MERGE_MODE="$2"
shift
;;
*)
if [ x$PINDEX == x0 ]; then
REPO_DIR="$opt"
PINDEX=1
elif [ x$PINDEX == x1 ]; then
VALIDATE_AND_SET_VERSION_STRING $opt
PINDEX=2
else
FAILURE "Unknown parameter '$opt'"
fi
;;
esac
shift
done
#
# Mandatory parameters validation
#
if [ -z "$REPO_DIR" ]; then
FAILURE "You have to provide path to repository."
fi
if [ -z "$VERSION" ]; then
FAILURE "You have to provide version string."
fi
if [ ! -d "$REPO_DIR" ]; then
FAILURE "Provided path is not a directory: $REPO_DIR"
fi
# Full path to REPO_DIR
REPO_DIR="`( cd \"$REPO_DIR\" && pwd )`"
#
# Main job starts here...
#
VALIDATE_GIT_STATUS
LOAD_DEPLOY_INFO_FILE
PUSH_DIR "${REPO_DIR}"
if [ ${#COMMANDS[@]} -eq 0 ]; then
# if there are no commands, try look for DEPLOY_COMMANDS, or execute everything
if [ -z "$DEPLOY_COMMANDS" ]; then
DEBUG_LOG "Using default set of commands."
COMMANDS=( "prepare" "push" "deploy" "merge" )
else
DEBUG_LOG "Using default set of commands from .limedeploy file."
COMMANDS=( $DEPLOY_COMMANDS )
fi
fi
for COMMAND in "${COMMANDS[@]}"; do
case "$COMMAND" in
prepare)
PREPARE_VERSIONING_FILES
EXECUTE_DO_DEPLOY $VERSION 'prepare'
;;
push)
PUSH_VERSIONING_FILES
;;
deploy)
EXECUTE_DO_DEPLOY $VERSION 'deploy'
;;
merge)
MERGE_TO_MASTER
;;
esac
done
POP_DIR
EXIT_SUCCESS