-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.sh
executable file
·119 lines (96 loc) · 2.53 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
#!/usr/bin/env bash
set -u
set -o pipefail
GH_REMOTE="origin"
SOURCE_BRANCH="main"
DEPLOY_BRANCH="gh-pages"
readonly WEBSITE_DIR_PATH="dist"
# CLI constants
readonly __SCRIPT_NAME__="${0##*/}"
readonly __SEE_HELP_MESSAGE__="See '${__SCRIPT_NAME__} --help' for more information."
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 'main')
-d, --destination <branch> Branch to deploy to (defaults to 'gh-pages')
-r, --remote <remote> Remote to deploy to (defaults to 'origin')
EOF
}
function cli {
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
}
function is_working_tree_dirty {
! git diff --quiet
}
function abort {
local message="${1}"
printf "ERROR: %s\\n" "${message}" >&2
exit 1
}
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_deploy {
yarn lint || abort "Lint failure"
yarn build || abort "Failed to build"
local shorthash
shorthash=$(get_shorthash)
local commitMessage="Update using ${SOURCE_BRANCH}/${shorthash}"
yarn gh-pages --dist "$WEBSITE_DIR_PATH" --message "$commitMessage" || abort "gh-pages failed to deploy"
echo "Successfully pushed to ${GH_REMOTE}/${DEPLOY_BRANCH}"
}
# main program
cli "$@"
if is_working_tree_dirty
then
abort "Working tree is dirty; please clean it up and try again"
fi
preprocess_and_deploy
exit 0