-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·139 lines (108 loc) · 2.98 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
#!/bin/bash
set -o errexit
set -o nounset
function help() {
cat <<EOH
Usage: $0 [-h|[-x] --skip-build] PATH_TO_FRONTEND_REPO PATH_TO_BACKEND_REPO
Options:
-h
Show this help.
-x
Be extra verbose
--skip-build
If specified, will only build the container, but not copy
any code or files from the frontend or the backend
--skip-image-build
If specified, it will not build the container image (usually for CI)
Arguments:
PATH_TO_FRONTEND_REPO
Path to the directory containing the frontend code.
PATH_TO_BACKEND_REPO
Path to the directory containing the backend server code.
Example:
# For personal laptops (usually x86 architecture)
./build.sh ../expenses-react ../expenses_server_fastapi x86_64
# or the following to build the image for the raspberry pi
./build.sh ../expenses-react ../expenses_server_fastapi arm64v8
EOH
}
function main() {
local do_build=true
local do_image_build=true
if [[ $# -lt 3 ]]; then
echo "Not enough arguments passed"
help
exit 1
fi
if [[ "$1" == "-h" ]]; then
help
exit 0
fi
if [[ "$1" == "-x" ]]; then
set -x
shift
fi
if [[ "$1" == "--skip-build" ]]; then
do_build=false
shift
fi
if [[ "$1" == "--skip-image-build" ]]; then
do_image_build=false
shift
fi
local fe_path="${1:?No frontend path passed}"
local be_path="${2:?No backend path passed}"
local env="${3:-}"
if [[ "$env" == "prod" ]]; then
arch="arm64v8"
dockerfile="./Dockerfile.prod"
else
arch="x86_64"
dockerfile="./Dockerfile"
fi
if $do_build; then
cleanup
buildFrontend "$fe_path"
copyBackend "$be_path" "./src"
copyFrontend "$fe_path" "src/static"
fi
if $do_image_build; then
buildContainer "$arch" "$dockerfile"
fi
}
function buildContainer() {
local arch="${1:-}"
local dockerfile="${2:-}"
podman build \
--tag "expenses-app:dev-latest" \
--arch "${arch}" \
-f "${dockerfile}" \
.
}
function cleanup() {
rm -rf ./src
}
function buildFrontend() {
local fe_path="${1:?No frontend path passed}"
# TODO: The fronend should be built inside the fe_path
cd "${fe_path}"
rm -rf node_modules
yarn install --frozen-lockfile
yarn run build
cd -
}
function buildBackend() {
local be_path="${1:?No backend path passed}"
cd "${be_path}"
}
function copyBackend() {
local be_path="${1:?No backend path passed}"
local be_dest_path="${2:?No backend destination path passed}"
cp -a "${be_path}" "${be_dest_path}"
}
function copyFrontend() {
local fe_path="${1:?No frontend path passed}"
local fe_dest_path="${2:?No frontend destination path passed}"
cp -a "${fe_path}/build" "${fe_dest_path}"
}
main "$@"