-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdeploy_ghpages
executable file
·169 lines (141 loc) · 3.92 KB
/
deploy_ghpages
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env bash
set -e
dir0="${PWD}"
sdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
root_dir="${sdir}"
ghp_dir="${root_dir}/.ghpages"
doclean=0
pretend=0
msg="Automated Deployment"
dest_dir=""
files=""
repo_slug="${DEPLOY_SLUG}"
# usage: abs_path="$(abspath ${relative_path} [${start}])"
# `start` is the path that the relative path is relative to; it defaults to ${PWD}
abspath() { cd "${2:-.}"; echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"; }
##################
# establish usage
print_usage() {
cat << EOF
Usage: ${0##*/} [-hpc] [-r REPO] [-m MSG] -d DEST file1 [file2 [file3 [...]]]
Copy and commit a set of files to the gh-pages branch
Optional Arguments:
-h help
-p pretend, i.e., do not actually do the commit/push
-c clean, i.e., remove (rm -rf) DEST
-r REPO github repository slug, i.e., "user/repo"
-m MSG commit message
Required Arguments:
-d DEST Destination directory relative to root on the gh-pages
branch
file* One or more files to move to DEST and commit
EOF
exit $1
}
##################
# parse arguments
OPTIND=1
while getopts "h?pcm:r:d:" arg; do
case $arg in
h|\?)
print_usage 0;;
p) pretend=1;;
c) doclean=1;;
m) msg=${OPTARG};;
r) repo_slug=${OPTARG};;
d) dest_dir=${OPTARG};;
*) print_usage 1;;
esac
done
shift $((OPTIND-1))
files=(); for f in "$@"; do files+=("${f}"); done
if [ -z "${dest_dir}" ]; then
echo "Error, missing DEST" >&2
echo "" >&2
print_usage 1
fi
if [ ${#files[@]} -eq 0 ]; then
echo "Error, you must supply one or more FILES" >&2
echo "" >&2
print_usage 2
fi
if [ "${repo_slug}" == "" ]; then
echo "Error, missing REPO" >&2
echo "" >&2
print_usage 3
fi
###################
# now we may begin
if [ ${pretend} -ne 0 ]; then
addopt="-v"
else
addopt=" "
fi
if [[ -n ${TRAVIS} && -n ${CONTINUOUS_INTEGRATION} ]]; then
# ok, so now on travis-ci, you will need to create an encrypted
# file that contains a private rsa key. In the before-install
# phase, you'll need to decrypt that file into ~/.ssh/id_rsa
# and give the correct permission to ~/.ssh (700) and
# ~/.ssh/id_rsa (600). Then, you can add the public key to the
# target repository's deployment keys on github.
repo="[email protected]:${repo_slug}.git"
git config --global user.name "Travis-CI"
git config --global user.email "[email protected]"
git config --global push.default simple
else
# this scipt is probably being run locally, so let's assume the user
# has set up passwordless access to github with an account that can
# push to the upstream repo
repo="[email protected]:${repo_slug}.git"
fi
echo "using repo::" ${repo}
git clone -b gh-pages ${repo} "${ghp_dir}"
if [ ! -d ${ghp_dir} ]; then
echo "Repo directory doesn't exist, the clone must have failed." >&2
exit 30
fi
cd "${ghp_dir}"
if [[ ${doclean} && -d "${dest_dir}" ]]; then
rm -rf "${dest_dir}"
fi
set +e; mkdir -p "${dest_dir}"; set -e
# make sure files are absolute paths
for f in ${files[@]}; do
cp ${addopt} -r "$(abspath ${f} ${dir0})" "${dest_dir}"
done
git add --all --force ${dest_dir}
if [ "$(git diff --name-only --cached)" != "" ]; then
if [ ${pretend} -ne 0 ]; then
echo "[PRETEND] ${msg}"
else
git commit -m "${msg}"
push_errcode=1
max_push_tries=5
push_tries=0
set +e
while [[ push_errcode -ne 0 && ${push_tries} -lt ${max_push_tries} ]]; do
echo "Push attempt ${push_tries}"
git pull --rebase
git push
push_errcode=$?
push_tries=$((${push_tries} + 1))
if [ ${push_errcode} -ne 0 ]; then
sleep 2
fi
done
set -e
if [ ${push_errcode} -eq 0 ]; then
echo "Push succeeded after ${push_tries} attempt(s)"
else
echo "Push failed ${push_tries} times"
exit ${push_errcode}
fi
fi
else
echo "No changes made, so no commit for you"
fi
cd "${dir0}"
rm -rf "${ghp_dir}"
##
## EOF
##