Last active
February 17, 2018 15:43
-
-
Save albertstill/b66110d8d3ffd60e9d86 to your computer and use it in GitHub Desktop.
Build a static site from a Rails app using Wget and AWS S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# precomile the static assets the HTML pages link to such as the .js, .css and .jpg files | |
RAILS_ENV=production bundle exec rake assets:precompile | |
# circleci has RAILS_ENV & RACK_ENV env variables set to test need to override. -d runs | |
# the server as a daemon. | |
RAILS_ENV=production RACK_ENV=production bundle exec rails s -d | |
# wait for server to load | |
sleep 10 | |
# regex means it only grabs the clean URL HTML pages e.g /contact-us. | |
# everything else eg .js or .jpg is in the public folder now we have done the precompile | |
wget --recursive --reject-regex '.*\..*' localhost:3000 | |
# misc pages that the crawl won't hit (--force-directories forces the creation of | |
# directory ./localhost:3000) | |
wget --force-directories \ | |
localhost:3000/unsupported-browser \ | |
localhost:3000/404 | |
# to kill the server | |
kill `cat ./tmp/pids/server.pid` | |
# gzip up all the HTML files. -9 flag means it will do it at the highest compression | |
# level. gzipping is important for Google Page Speed ranking | |
gzip --recursive -9 localhost:3000 | |
# remove .gz extension (% strips the .gz) | |
for f in `find localhost:3000 -iname '*.gz'`; do | |
mv $f ${f%.gz} | |
done | |
# delete everything and start with a clean bucket | |
aws s3 rm s3://staging.mybucket.com --recursive --region eu-west-1 | |
# sync the HTML pages. Note S3 can't infer content type because no extension | |
# on the clean URL's. | |
aws s3 sync ./localhost:3000 s3://staging.mybucket.com \ | |
--content-type text/html \ | |
--content-encoding gzip \ | |
--cache-control 'public, max-age=600' \ | |
--region eu-west-1 | |
# gzip up the text files from the precompile. namely the application .js and .css files. | |
# note image files are binary and don't benefit from being gziped. They should be compressed | |
# by other means for example Photoshop. | |
cd ./public/assets | |
for f in `find . -name "*.css" -o -name "*.js"`; do | |
gzip -9 $f | |
mv $f.gz $f | |
# cache control header can be at max age because asset pipeline uses cache busting | |
# fingerprinting in the file names | |
aws s3 cp $f s3://staging.mybucket.com/assets/ \ | |
--content-encoding gzip \ | |
--cache-control 'public, max-age=31536000' \ | |
--region eu-west-1 | |
done | |
cd .. | |
# sync the rest of the asset folder such as .jpg files | |
aws s3 sync ./assets s3://staging.mybucket.com/assets \ | |
--region eu-west-1 \ | |
--cache-control 'public, max-age=31536000' | |
# this file is an exception and lives in the /public folder in a Rails application. | |
# It can not be in the assets pipeline because it can't have a fingerprint file name | |
aws s3 cp robots.txt s3://staging.mybucket.com/ --region eu-west-1 | |
exit 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment