Skip to content

Commit 37d4d4c

Browse files
Made the publish script generic to support publishing to arbitrary repositories and branches
1 parent edc2393 commit 37d4d4c

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

scripts/publish-ghpages.sh

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ ACTION="Manual"
1515
REPO_NAME="origin"
1616
SOURCE_BRANCH=`git rev-parse --abbrev-ref HEAD`
1717
QUIET=false
18+
USE_SITE_REPO=false
19+
PUSH_REPO=false
20+
PUSH_BRANCH="gh-pages"
21+
BOWER_WEB_FILES_ONLY=false
1822

1923
RED='\033[0;31m'
2024
YELLOW='\033[1;33m'
@@ -80,19 +84,23 @@ checkTriggerRepo () {
8084
}
8185

8286
inferRepo () {
83-
REPO=`git config remote.${REPO_NAME}.url`
84-
SSH_REPO=${REPO/https:\/\/github.com\//git@github.com:}
85-
echo "Inferred REPO ${SSH_REPO}"
87+
if [[ "${REPO_NAME}" == *.git ]]; then
88+
PUSH_REPO=$REPO_NAME
89+
else
90+
REPO=`git config remote.${REPO_NAME}.url`
91+
# Use the externally set global PUSH_REPO by default
92+
PUSH_REPO="${REPO/https:\/\/github.com\//git@github.com:}"
93+
fi
94+
echo "Inferred REPO ${PUSH_REPO}"
8695
}
8796

8897
confirmRepo () {
89-
confirm "${YELLOW}Push ${SITE_FOLDER} to repo ${SSH_REPO}/gh-pages? [y/N] ${NC}"
98+
confirm "${YELLOW}Push ${SITE_FOLDER} to repo ${PUSH_REPO}/${PUSH_BRANCH}? [y/N] ${NC}"
9099
return $?
91100
}
92101

93102
checkRemoteBranchExists () {
94-
BRANCH="${1:-gh-pages}"
95-
EXISTING=`git ls-remote --heads ${REPO} ${BRANCH}`
103+
EXISTING=`git ls-remote --heads ${PUSH_REPO} ${PUSH_BRANCH}`
96104
echo $EXISTING
97105
}
98106

@@ -115,12 +123,15 @@ cleanBranch () {
115123
}
116124

117125
cloneSite () {
118-
git clone --branch gh-pages $SSH_REPO github.io
126+
git clone --branch ${PUSH_BRANCH} $PUSH_REPO github.io
119127
}
120128

121129
copySite () {
122130
echo rsync -q -rav --delete --exclude .git ${SITE_FOLDER} github.io
123131
rsync -q -rav --delete --exclude .git ${SITE_FOLDER}/ github.io/
132+
if $BOWER_WEB_FILES_ONLY; then
133+
find ${SITE_FOLDER}/components -type f -not -regex ".*/.*\.\(html\|js\|css\|less\|otf\|eot\|svg\|ttf\|woff\|woff2\)" -print0 | xargs -0 rm
134+
fi
124135
}
125136

126137
pushSite () {
@@ -131,10 +142,10 @@ pushSite () {
131142
else
132143
echo -e "${YELLOW}Changes in site directory, committing changes.${NC}"
133144
git -C github.io commit -q -a -m "Added files from commit #${SHA} "
134-
echo -e "Pushing commit ${SHA} to repo ${SSH_REPO}."
145+
echo -e "Pushing commit ${SHA} to repo ${PUSH_REPO}."
135146
confirmRepo && rc=$? || rc=$?
136147
if [ "$rc" = 0 ]; then
137-
git -C github.io push $SSH_REPO gh-pages:gh-pages
148+
git -C github.io push $PUSH_REPO ${PUSH_BRANCH}:${PUSH_BRANCH}
138149
fi
139150
fi
140151
}
@@ -145,30 +156,31 @@ splitSite () {
145156
git commit -q -m "Added ${SITE_FOLDER} folder"
146157

147158
SHA=`git subtree split --prefix ${SITE_FOLDER} gh-pages-deploy`
148-
echo -e "Pushing commit ${SHA} to repo ${SSH_REPO}."
159+
echo -e "Pushing commit ${SHA} to repo ${PUSH_REPO}."
149160
confirmRepo && rc=$? || rc=$?
150161
if [ "$rc" = 0 ]; then
151-
git push ${SSH_REPO} ${SHA}:refs/heads/gh-pages --force
162+
git push ${PUSH_REPO} ${SHA}:refs/heads/${PUSH_BRANCH} --force
152163
fi
153164
}
154165

155166
deploySite () {
156-
if [ "$SOURCE_BRANCH" = "gh-pages" -o "$SOURCE_BRANCH" = "gh-pages-deploy" ]; then
157-
echo -e "${RED}Error: cannot deploy from the gh-pages branch. Please checkout a different branch."
167+
if [ "$SOURCE_BRANCH" = "gh-pages-deploy" ]; then
168+
echo -e "${RED}Error: cannot deploy from the current branch. Please checkout a different branch.${NC}"
169+
exit -1
158170
fi
159171

160172
git checkout ${SOURCE_BRANCH}
161173
inferRepo $REPO_NAME
162-
EXISTING=`checkRemoteBranchExists gh-pages`
174+
EXISTING=`checkRemoteBranchExists`
163175
if [ -n "$EXISTING" ]; then
164-
echo -e "${GREEN}### gh-pages branch exists, pushing updates${NC}"
176+
echo -e "${GREEN}### ${PUSH_BRANCH} branch exists, pushing updates${NC}"
165177
cleanSite
166178
cloneSite
167179
copySite
168180
pushSite
169181
cleanSite
170182
else
171-
echo -e "${GREEN}### gh-pages branch does not exist, splitting branch${NC}"
183+
echo -e "${GREEN}### ${PUSH_BRANCH} branch does not exist, splitting branch${NC}"
172184
cleanBranch
173185
splitSite
174186
cleanBranch
@@ -200,14 +212,15 @@ checkSiteFolderExists () {
200212
}
201213

202214
parseOpts() {
203-
while getopts htr:b: OPT "$@"; do
215+
while getopts htwb:r: OPT "$@"; do
204216
case $OPT in
205217
h) usage; exit 0;;
206218
t) ACTION="Travis"
207219
QUIET=true
208220
;;
209221
r) REPO_NAME=$OPTARG;;
210-
b) SOURCE_BRANCH=$OPTARG;;
222+
w) BOWER_WEB_FILES_ONLY=true;;
223+
b) PUSH_BRANCH=$OPTARG;;
211224
esac
212225
done
213226

