-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
129 lines (102 loc) · 3 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env bash
set -u
set -o pipefail
readonly __SCRIPT_NAME__="${0##*/}"
readonly __SEE_HELP_MESSAGE__="See '${__SCRIPT_NAME__} --help' for more information."
GH_REMOTE='origin'
SOURCE_BRANCH='master'
DEPLOY_BRANCH='gh-pages'
WEBSITE_DIR_PATH='dist'
RETURN_PATH='..'
function abort {
local message="${1}"
printf "ERROR: %s\\n" "${message}" >&2
exit 1
}
function show_help {
cat << EOF
${__SCRIPT_NAME__}
Deploy site to GitHub Pages branch
Options:
-h, --help Show help text
-s, --source <branch> Upstream branch (defaults to 'master')
-d, --destination <branch> Branch to deploy to (defaults to 'gh-pages')
-r, --remote <remote> Remote to deploy to (defaults to 'origin')
EOF
}
function is_working_tree_dirty {
! git diff --quiet
}
function get_shorthash {
local shorthash
shorthash=$(git rev-parse --short "refs/heads/${SOURCE_BRANCH}")
# the shorthash is 7 characters long
if [ ${#shorthash} -lt 7 ]
then
abort "git rev-parse returned an invalid shorthash from branch '${SOURCE_BRANCH}'"
fi
echo "${shorthash}"
}
function preprocess_and_publish {
yarn lint || abort "Lint failure"
yarn build || abort "Failed to build"
local shorthash
shorthash=$(get_shorthash)
local remote_url
remote_url=$(git remote get-url "${GH_REMOTE}")
cd ${WEBSITE_DIR_PATH} || abort "Failed to navigate to website directory"
git init || abort "Failed to initialize git repository in website directory"
git add -A . || abort "Failed to stage website files"
git commit -m "update using ${SOURCE_BRANCH}/${shorthash}" || abort "Failed to commit website files"
git push -f "${remote_url}" ${SOURCE_BRANCH}:${DEPLOY_BRANCH} || abort "Failed to push to ${GH_REMOTE}/${DEPLOY_BRANCH}"
echo "Successfully pushed to ${GH_REMOTE}/${DEPLOY_BRANCH}"
rm -rf .git || abort "Failed to delete .git folder in ${WEBSITE_DIR_PATH}"
cd ${RETURN_PATH} || abort "Failed to navigate back to root directory"
}
function main {
while :; do
case "${1-default}" in
-h|--help)
show_help
exit
;;
-s|--source)
if [[ -z $2 ]]; then
printf "'source' option requires an argument.\\n" >&2
printf "%s\\n" "${__SEE_HELP_MESSAGE__}" >&2
exit 1
fi
SOURCE_BRANCH="${2}"
shift
;;
-d|--destination)
if [[ -z $2 ]]; then
printf "'destination' option requires an argument.\\n" >&2
printf "%s\\n" "${__SEE_HELP_MESSAGE__}" >&2
exit 1
fi
DEPLOY_BRANCH="${2}"
shift
;;
-r|--remote)
if [[ -z $2 ]]; then
printf "'remote' option requires an argument.\\n" >&2
printf "%s\\n" "${__SEE_HELP_MESSAGE__}" >&2
exit 1
fi
GH_REMOTE="${2}"
shift
;;
*)
break
esac
shift
done
if is_working_tree_dirty
then
abort 'Working tree is dirty; please clean it up and try again'
fi
preprocess_and_publish
exit 0
}
main "$@"