-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeploy.sh
executable file
·75 lines (62 loc) · 1.52 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
#!/bin/bash
# configuration
TARGET_BRANCH=gh-pages
cd $(dirname "$0")
CURR_REPO=$(git rev-parse --show-toplevel)
CURR_ORIGIN=$(git config --get remote.origin.url)
function usage() {
echo "Usage: ./deploy.sh [REMOTE-REPO-URL]"
echo ""
echo "By default, REMOTE-REPO-URL is '$CURR_ORIGIN',"
echo "the 'origin' of the git repository at '$CURR_REPO'."
exit 0
}
if [ "$1" == "-h" -o "$1" == "--help" ]; then
usage
fi
if [ $# -gt 0 ]; then
ORIGIN="$1"
shift
else
ORIGIN="$CURR_ORIGIN"
fi
BUILD_ARGS="$@"
BUILD_DIR=$(mktemp -d builddir-XXXX)
function onerr() {
cleanup_build_dir
exit 1
}
set -o errtrace # functions inherit the error trap
trap onerr ERR
function clone_remote_repo_to_build_dir() {
# may fail if "$CURR_REPO" is a 'shallow' repository
if git clone -b "$TARGET_BRANCH" --single-branch "$CURR_REPO" "$BUILD_DIR"; then
pushd "$BUILD_DIR"
git pull "$ORIGIN" "$TARGET_BRANCH"
popd
else
git clone -b "$TARGET_BRANCH" --single-branch "$ORIGIN" "$BUILD_DIR"
fi
}
function copy_files_to_build_dir() {
cp -R ./_site/* "$BUILD_DIR"
}
function commit_and_push_changes_to_remote_repo() {
cd "$BUILD_DIR"
git add --all
if git commit -m "Updated website output $(date '+%m/%d/%y %H:%M')"; then
git push "$ORIGIN" "$TARGET_BRANCH"
else
echo "No changes to generated website."
fi
}
function cleanup_build_dir() {
rm -rf "$BUILD_DIR"
}
function main() {
clone_remote_repo_to_build_dir
copy_files_to_build_dir
commit_and_push_changes_to_remote_repo
cleanup_build_dir
}
main