-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·107 lines (91 loc) · 2.67 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
set -e
: "${SOURCE_BRANCH:=main}"
: "${TARGET_BRANCH:=pages}"
: "${ORIGIN:=origin}"
# Disable this if you've already run the build script.
: "${BUILD:=true}"
# Explode and deploy the contents of this directory.
EXPLODE=dist
# Adapted from:
# https://github.com/git/git/blob/8d530c4d64ffcc853889f7b385f554d53db375ed/git-sh-setup.sh#L207-L222
ensure_clean_working_tree() {
local err=0
if ! git diff-files --quiet --ignore-submodules; then
echo >&2 "Cannot deploy: You have unstaged changes."
err=1
fi
if ! git diff-index --cached --quiet --ignore-submodules HEAD -- ; then
if [[ "$err" -eq 0 ]]; then
echo >&2 "Cannot deploy: Your index contains uncommitted changes."
else
echo >&2 "Additionally, your index contains uncommitted changes."
fi
err=1
fi
if [[ "$err" -ne 0 ]]; then
exit "$err"
fi
}
ensure_no_stopships() {
if git grep -n -i "STOPSHIP" -- './*' ':!deploy.sh'; then
echo >&2 "Cannot deploy: STOPSHIP annotations remain."
exit 1
fi
}
ensure_up_to_date() {
git fetch "$ORIGIN"
if [ "$(git rev-parse "$ORIGIN/$SOURCE_BRANCH")" \
!= "$(git rev-parse "$SOURCE_BRANCH")" ]; then
echo >&2 "Cannot deploy: not up to date with $ORIGIN/$SOURCE_BRANCH."
exit 1
fi
}
prompt() {
>&2 printf '%s\n' "$1"
>&2 printf 'yes/no> '
read line
if [[ "$line" != "yes" ]]; then
>&2 printf '%s\n' "Aborting!"
fi
printf '%s' "$line"
}
if [[ "$(git rev-parse --abbrev-ref HEAD)" != "$SOURCE_BRANCH" ]]; then
msg="You're not on '$SOURCE_BRANCH'. Do you want to go there now?"
if [[ "$(prompt "$msg")" != "yes" ]]; then
exit 0
else
git checkout "$SOURCE_BRANCH"
fi
fi
ensure_clean_working_tree
ensure_no_stopships
ensure_up_to_date
SOURCE_COMMIT="$(git rev-parse HEAD)"
printf 'Preparing to deploy commit %s.\n' "$SOURCE_COMMIT"
TMPDIR="$(mktemp -d)"
printf 'Temporary directory: %s\n' "$TMPDIR"
if [[ "$BUILD" == "true" ]]; then
yarn build
else
printf '%s\n' "Warning: skipping build."
fi
cp -r "$EXPLODE"/* "$TMPDIR"
pushd "$TMPDIR"
STAGE=( * )
popd
git checkout --orphan "$TARGET_BRANCH" || git checkout "$TARGET_BRANCH"
git rm -r --cached --ignore-unmatch .
cp -r "$TMPDIR"/* .
git add "${STAGE[@]}" -f
git commit --allow-empty --no-verify -m "Deploy: $SOURCE_COMMIT"
echo
printf 'Please review the build output now---run:\n'
printf ' cd "%s" && python -m SimpleHTTPServer\n' "$TMPDIR"
msg="Do you want to deploy?"
if [[ "$(prompt "$msg")" == "yes" ]]; then
git push origin "$TARGET_BRANCH"
fi
git clean -xfd -e node_modules
git checkout "$SOURCE_BRANCH"
rm -rf "$TMPDIR"