-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy.sh
executable file
·64 lines (57 loc) · 2.14 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
#!/bin/bash
#
# Builds html static pages and deploys them into gh-pages github repo branch.
#
# by Alexander Borsuk <[email protected]> from Minsk, Belarus.
#
# Useful debug options:
# -e aborts if any command has failed.
# -u aborts on using unset variable.
# -x prints all executed commands.
# -o pipefail aborts if on any failed pipe operation.
set -euo pipefail
# Publish directory where static html content is generated.
# TODO: Share it between scripts, or use one script with different commands.
OUT_DIR=docs
# Pre-requisites check.
[ -f ./deploy.sh ] || { echo "ERROR: It is not the root repo directory."; exit 1; }
[ -d ./.git ] || { echo "ERROR: It is not a git repository."; exit 1; }
git check-ignore -q $OUT_DIR || { echo "ERROR: Please git rm $OUT_DIR; git commit -a; and add $OUT_DIR to .gitignore."; exit 1; }
# Sanity check.
[[ "$OUT_DIR" != "/" ]] || { echo "Invalid OUT_DIR? $OUT_DIR"; exit 1; }
# Setup cloned git repo in the generated directory.
if [ ! -d "$OUT_DIR/.git" ]; then
echo "Initializing $OUT_DIR directory and binding it to gh-pages branch of the same repository."
if [ ! -d "$OUT_DIR" ]; then
mkdir "$OUT_DIR"
else
rm -rf "$OUT_DIR/*"
fi
# Repo should be up-to-date before copying it.
git remote update
cp -r .git "$OUT_DIR/.git"
fi
# Initialize and switch to gh-pages branch in the $OUT_DIR/.git repo.
pushd "$OUT_DIR"
# Clear all local changes to avoid pull merge conflicts.
git reset --hard HEAD
git checkout gh-pages > /dev/null 2>&1 && git clean -f && git pull || { git checkout --orphan gh-pages; git rm -rf .; }
popd
# Rebuild everything before deployment.
# Need to save and restore $OUT_DIR/.git directory because generator can delete everything in $OUT_DIR.
mv "$OUT_DIR/.git" .git.backup
source build.sh
mv .git.backup "$OUT_DIR/.git"
# Check if there are any changes to publish.
pushd "$OUT_DIR"
if [[ $(git status --porcelain | wc -l) -eq 0 ]]; then
echo "There is nothing to publish. Have you made any changes?"
popd
exit 0
fi
# Publish web site to Github Pages.
git add -A
git commit -m "Regenerated by deployment script."
git push -u origin gh-pages
popd
echo "Successfully published changes to GitHub Pages."