1
1
#! /usr/bin/env bash
2
2
#
3
- # Deploy the content of _site to 'origin/<pages_branch>'
3
+ # Build, test and then deploy the site content to 'origin/<pages_branch>'
4
+ #
5
+ # Requirement: html-proofer, jekyll
6
+ #
7
+ # Usage: See help information
4
8
5
9
set -eu
6
10
7
11
PAGES_BRANCH=" gh-pages"
8
12
9
- _no_branch=false
13
+ SITE_DIR=" _site"
14
+
15
+ _opt_dry_run=false
16
+
17
+ _config=" _config.yml"
18
+
19
+ _no_pages_branch=false
20
+
10
21
_backup_dir=" $( mktemp -d) "
11
22
23
+ _baseurl=" "
24
+
25
+ help () {
26
+ echo " Build, test and then deploy the site content to 'origin/<pages_branch>'"
27
+ echo
28
+ echo " Usage:"
29
+ echo
30
+ echo " bash ./tools/deploy.sh [options]"
31
+ echo
32
+ echo " Options:"
33
+ echo ' -c, --config "<config_a[,config_b[...]]>" Specify config file(s)'
34
+ echo " --dry-run Build site and test, but not deploy"
35
+ echo " -h, --help Print this information."
36
+ }
37
+
12
38
init () {
13
- if [[ -z ${GITHUB_ACTION+x} ]]; then
14
- echo " ERROR: This script is not allowed to run outside of GitHub Action."
39
+ if [[ -z ${GITHUB_ACTION+x} && $_opt_dry_run == ' false' ]]; then
40
+ echo " ERROR: It is not allowed to deploy outside of the GitHub Action envrionment."
41
+ echo " Type option '-h' to see the help information."
15
42
exit -1
16
43
fi
17
44
18
- # Gemfile could be changed by `bundle install` in actions workflow
19
- if [[ -n $( git ls-files | grep Gemfile.lock) && -n \
20
- $( git status Gemfile.lock --porcelain) ]]; then
21
- git checkout -- Gemfile.lock
45
+ _baseurl=" $( grep ' ^baseurl:' _config.yml | sed " s/.*: *//;s/['\" ]//g;s/#.*//" ) "
46
+ }
47
+
48
+ build () {
49
+ # clean up
50
+ if [[ -d $SITE_DIR ]]; then
51
+ rm -rf " $SITE_DIR "
52
+ fi
53
+
54
+ # build
55
+ JEKYLL_ENV=production bundle exec jekyll b -d " $SITE_DIR$_baseurl " --config " $_config "
56
+ }
57
+
58
+ test () {
59
+ bundle exec htmlproofer \
60
+ --disable-external \
61
+ --check-html \
62
+ --allow_hash_href \
63
+ " $SITE_DIR "
64
+ }
65
+
66
+ resume_site_dir () {
67
+ if [[ -n $_baseurl ]]; then
68
+ # Move the site file to the regular directory '_site'
69
+ mv " $SITE_DIR$_baseurl " " ${SITE_DIR} -rename"
70
+ rm -rf " $SITE_DIR "
71
+ mv " ${SITE_DIR} -rename" " $SITE_DIR "
22
72
fi
73
+ }
23
74
75
+ setup_gh () {
24
76
if [[ -z $( git branch -av | grep " $PAGES_BRANCH " ) ]]; then
25
- _no_branch =true
77
+ _no_pages_branch =true
26
78
git checkout -b " $PAGES_BRANCH "
27
79
else
28
80
git checkout " $PAGES_BRANCH "
29
81
fi
30
82
}
31
83
32
84
backup () {
33
- mv _site /* " $_backup_dir "
85
+ mv " $SITE_DIR " /* " $_backup_dir "
34
86
mv .git " $_backup_dir "
35
87
36
88
# When adding custom domain from Github website,
@@ -56,7 +108,7 @@ deploy() {
56
108
git add -A
57
109
git commit -m " [Automation] Site update No.${GITHUB_RUN_NUMBER} "
58
110
59
- if $_no_branch ; then
111
+ if $_no_pages_branch ; then
60
112
git push -u origin " $PAGES_BRANCH "
61
113
else
62
114
git push -f
@@ -65,9 +117,43 @@ deploy() {
65
117
66
118
main () {
67
119
init
120
+ build
121
+ test
122
+ resume_site_dir
123
+
124
+ if $_opt_dry_run ; then
125
+ exit 0
126
+ fi
127
+
128
+ setup_gh
68
129
backup
69
130
flush
70
131
deploy
71
132
}
72
133
134
+ while (( $# )) ; do
135
+ opt=" $1 "
136
+ case $opt in
137
+ -c | --config)
138
+ _config=" $2 "
139
+ shift
140
+ shift
141
+ ;;
142
+ --dry-run)
143
+ # build & test, but not deploy
144
+ _opt_dry_run=true
145
+ shift
146
+ ;;
147
+ -h | --help)
148
+ help
149
+ exit 0
150
+ ;;
151
+ * )
152
+ # unknown option
153
+ help
154
+ exit 1
155
+ ;;
156
+ esac
157
+ done
158
+
73
159
main
0 commit comments