1
+ #! /usr/bin/env bash
2
+
3
+ # Run this script to deploy the app to Github Pages
4
+
5
+ # Parse cmd arguments
6
+
7
+ SRC_BRANCH=" master"
8
+ DEPLOY_BRANCH=" gh-pages"
9
+
10
+ USAGE_MSG=" usage: deploy [-h|--help] [-u|--user] [-s|--src SRC_BRANCH] [-d|--deploy DEPLOY_BRANCH]"
11
+
12
+ while [[ $# > 0 ]]; do
13
+ key=" $1 "
14
+
15
+ case $key in
16
+ -h|--help)
17
+ echo $USAGE_MSG
18
+ exit 0
19
+ ;;
20
+ -u|--user)
21
+ SRC_BRANCH=" source"
22
+ DEPLOY_BRANCH=" master"
23
+ shift
24
+ ;;
25
+ -s|--src)
26
+ SRC_BRANCH=" $2 "
27
+ shift
28
+ ;;
29
+ -d|--deploy)
30
+ DEPLOY_BRANCH=" $2 "
31
+ shift
32
+ ;;
33
+ * )
34
+ echo " Option $1 is unknown."
35
+ echo $USAGE_MSG
36
+ exit 0
37
+ ;;
38
+ esac
39
+ shift
40
+ done
41
+
42
+ # Exit if any subcommand fails
43
+ set -e
44
+
45
+ echo " Deploying..."
46
+ echo " Source branch: $SRC_BRANCH "
47
+ echo " Deploy branch: $DEPLOY_BRANCH "
48
+
49
+ read -r -p " Do you want to proceed? [y/N] " response
50
+ if [[ ! $response =~ ^([yY][eE][sS]| [yY])+$ ]]
51
+ then
52
+ echo " Aborting."
53
+ [[ " $0 " = " $BASH_SOURCE " ]] && exit 1 || return 1
54
+ fi
55
+
56
+ # Check if there are any uncommitted changes
57
+ if ! git diff-index --quiet HEAD --; then
58
+ echo " Changes to the following files are uncommitted:"
59
+ git diff-index --name-only HEAD --
60
+ echo " Please commit the changes before proceeding."
61
+ echo " Aborting."
62
+ [[ " $0 " = " $BASH_SOURCE " ]] && exit 1 || return 1
63
+ fi
64
+
65
+ # Switch to source branch (creates it if necessary from the current branch)
66
+ if [ ` git branch | grep $SRC_BRANCH | tr ' ' ' \n' | tail -1` ]
67
+ then
68
+ git checkout $SRC_BRANCH
69
+ else
70
+ git checkout -b $SRC_BRANCH
71
+ fi
72
+
73
+ # Checkout DEPLOY_BRANCH branch
74
+ if [ ` git branch | grep $DEPLOY_BRANCH ` ]
75
+ then
76
+ git branch -D $DEPLOY_BRANCH
77
+ fi
78
+ git checkout -b $DEPLOY_BRANCH
79
+
80
+ # Build site
81
+ bundle exec jekyll build
82
+
83
+ # Delete and move files
84
+ find . -maxdepth 1 ! -name ' _site' ! -name ' .git' ! -name ' CNAME' ! -name ' .gitignore' -exec rm -rf {} \;
85
+ mv _site/* .
86
+ rm -R _site/
87
+
88
+ # Push to DEPLOY_BRANCH
89
+ git add -fA
90
+ git commit --allow-empty -m " $( git log -1 --pretty=%B) [ci skip]"
91
+ git push -f -q origin $DEPLOY_BRANCH
92
+
93
+ # Move back to SRC_BRANCH
94
+ git checkout $SRC_BRANCH
95
+
96
+ echo " Deployed successfully!"
97
+
98
+ exit 0
0 commit comments