@@ -218,7 +231,7 @@ parseOpts() {
218231
usage () {
219232
cat <<- EEOOFF
220233
221-
This script will publish files to the gh-pages branch of your repo.
234+
This script will publish files to the ${PUSH_BRANCH} branch of your repo.
222235
223236
$SCRIPT [option] folder
224237
@@ -227,19 +240,20 @@ cat <<- EEOOFF
227240
OPTIONS:
228241
h Display this message
229242
t Perform a deploy from travis, using a travis encrypted key
230-
r Repository name to publish to (deafult: origin)
231-
b Source branch to publish from (default: master)
243+
w Remove non-web files from the SITE_FOLDER/components folder prior to publishing
244+
b Remote branch this script will publish to
245+
default: gh-pages
246+
r Git repo this script will publish to
247+
eg.: origin, upstream, bleathem, [email protected]:bleathem/bleathem.github.io.git
248+
default: origin
232249
233250
EEOOFF
234251
}
235252

236253
main () {
237254
parseOpts "$@"
238-
239255
checkSiteFolderExists
240-
241-
echoHeader "${ACTION} deploy of ${REPO_NAME}/${SOURCE_BRANCH}:${SITE_FOLDER} to gh-pages"
242-
256+
echoHeader "${ACTION} deploy of ${SOURCE_BRANCH}:${SITE_FOLDER} to ${PUSH_BRANCH}"
243257
case $ACTION in
244258
Manual)
245259
manualDeploy "$@"

0 commit comments

Comments
 (0)