11#! /usr/bin/env bash
22#
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
48
59set -eu
610
711PAGES_BRANCH=" gh-pages"
812
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+
1021_backup_dir=" $( mktemp -d) "
1122
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+
1238init () {
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."
1542 exit -1
1643 fi
1744
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 "
2272 fi
73+ }
2374
75+ setup_gh () {
2476 if [[ -z $( git branch -av | grep " $PAGES_BRANCH " ) ]]; then
25- _no_branch =true
77+ _no_pages_branch =true
2678 git checkout -b " $PAGES_BRANCH "
2779 else
2880 git checkout " $PAGES_BRANCH "
2981 fi
3082}
3183
3284backup () {
33- mv _site /* " $_backup_dir "
85+ mv " $SITE_DIR " /* " $_backup_dir "
3486 mv .git " $_backup_dir "
3587
3688 # When adding custom domain from Github website,
@@ -56,7 +108,7 @@ deploy() {
56108 git add -A
57109 git commit -m " [Automation] Site update No.${GITHUB_RUN_NUMBER} "
58110
59- if $_no_branch ; then
111+ if $_no_pages_branch ; then
60112 git push -u origin " $PAGES_BRANCH "
61113 else
62114 git push -f
@@ -65,9 +117,43 @@ deploy() {
65117
66118main () {
67119 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
68129 backup
69130 flush
70131 deploy
71132}
72133
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+
73159main
0 commit comments