-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy.sh
executable file
·77 lines (65 loc) · 2.13 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
76
77
#!/bin/bash
# Script used by to push build folder content to gh-pages branch
# Note: $1 can be used to define the commit message
remote="undefined"
# Get repo url (https://github.com/user/repo.git or [email protected]:user/repo.git)
url=$(git config remote.origin.url)
# Prepare remote variable to be github.com/user/repo
if [[ $url == *"https"* ]]; then
echo "Use https URL with token for git operations .."
# Remove 'https://' prefix and '.git' suffix
remote=$(echo "${url:8:-4}")
# Check for github token
if [ -z "$GH_TOKEN" ]; then
echo "ERROR: Set environment variable GH_TOKEN to be able to push to github!"
exit
fi
else
echo "Use ssh URL without token for git operations .."
# Remove 'git@' prefix and '.git' suffix && replace : with /
remote=$(echo "${url:4:-4}")
remote=$(echo $remote | sed 's/:/\//g')
fi
echo "Deploying to $remote into branch gh-pages"
# Prepare gh-pages commit message
commitMessage="Manual deployment to Github Pages"
if [ -n "$1" ]; then
commitMessage="$1"
fi
# Extract user & repo names
set -- "$remote"
IFS="/"; declare -a split=($*)
user="${split[1]}"
repo="${split[2]}"
gitCloneUrl="$url"
gitPushUrl="$gitCloneUrl"
if [[ $url == *"https"* ]]; then
gitCloneUrl="https://$user:${GH_TOKEN}@$remote.git"
gitPushUrl="https://${GH_TOKEN}@$remote"
fi
# Clone master branch from user repo
git clone --quiet "$gitCloneUrl" --branch=master source
if [ ! -d "source" ]; then
echo "ERROR: Failed to clone $url"
exit
fi
cd source
# Get latest commit ID from master branch
head=$(git log --format="%h" -n 1)
user=$(git log -1 --pretty=format:'%an')
# Switch to gh-pages + apply changes
git checkout --quiet gh-pages
git ls-files -z | xargs -0 rm -f
git ls-tree --name-only -d -r -z HEAD | sort -rz | xargs -0 rmdir
cp -rf ../build/* .
git add -A
# Check for changes
status=$(git status)
echo "$status";
# Setup travis git user, commit and push changes
if [[ $status != *"nothing to commit"* ]] ; then
git config user.name "travis"
git config user.email "[email protected]"
git commit -m "$commitMessage (triggered by: $user@$head)"
git push --force --quiet "$gitPushUrl" gh-pages:gh-pages > /dev/null 2>&1
fi