|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Original script: https://gist.github.com/imkarthikk/2fe9053f0aef275f5527 |
| 4 | +# Run it from the root of your Jekyll site like bash Jekyll-S3.sh |
| 5 | + |
| 6 | +# This script uses the AWS command line interface (CLI) tool to upload content |
| 7 | +# to S3. The okta-awscli tool can be set up to get temporary credentials for |
| 8 | +# use by the AWS CLI. |
| 9 | +# |
| 10 | +# For details about using okta-awscli with the AWS CLI, see also: |
| 11 | +# https://confluence.nmdp.org/display/BIO/AWS+CLI+access+with+Okta+%28with+MFA%29+-+BIO+and+GDR |
| 12 | +# |
| 13 | +# E.g. |
| 14 | +# brew install okta-awscli |
| 15 | +# brew install awscli |
| 16 | +# (define [bio] and [gdr] profiles in ~/.okta-aws) |
| 17 | +# okta-awscli --okta-profile bio --profile bio |
| 18 | +# aws --profile bio <awscli command> |
| 19 | + |
| 20 | +## |
| 21 | +# Configuration options |
| 22 | +## |
| 23 | +STAGING_BUCKET='s3://plstring-org-staging' |
| 24 | +LIVE_BUCKET='s3://plstring.org' |
| 25 | +SITE_DIR='_site/' |
| 26 | +AWS_S3_CMD='aws --profile bio s3' |
| 27 | + |
| 28 | +## |
| 29 | +# Usage |
| 30 | +## |
| 31 | +usage() { |
| 32 | +cat << _EOF_ |
| 33 | +Usage: ${0} [staging | live] |
| 34 | + |
| 35 | + staging Deploy to the staging bucket |
| 36 | + live Deploy to the live (www) bucket |
| 37 | +_EOF_ |
| 38 | +} |
| 39 | + |
| 40 | +## |
| 41 | +# Color stuff |
| 42 | +## |
| 43 | +NORMAL=$(tput sgr0) |
| 44 | +RED=$(tput setaf 1) |
| 45 | +GREEN=$(tput setaf 2; tput bold) |
| 46 | +YELLOW=$(tput setaf 3) |
| 47 | + |
| 48 | +function red() { |
| 49 | + echo "$RED$*$NORMAL" |
| 50 | +} |
| 51 | + |
| 52 | +function green() { |
| 53 | + echo "$GREEN$*$NORMAL" |
| 54 | +} |
| 55 | + |
| 56 | +function yellow() { |
| 57 | + echo "$YELLOW$*$NORMAL" |
| 58 | +} |
| 59 | + |
| 60 | +## |
| 61 | +# Actual script |
| 62 | +## |
| 63 | + |
| 64 | +# Expecting at least 1 parameter |
| 65 | +if [[ "$#" -ne "1" ]]; then |
| 66 | + echo "Expected 1 argument, got $#" >&2 |
| 67 | + usage |
| 68 | + exit 2 |
| 69 | +fi |
| 70 | + |
| 71 | +if [[ "$1" = "live" ]]; then |
| 72 | + BUCKET=$LIVE_BUCKET |
| 73 | + green 'Deploying to live bucket' |
| 74 | +else |
| 75 | + BUCKET=$STAGING_BUCKET |
| 76 | + green 'Deploying to staging bucket' |
| 77 | +fi |
| 78 | + |
| 79 | + |
| 80 | +yellow '--> Running Jekyll' |
| 81 | +bundle exec jekyll build \ |
| 82 | +|| { red '--> Jekyll build failed' ; exit 3 ; } |
| 83 | + |
| 84 | + |
| 85 | +yellow '--> Gzipping all html, css and js files' |
| 86 | +find $SITE_DIR \( -iname '*.html' -o -iname '*.css' -o -iname '*.js' \) -exec gzip -9 -n {} \; -exec mv {}.gz {} \; \ |
| 87 | +|| { red '--> Gzipping failed' ; exit 3 ; } |
| 88 | + |
| 89 | +# Sync css files (Cache: 4 hours) |
| 90 | +yellow '--> Uploading css files' |
| 91 | +${AWS_S3_CMD} sync --exclude '*.*' --include '*.css' --content-type 'text/css' --cache-control 'max-age=14400' --content-encoding 'gzip' $SITE_DIR $BUCKET \ |
| 92 | +|| { red '--> css upload failed' ; exit 4 ; } |
| 93 | + |
| 94 | +# Sync js files (Cache: 4 hours) |
| 95 | +yellow '--> Uploading js files' |
| 96 | +${AWS_S3_CMD} sync --exclude '*.*' --include '*.js' --content-type 'application/javascript' --cache-control 'max-age=14400' --content-encoding 'gzip' $SITE_DIR $BUCKET \ |
| 97 | +|| { red '--> js upload failed' ; exit 4 ; } |
| 98 | + |
| 99 | +# Sync media files (Cache: 4 hours) |
| 100 | +yellow '--> Uploading images (jpg, png, ico)' |
| 101 | +${AWS_S3_CMD} sync --exclude '*.*' --include '*.jpg' --content-type 'image/jpeg' --cache-control 'max-age=14400' $SITE_DIR $BUCKET \ |
| 102 | +|| { red '--> jpg upload failed' ; exit 4 ; } |
| 103 | +${AWS_S3_CMD} sync --exclude '*.*' --include '*.png' --content-type 'image/png' --cache-control 'max-age=14400' $SITE_DIR $BUCKET \ |
| 104 | +|| { red '--> png upload failed' ; exit 4 ; } |
| 105 | +${AWS_S3_CMD} sync --exclude '*.*' --include '*.ico' --content-type 'image/x-icon' --cache-control 'max-age=14400' $SITE_DIR $BUCKET \ |
| 106 | +|| { red '--> ico upload failed' ; exit 4 ; } |
| 107 | + |
| 108 | +# Sync html files (Cache: 2 hours) |
| 109 | +yellow '--> Uploading html files' |
| 110 | +${AWS_S3_CMD} sync --exclude '*.*' --include '*.html' --content-type 'text/html' --cache-control 'max-age=7200, must-revalidate' --content-encoding 'gzip' $SITE_DIR $BUCKET \ |
| 111 | +|| { red '--> html upload failed' ; exit 4 ; } |
| 112 | + |
| 113 | + |
| 114 | +# Sync everything else |
| 115 | +yellow '--> Syncing everything else' |
| 116 | +${AWS_S3_CMD} sync --delete $SITE_DIR $BUCKET \ |
| 117 | +|| { red '--> Sync failed' ; exit 4 ; } |
| 118 | + |
| 119 | +green "Done deploying to $1 bucket" |
0 commit comments