forked from mrsone40/vets-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·219 lines (190 loc) · 5.77 KB
/
deploy.sh
File metadata and controls
executable file
·219 lines (190 loc) · 5.77 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash -e
# application-build.sh
# deploys the assets of a given tarball in s3 to s3 asset bucket
ME=$(basename "$0")
ASSET_DEST=""
DEST=""
SOURCE=""
VERBOSE="no"
WORKDIR="."
EXIT_OK=no
function usage {
echo "$ME: perform a sync'ed deploy of static assets using 'aws s3 sync'"
echo "Usage: $ME -s SOURCE -d DEST [-a ASSET_DEST] [-w WORKDIR] [-v]"
echo " -s : An S3 URL to the build tarball object"
echo " -d : An S3 URL to the website bucket to deploy to"
echo " -a : An S3 URL to the asset bucket to deploy to"
echo " -w : local fs path to work in, defaults to current dir"
echo " -v : Use verbose output"
}
function say {
if [ "$VERBOSE" == "yes" ] ; then
echo "$1"
fi
}
function say_err {
>&2 echo "$1"
}
function bail {
say_err "$1"
exit 2
}
while getopts ":a:d:s:vw:h" option ; do
case $option in
a) ASSET_DEST="$OPTARG" ;;
d) DEST="$OPTARG" ;;
s) SOURCE="$OPTARG" ;;
v) VERBOSE="yes" ;;
w) WORKDIR="$OPTARG" ;;
\?) bail "Invalid option: $OPTARG" ;;
h) usage ; exit 0 ;;
*) bail "Something went wrong with argument parsing, use the -h arg for help" ;;
esac
done
shift $((OPTIND - 1))
# Exit if source or destination is missing
if [ -z "$SOURCE" ] || [ -z "$DEST" ]; then
say_err "ERROR: Missing required source or destination"
usage
exit 1
fi
say "INFO: Starting up..."
say "INFO: -> Deploying $SOURCE to $DEST"
if [ -n "$ASSET_DEST" ] ; then
say "INFO: -> Will also sync assets to $ASSET_DEST"
fi
# Create working directory
dir=$(mktemp -d "$WORKDIR/$ME.XXXXXX")
say "INFO: Created $dir to work in"
# Exit when script is finished or aborted
function finish {
say "INFO: Exiting ..."
if [ -n "$OLD_PWD" ] ; then
cd "$OLD_PWD"
fi
if [ -d "$dir" ] ; then
say "INFO: Removing $dir"
rm -rf "$dir"
fi
if [ $EXIT_OK != "yes" ] ; then
say_err "ERROR: Script aborted early, deploy may be in an incosistent state!"
exit 1
fi
}
trap finish EXIT
OLD_PWD=$PWD
cd "$dir"
# Fetch source tarbell from s3 and determine decompression tool
say "INFO: Fetching source..."
aws s3 --only-show-errors cp "$SOURCE" .
file=$(basename "$SOURCE")
build_file_type=$(file "$file")
if echo "$build_file_type" | grep -q "gzip compressed data" ; then
# XXX add pigz to jenkins ami builds, and switch to using that for 10 second savings
say "INFO: Detected gzip compression, using gzip"
compress="--use-compress-program=gzip"
elif echo "$build_file_type" | grep -q "bzip2 compressed data" ; then
say "INFO: Detected bzip compressed data, using -j"
compress="-j"
fi
# Unzip source into build directory
say "INFO: Expanding source into build/"
mkdir build
tar -x $compress -C build -f "$(basename "$SOURCE")"
# Copy assets from build directory into assets directory, excluding HTML files
if [ ! -f build/BUILD.txt ] ; then
say_err "ERROR: BUILD.txt file missing from source tarball"
exit 1
fi
if [ -n "$ASSET_DEST" ] ; then
say "INFO: Prepping assets for $ASSET_DEST"
mkdir assets
rsync -a --exclude '*.asp' --exclude '*.html' build/ assets/
cd assets
# XXX: switch this to pigz for 15 seconds of savings
say "INFO: Compressing text assets"
find . \
\( \
-name '*.js' -o \
-name '*.css' -o \
-name '*.ico' -o \
-name '*.txt' -o \
-name '*.xml' -o \
-name '*.ttf' -o \
-name '*.svg' \
\) \
-exec gzip -n {} \; -exec mv {}.gz {} \;
# Sync text assets to s3
say "INFO: Syncing compressed assets to $ASSET_DEST"
aws s3 sync --only-show-errors \
--acl public-read \
--content-encoding gzip \
--cache-control "public, no-cache" \
--exclude '*' \
--include '*.js' \
--include '*.css' \
--include '*.ico' \
--include '*.txt' \
--include '*.xml' \
--include '*.ttf' \
--include '*.svg' \
--exclude generated/styleConsolidated.css \
--exclude generated/polyfills.entry.js \
--exclude generated/vendor.entry.js \
--exclude generated/proxy-rewrite.entry.js \
--exclude js/settings.js \
--exclude js/vendor/uswds.min.js \
. "$ASSET_DEST"
# XXX: This list should come from `vets-website` build
say "INFO: Re-sync subset with shorter cache control to $ASSET_DEST"
aws s3 sync --only-show-errors \
--acl public-read \
--content-encoding gzip \
--cache-control "public, no-cache" \
--exclude '*' \
--include generated/styleConsolidated.css \
--include generated/polyfills.entry.js \
--include generated/vendor.entry.js \
--include generated/proxy-rewrite.entry.js \
--include js/settings.js \
--include js/vendor/uswds.min.js \
. "$ASSET_DEST"
say "INFO: Syncing assets to $ASSET_DEST"
aws s3 sync --only-show-errors \
--acl public-read \
--cache-control "public, no-cache" \
. "$ASSET_DEST"
cd ..
fi
cd build
say "INFO: Syncing assets to $DEST"
aws s3 sync --only-show-errors \
--acl public-read \
--cache-control "public, no-cache" \
--exclude '*' \
--include '*.css' \
--include '*.js' \
--include '*.png' \
--include '*.jpg' \
--include '*.svg' \
--include '*.woff2' \
--include '*.ttf' \
. "$DEST"
say "INFO: Syncing all content to $DEST"
aws s3 sync --only-show-errors \
--acl public-read \
--cache-control "public, no-cache" \
--delete \
. "$DEST"
cd ..
if [ -n "$ASSET_DEST" ] ; then
cd assets
say "INFO: Cleanup sync for $ASSET_DEST"
aws s3 sync --only-show-errors \
--acl public-read \
--cache-control "public, no-cache" \
--delete \
. "$ASSET_DEST"
cd ..
fi
EXIT_OK=yes