-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.sh
executable file
·147 lines (119 loc) · 2.77 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
set -e
set -o pipefail
dir="$( cd "$(dirname "$0")" ; pwd -P )"
cd $dir
if [ "$1" != "" ]; then
branch=$1
else
branch=master
fi
if [ ! -d "working" ]; then
./init.sh
fi
deploy=$dir/working/deploy
if [ "$branch" != "master" ]; then
deploy=$deploy/$branch
mkdir -p $deploy
fi
source=$dir/working/source
merge_trees() {
local source=$1
local dest=$2
cd $source
# Ideally we would use cp --parent, but that doesn't work on macOS
find . -type f | grep -v .git | xargs -I % rsync -R % $dest
}
get_old_commit() {
cd $deploy
local commits=$(git log --pretty=format:'%s' -n 20 | grep '[$1]')
if [[ $commits =~ [a-f0-9]{40} ]]; then
echo $BASH_REMATCH
else
echo "???"
fi
}
get_current_commit() {
cd $source
echo $(git rev-parse HEAD)
}
yes_or_no() {
# https://stackoverflow.com/a/29436423
while true; do
read -p "[Deploy] $* [y/n]: " yn
case $yn in
[Yy]*) return 0 ;;
[Nn]*) echo "Aborted" ; return 1 ;;
esac
done
}
scan_script() {
local script=$1
#if [[ $(cat $script) =~ ' debugger;' ]]; then
# echo "Found bad debugger statement in $script; aborting"
# exit 1
#fi
}
echo "[Deploy] Updating repositories"
cd $deploy
git fetch
git reset --hard origin/master
cd $source
git fetch origin
git checkout $branch
git reset --hard origin/$branch
git pull origin $branch -Xtheirs
oldCommit=$(get_old_commit $branch)
newCommit=$(get_current_commit)
if [[ $oldCommit == $newCommit ]]; then
if ! yes_or_no "New commit and old commit appear to be the same. Continue? ($newCommit)"; then
exit 1
fi
fi
merge_trees "$source" "$deploy"
merge_trees "$dir/patches" "$deploy"
cd "$source"
python3 "$dir/apply-index-mods.py" "$deploy/index.html"
echo "[Deploy] Installing & Building"
cd $deploy
npm ci
npm run build
scan_script phosphorus.dist.js
# echo "[Deploy] Running tests"
# cd tests
# if [ ! -d node_modules ]; then
# if [ "$branch" != "master" ]; then
# if [ -d ../../tests/node_modules ]; then
# ln -s ../../tests/node_modules node_modules
# fi
# fi
# if [ ! -d node_modules ]; then
# npm ci
# fi
# fi
# node runner.js
cd $deploy
rm -r src tsconfig.json package.json package-lock.json dev.js
if [ "$branch" != "master" ]; then
rm README.md LICENSE
fi
git stage --all
echo "[Deploy] Modified files:"
git status -s
echo "(paths relative to $deploy)"
echo
subject="[$branch] Deploy $newCommit"
echo "[Deploy] Subject: $subject"
if ! yes_or_no "Continue with commit?"; then
exit 1
fi
git commit -m "$subject" -m "https://github.com/forkphorus/forkphorus/compare/$oldCommit...$newCommit" --author="forkphorus deploy bot <[email protected]>" --signoff
if yes_or_no "Push?"; then
git push origin master
else
git reset --hard HEAD~1
exit 1
fi
echo
echo "Everything is good!"
echo