-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·87 lines (67 loc) · 1.48 KB
/
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
#!/bin/sh
set -o errexit
set -o nounset
if [ "${TRACE-0}" = "1" ]; then set -o xtrace; fi
if [ -f "./.env" ]; then
. ./.env
fi
skip_build=
error() {
echo "$0: $1" 1>&2
exit 1
}
check_environment_variables() {
error=
# shellcheck disable=SC2068
# We want explicit re-splitting here.
for var in $@; do
# POSIX shell doesn't support variable indirection, we use eval. $var is
# always set by the for-loop, but value might not be, so we escape the
# $, { and } characters
eval value=\"\$\{"$var"-\}\"
if [ -z "$value" ]; then
echo "\$$var is not set."
error=1
fi
done
if [ -n "$error" ]; then
error "Not all environment variables are set."
fi
}
handle_args() {
while getopts :s opt; do
case $opt in
s)
skip_build="true"
;;
'?')
error "invalid option -$OPTARG"
;;
esac
done
shift $((OPTIND - 1))
}
build_project() {
npm run build
}
remove_old_files() {
echo "Removing old files..."
ssh "$DESTINATION" rm -rf "$REMOTE_DIR/*"
echo "Removed old files."
}
copy_new_files() {
echo "Copying new files..."
scp -qr "$LOCAL_DIR"/* "$DESTINATION:$REMOTE_DIR/"
echo "Copied new files."
}
check_environment_variables "DESTINATION LOCAL_DIR REMOTE_DIR"
handle_args "$@"
if [ -z "$skip_build" ]; then
build_project
elif [ -d "$LOCAL_DIR" ]; then
echo "Using existing build..."
else
error "Attempted to use existing build, but $LOCAL_DIR doesn't exist."
fi
remove_old_files
copy_new_files