-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeploy
executable file
·167 lines (144 loc) · 4.5 KB
/
deploy
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
set -e
# Make sure we're in the right directory:
cd "$(dirname ${BASH_SOURCE[0]})"/..
usage_and_exit() {
echo "Usage: $0 --only-build | (staging|production)"
exit 1
}
if [ "$1" = "--only-build" ]
then
ONLY_BUILD=1
shift
if [ "$#" != 0 ]
then
usage_and_exit
fi
fi
# Check parameters and set up environment variables that relate to
# actually pushing a new version of the site:
if [ "$ONLY_BUILD" != 1 ]
then
if [ "$1" = 'production' ] || [ "$1" = 'staging' ]
then
DEPLOY_TYPE="$1"
else
usage_and_exit
fi
if [ "$DEPLOY_TYPE" = 'production' ]
then
GH_PAGES_REPO='shineyoureye-static'
export RACK_ENV=production
else
GH_PAGES_REPO='shineyoureye-static-staging'
export RACK_ENV=development
fi
GH_PAGES_USER='theyworkforyou'
if [ x = x"$GITHUB_ACCESS_TOKEN" ]
then
GH_PAGES_GIT_URL="git@github.com:$GH_PAGES_USER/$GH_PAGES_REPO.git"
GH_PAGES_GIT_URL_REDACTED="$GH_PAGES_GIT_URL"
echo "GITHUB_ACCESS_TOKEN is not set, so using an scp-style Git SSH URL instead: $GH_PAGES_GIT_URL"
else
GH_PAGES_GIT_URL="https://${GITHUB_ACCESS_TOKEN}@github.com/$GH_PAGES_USER/$GH_PAGES_REPO.git"
GH_PAGES_GIT_URL_REDACTED="https://XXXXXXXXXXXXX@github.com/$GH_PAGES_USER/$GH_PAGES_REPO.git"
echo "GITHUB_ACCESS_TOKEN was set, so using an https URL containing that: $GH_PAGES_GIT_URL_REDACTED"
fi
fi
BUILD_DIR="build/$(date +'%Y-%m-%dT%H-%M-%S')"
mkdir -p "$BUILD_DIR"
ln -snf "${BUILD_DIR##*/}" build/current
SINATRA_PORT=9292
SINATRA_BASE_URL="http://localhost:$SINATRA_PORT"
server_pid_listening() {
lsof -n -iTCP:"$SINATRA_PORT" | egrep "\b${1}\b.*LISTEN" > /dev/null
}
start_sinatra() {
# Start the Sinatra server in the background:
bundle exec rackup > "$BUILD_DIR/sinatra.log" 2>&1 &
SINATRA_PID=$!
echo "Waiting for Sinatra to start..."
while kill -0 "$SINATRA_PID" 2> /dev/null && ! server_pid_listening "$SINATRA_PID"
do
sleep 1
done
# Make sure it's not an old one server still running:
kill -0 "$SINATRA_PID" 2> /dev/null ||
{
echo "Couldn't start Sinatra - is an old server running?"
echo "Server output was:"
cat "$BUILD_DIR/sinatra.log"
exit 1
}
echo "done"
}
stop_sinatra() {
echo "Killing Sinatra (pid $SINATRA_PID)"
kill "$SINATRA_PID"
echo "done"
}
crawl_site() {
echo "Crawling $SINATRA_BASE_URL...."
WGET_EXIT_CODE=0
(
cd "$BUILD_DIR" &&
wget -o wget.log -r -l 5 --domains='localhost' \
"$SINATRA_BASE_URL"/scraper-start-page.html
) || WGET_EXIT_CODE="$?"
echo done
}
deploy_site() {
(
cd "$BUILD_DIR"
echo "Cloning: $GH_PAGES_GIT_URL_REDACTED"
git clone --depth=1 "$GH_PAGES_GIT_URL" -b gh-pages
cd "$GH_PAGES_REPO"
git rm -rq .
# But keep the CNAME file!
git checkout HEAD -- CNAME
cp -R ../"${SINATRA_BASE_URL##*//}"/* .
git add .
# Annoyingly, files without an extension are served as
# application/octet-stream, so find any files that look like
# HTML (according to file(1)) and if they don't have a .html
# extension, rename them so that they do.
find . -name .git -prune -o -print | while read filename
do
if ! [ x"${filename: -5}" = x.html ]
then
if [ "$(file -b --mime-type "$filename")" = 'text/html' ]
then
git mv -f $filename "${filename}.html"
fi
fi
done
if [ -n "$(git status --porcelain)" ]
then
git -c "user.name=Democracy Sites Team" -c "user.email=parliaments@mysociety.org" commit -m "Automated commit of generated static site data
The shineyoureye-sinatra version was: $SYE_SINATRA_VERSION
The shineyoureye-prose version was: $PROSE_VERSION"
git push origin HEAD
else
echo "There were no changes to deploy!"
fi
)
}
bin/prepare-data
# Save the versions of the prose repository and this repository so
# that we can include them in the eventual commit message.
PROSE_VERSION="$(cd prose && git rev-parse HEAD)"
SYE_SINATRA_VERSION="$(git rev-parse HEAD)"
start_sinatra
crawl_site
stop_sinatra
if [ "$WGET_EXIT_CODE" = 0 ]
then
if [ "$ONLY_BUILD" != 1 ]
then
deploy_site
fi
else
echo "The errors were:"
egrep -B 3 ' ERROR [0-9]{3}' "$BUILD_DIR/wget.log"
exit $WGET_EXIT_CODE
fi