github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/website/scripts/deploy.sh (about) 1 #!/bin/sh 2 set -e 3 4 PROJECT="terraform" 5 PROJECT_URL="www.terraform.io" 6 FASTLY_SERVICE_ID="7GrxRJP3PVBuqQbyxYQ0MV" 7 8 # Ensure the proper AWS environment variables are set 9 if [ -z "$AWS_ACCESS_KEY_ID" ]; then 10 echo "Missing AWS_ACCESS_KEY_ID!" 11 exit 1 12 fi 13 14 if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then 15 echo "Missing AWS_SECRET_ACCESS_KEY!" 16 exit 1 17 fi 18 19 # Ensure the proper Fastly keys are set 20 if [ -z "$FASTLY_API_KEY" ]; then 21 echo "Missing FASTLY_API_KEY!" 22 exit 1 23 fi 24 25 # Ensure we have s3cmd installed 26 if ! command -v "s3cmd" >/dev/null 2>&1; then 27 echo "Missing s3cmd!" 28 exit 1 29 fi 30 31 # Get the parent directory of where this script is and cd there 32 DIR="$(cd "$(dirname "$(readlink -f "$0")")/.." && pwd)" 33 34 # Delete any .DS_Store files for our OS X friends. 35 find "$DIR" -type f -name '.DS_Store' -delete 36 37 # Upload the files to S3 - we disable mime-type detection by the python library 38 # and just guess from the file extension because it's surprisingly more 39 # accurate, especially for CSS and javascript. We also tag the uploaded files 40 # with the proper Surrogate-Key, which we will later purge in our API call to 41 # Fastly. 42 if [ -z "$NO_UPLOAD" ]; then 43 echo "Uploading to S3..." 44 45 # Check that the site has been built 46 if [ ! -d "$DIR/build" ]; then 47 echo "Missing compiled website! Run 'make build' to compile!" 48 exit 1 49 fi 50 51 # Set browser-side cache-control to ~4h, but tell Fastly to cache for much 52 # longer. We manually purge the Fastly cache, so setting it to a year is more 53 # than fine. 54 s3cmd \ 55 --quiet \ 56 --delete-removed \ 57 --guess-mime-type \ 58 --no-mime-magic \ 59 --acl-public \ 60 --recursive \ 61 --add-header="Cache-Control: max-age=14400" \ 62 --add-header="x-amz-meta-surrogate-control: max-age=31536000, stale-white-revalidate=86400, stale-if-error=604800" \ 63 --add-header="x-amz-meta-surrogate-key: site-$PROJECT" \ 64 sync "$DIR/build/" "s3://hc-sites/$PROJECT/latest/" 65 66 # The s3cmd guessed mime type for text files is often wrong. This is 67 # problematic for some assets, so force their mime types to be correct. 68 echo "Overriding javascript mime-types..." 69 s3cmd \ 70 --mime-type="application/javascript" \ 71 --add-header="Cache-Control: max-age=31536000" \ 72 --exclude "*" \ 73 --include "*.js" \ 74 --recursive \ 75 modify "s3://hc-sites/$PROJECT/latest/" 76 77 echo "Overriding css mime-types..." 78 s3cmd \ 79 --mime-type="text/css" \ 80 --add-header="Cache-Control: max-age=31536000" \ 81 --exclude "*" \ 82 --include "*.css" \ 83 --recursive \ 84 modify "s3://hc-sites/$PROJECT/latest/" 85 86 echo "Overriding svg mime-types..." 87 s3cmd \ 88 --mime-type="image/svg+xml" \ 89 --add-header="Cache-Control: max-age=31536000" \ 90 --exclude "*" \ 91 --include "*.svg" \ 92 --recursive \ 93 modify "s3://hc-sites/$PROJECT/latest/" 94 fi 95 96 # Perform a purge of the surrogate key. 97 if [ -z "$NO_PURGE" ]; then 98 echo "Purging Fastly cache..." 99 curl \ 100 --fail \ 101 --silent \ 102 --output /dev/null \ 103 --request "POST" \ 104 --header "Accept: application/json" \ 105 --header "Fastly-Key: $FASTLY_API_KEY" \ 106 --header "Fastly-Soft-Purge: 1" \ 107 "https://api.fastly.com/service/$FASTLY_SERVICE_ID/purge/site-$PROJECT" 108 fi 109 110 # Warm the cache with recursive wget. 111 if [ -z "$NO_WARM" ]; then 112 echo "Warming Fastly cache..." 113 echo "" 114 echo "If this step fails, there are likely missing or broken assets or links" 115 echo "on the website. Run the following command manually on your laptop, and" 116 echo "search for \"ERROR\" in the output:" 117 echo "" 118 echo "wget --recursive --delete-after https://$PROJECT_URL/" 119 echo "" 120 wget \ 121 --recursive \ 122 --delete-after \ 123 --quiet \ 124 "https://$PROJECT_URL/" 125 fi