github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/website/scripts/deploy.sh (about)

     1  #!/bin/bash
     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 change into our website
    32  # directory
    33  SOURCE="${BASH_SOURCE[0]}"
    34  while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
    35  DIR="$(cd -P "$( dirname "$SOURCE" )/.." && pwd)"
    36  
    37  # Delete any .DS_Store files for our OS X friends.
    38  find "$DIR" -type f -name '.DS_Store' -delete
    39  
    40  # Upload the files to S3 - we disable mime-type detection by the python library
    41  # and just guess from the file extension because it's surprisingly more
    42  # accurate, especially for CSS and javascript. We also tag the uploaded files
    43  # with the proper Surrogate-Key, which we will later purge in our API call to
    44  # Fastly.
    45  if [ -z "$NO_UPLOAD" ]; then
    46    echo "Uploading to S3..."
    47  
    48    # Check that the site has been built
    49    if [ ! -d "$DIR/build" ]; then
    50      echo "Missing compiled website! Run 'make build' to compile!"
    51      exit 1
    52    fi
    53  
    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=31536000" \
    62      --add-header="x-amz-meta-surrogate-key: site-$PROJECT" \
    63      sync "$DIR/build/" "s3://hc-sites/$PROJECT/latest/"
    64  
    65    # The s3cmd guessed mime type for text files is often wrong. This is
    66    # problematic for some assets, so force their mime types to be correct.
    67    echo "Overriding javascript mime-types..."
    68    s3cmd \
    69      --mime-type="application/javascript" \
    70      --exclude "*" \
    71      --include "*.js" \
    72      --recursive \
    73      modify "s3://hc-sites/$PROJECT/latest/"
    74  
    75    echo "Overriding css mime-types..."
    76    s3cmd \
    77      --mime-type="text/css" \
    78      --exclude "*" \
    79      --include "*.css" \
    80      --recursive \
    81      modify "s3://hc-sites/$PROJECT/latest/"
    82  
    83    echo "Overriding svg mime-types..."
    84    s3cmd \
    85      --mime-type="image/svg+xml" \
    86      --exclude "*" \
    87      --include "*.svg" \
    88      --recursive \
    89      modify "s3://hc-sites/$PROJECT/latest/"
    90  fi
    91  
    92  # Perform a soft-purge of the surrogate key.
    93  if [ -z "$NO_PURGE" ]; then
    94    echo "Purging Fastly cache..."
    95    curl \
    96      --fail \
    97      --silent \
    98      --output /dev/null \
    99      --request "POST" \
   100      --header "Accept: application/json" \
   101      --header "Fastly-Key: $FASTLY_API_KEY" \
   102      --header "Fastly-Soft-Purge: 1" \
   103      "https://api.fastly.com/service/$FASTLY_SERVICE_ID/purge/site-$PROJECT"
   104  fi
   105  
   106  # Warm the cache with recursive wget.
   107  if [ -z "$NO_WARM" ]; then
   108    echo "Warming Fastly cache..."
   109    wget \
   110      --recursive \
   111      --delete-after \
   112      --quiet \
   113      "https://$PROJECT_URL/"
   114  fi