-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbuild.sh
executable file
·297 lines (231 loc) · 6.41 KB
/
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
#!/bin/bash
set -e
#
# Script to build and push new image in remote docker repository
#
DRY="true"
REPO="https://github.com/unep-grid/mapx"
BRANCH=$(git branch --show-current)
REMOTE="github"
DOCKER_REPO_APP="fredmoser/mapx_app"
DOCKER_REPO_API="fredmoser/mapx_api"
NEW_VERSION=""
OLD_VERSION=$(cat version.txt)
FG_GREEN="\033[32m"
FG_RED="\033[31m"
FG_NORMAL="\033[0m"
FG_CYAN="\033[36m"
DIR_CUR="$(pwd)"
DIR_APP="$DIR_CUR/app"
DIR_SDK="$DIR_CUR/app/src/js/sdk"
DIR_API="$DIR_CUR/api"
CHANGES_CHECK="$(git status --porcelain | wc -l)"
CUR_HASH="$(git rev-parse HEAD)"
CHANGELOG="changelog.md"
CHANGELOG_TMP="/tmp/changelog.md"
#
# log/re log in docker
#
docker login
#
# Helpers
#
usage() {
echo -e "Usage: $0 [-v version <version>. E.g. \"-v $FG_CYAN$OLD_VERSION$FG_NORMAL\" ] [-a actually do it]" 1>&2;
if [[ -z $DRY ]]
then
exit 1
fi
}
# generic error
error()
{
echo -e "${FG_RED}ERROR${FG_NORMAL}: $1"
if [[ $2 -eq 1 ]]
then
usage
fi
if [[ -z $DRY ]]
then
exit 1
fi
}
# Update version in package.json
update_json()
{
yq -o=json -P -i '.version = "'$NEW_VERSION'"' $1
}
# Check tooling
check_command()
{
if [[ -z `command -v $1` ]];
then
error "Missing command $1, please install it"
fi
}
#--------------------------------------------------------------------------------
# Check installed tools
#--------------------------------------------------------------------------------
check_command 'yq'
check_command 'git'
check_command 'docker'
check_command 'npm'
check_command 'vim'
#--------------------------------------------------------------------------------
# Parse options
#--------------------------------------------------------------------------------
while getopts "hav:" opt; do
case "$opt" in
h|\?)
usage
;;
v)
NEW_VERSION=$OPTARG
;;
a)
DRY=""
;;
esac
done
if [[ $CHANGES_CHECK -gt 0 ]]
then
error "This project has uncommited changes"
fi
if [[ -z "$NEW_VERSION" ]]
then
error "Missing version" 1
fi
if [[ "$NEW_VERSION" == "$OLD_VERSION" ]]
then
error "Same old/new version" 1
fi
DOCKER_TAG_API="$DOCKER_REPO_API:$NEW_VERSION"
DOCKER_TAG_APP="$DOCKER_REPO_APP:$NEW_VERSION"
#--------------------------------------------------------------------------------
# Confirm
#--------------------------------------------------------------------------------
MSG_CONFIRM="Build, commit and push $FG_GREEN$NEW_VERSION$FG_NORMAL on branch $FG_GREEN$BRANCH$FG_NORMAL in remote $FG_GREEN$REMOTE$FG_NORMAL [YES/NO]"
if [[ -z $DRY ]]
then
MSG_CONFIRM="Dry mode ${FG_GREEN}DISABLED${FG_NORMAL}: $MSG_CONFIRM"
else
MSG_CONFIRM="Dry mode ${FG_CYAN}ENABLED${FG_NORMAL}: $MSG_CONFIRM"
fi
echo -e "$MSG_CONFIRM "
if [[ -z $DRY ]]
then
read confirm_diff
if [[ "$confirm_diff" != "YES" ]]
then
error "User cancelled"
exit 1
fi
fi
#--------------------------------------------------------------------------------
# Update versions in files
#--------------------------------------------------------------------------------
echo -e "Update versions in version.txt, package.json to $FG_GREEN$NEW_VERSION$FG_NORMAL "
echo -e "Update api/api_dev image in docker-compose.yml to $FG_GREEN$DOCKER_TAG_API$FG_NORMAL "
echo -e "Update app/app_dev image in docker-compose.yml to $FG_GREEN$DOCKER_TAG_APP$FG_NORMAL "
if [[ -z $DRY ]]
then
# Package, versions.txt
echo $NEW_VERSION > version.txt
update_json ./app/package.json
update_json ./app/src/js/sdk/package.json
update_json ./api/package.json
# Compose file
yq -o='yaml' -i '.services.app.image ="'$DOCKER_TAG_APP'"' docker-compose.yml
yq -o='yaml' -i '.services.app_dev.image ="'$DOCKER_TAG_APP'"' docker-compose.yml
yq -o='yaml' -i '.services.api.image ="'$DOCKER_TAG_API'"' docker-compose.yml
yq -o='yaml' -i '.services.api_dev.image ="'$DOCKER_TAG_API'"' docker-compose.yml
fi
#--------------------------------------------------------------------------------
# Linting
#--------------------------------------------------------------------------------
cd $DIR_APP
echo "Lint app"
if [[ -z $DRY ]]
then
npm run lint
fi
if [[ ! $? -eq 0 ]]
then
error "App linting failed"
fi
cd $DIR_API
echo "Lint api"
if [[ -z $DRY ]]
then
npm run lint
fi
if [[ ! $? -eq 0 ]]
then
error "Api linting failed"
fi
cd $DIR_CUR
#--------------------------------------------------------------------------------
# Build prod
#--------------------------------------------------------------------------------
echo "Build sdk, app, api prod + build docker images"
if [[ -z $DRY ]]
then
echo "Build sdk prod"
cd $DIR_SDK
npm run prod
echo "Build app prod"
cd $DIR_APP
npm run prod_docker
echo "Build api prod"
cd $DIR_API
npm run prod_docker
fi
cd $DIR_CUR
#--------------------------------------------------------------------------------
# Write changes
#--------------------------------------------------------------------------------
MSG_HEAD_CHANGE="[${NEW_VERSION}](${REPO}/tree/${NEW_VERSION})"
echo "Write change header: $MSG_HEAD_CHANGE "
if [[ -z $DRY ]]
then
echo -e " - $MSG_HEAD_CHANGE \n"\
> $CHANGELOG_TMP
cat $CHANGELOG >> $CHANGELOG_TMP
cp $CHANGELOG_TMP $CHANGELOG
vim $CHANGELOG
echo "Get diff"
git --no-pager diff --minimal
echo "Verify git diff of versioning changes. Continue (commit, push) ? [YES/NO/EDIT]"
while true; do
read confirm_diff
confirm_diff=$(echo "$confirm_diff" | awk '{print tolower($0)}')
if [[ "$confirm_diff" == "yes" || "$confirm_diff" == "y" ]]; then
echo "Proceeding with commit, build, and push."
break
elif [[ "$confirm_diff" == "no" || "$confirm_diff" == "n" ]]; then
echo "Stop here, stash changes. rollback to $CUR_HASH"
git stash
exit 1
elif [[ "$confirm_diff" == "edit" || "$confirm_diff" == "e" ]]; then
echo "Re-editing the diff file."
vim $CHANGELOG
echo "Get diff"
git --no-pager diff --minimal
echo "Verify git diff of versioning changes. Continue (commit, push) ? [YES/NO/EDIT]"
else
echo "Invalid input. Please enter 'YES', 'NO', 'EDIT', 'y', 'n', or 'e':"
fi
done
fi
#--------------------------------------------------------------------------------
# Git commit
#--------------------------------------------------------------------------------
echo "Commit"
if [[ -z $DRY ]]
then
git add .
git commit -m "auto build version $NEW_VERSION"
git tag $NEW_VERSION
git push $REMOTE $BRANCH --tags
fi
echo "Done"