Skip to content

Instantly share code, notes, and snippets.

@imkarthikk
Created March 16, 2016 03:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imkarthikk/2fe9053f0aef275f5527 to your computer and use it in GitHub Desktop.
Save imkarthikk/2fe9053f0aef275f5527 to your computer and use it in GitHub Desktop.
# Run it from the root of your Jekyll site like bash Jekyll-S3.sh
##
# Configuration options
##
STAGING_BUCKET='s3://<YOUR-S3-BUCKET-NAME>'
LIVE_BUCKET='s3://<YOUR-S3-BUCKET-NAME>'
SITE_DIR='_site/'
##
# Usage
##
usage() {
cat << _EOF_
Usage: ${0} [staging | live]
staging Deploy to the staging bucket
live Deploy to the live (www) bucket
_EOF_
}
##
# Color stuff
##
NORMAL=$(tput sgr0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2; tput bold)
YELLOW=$(tput setaf 3)
function red() {
echo "$RED$*$NORMAL"
}
function green() {
echo "$GREEN$*$NORMAL"
}
function yellow() {
echo "$YELLOW$*$NORMAL"
}
##
# Actual script
##
# Expecting at least 1 parameter
if [[ "$#" -ne "1" ]]; then
echo "Expected 1 argument, got $#" >&2
usage
exit 2
fi
if [[ "$1" = "live" ]]; then
BUCKET=$LIVE_BUCKET
green 'Deploying to live bucket'
else
BUCKET=$STAGING_BUCKET
green 'Deploying to staging bucket'
fi
red '--> Running Jekyll'
jekyll build
red '--> Gzipping all html, css and js files'
find $SITE_DIR \( -iname '*.html' -o -iname '*.css' -o -iname '*.js' \) -exec gzip -9 -n {} \; -exec mv {}.gz {} \;
yellow '--> Uploading css files'
s3cmd sync --exclude '*.*' --include '*.css' --add-header='Content-Type: text/css' --add-header='Cache-Control: max-age=604800' --add-header='Content-Encoding: gzip' $SITE_DIR $BUCKET
yellow '--> Uploading js files'
s3cmd sync --exclude '*.*' --include '*.js' --add-header='Content-Type: application/javascript' --add-header='Cache-Control: max-age=604800' --add-header='Content-Encoding: gzip' $SITE_DIR $BUCKET
# Sync media files first (Cache: expire in 10weeks)
yellow '--> Uploading images (jpg, png, ico)'
s3cmd sync --exclude '*.*' --include '*.png' --include '*.jpg' --include '*.ico' --add-header='Expires: Sat, 20 Nov 2020 18:46:39 GMT' --add-header='Cache-Control: max-age=6048000' $SITE_DIR $BUCKET
# Sync html files (Cache: 2 hours)
yellow '--> Uploading html files'
s3cmd sync --exclude '*.*' --include '*.html' --add-header='Content-Type: text/html' --add-header='Cache-Control: max-age=7200, must-revalidate' --add-header='Content-Encoding: gzip' $SITE_DIR $BUCKET
# Sync everything else
yellow '--> Syncing everything else'
s3cmd sync --delete-removed $SITE_DIR $BUCKET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment