Travis invalidate aws CloudFront cache

How to make aws CloudFront cache invalidation in travis CI.
NB: In previous articles You can always find how to set up deployment in travis ci to aws.
So, let say we need to deploy static files to aws s3 (example) let’s add “after_deploy” section to .travis.yml:

lang: ruby
#...
deploy:
  provider: s3
#...
after_deploy:
  - ./.travis/reset_cloudfront_cache.sh $SITE_KEY $SITE_KEY_SECRET

Note: that $SITE_KEY and $SITE_KEY_SECRET must be travis encrypted values (details). Also, this user (which key and secret we are going to use) must have CloudFront permissions, so do not forget to add him to the appropriate group in aws/IAM.

For the .travis.yml config that’s it, we just need to run reset_cloudfront_cache script from the after_deploy section (so it will be started after deploy is done).

Let’s take a look at reset_cloudfront_cache.sh:

###############################################################################
###  Resets CloudFront cache with boto/cfadmin utility
###  Run: ./this_script  
###############################################################################

#
# Travis specific part - run this script only for production
#

# If this is fork - just exit
if [[ -n "${TRAVIS_PULL_REQUEST}" && "${TRAVIS_PULL_REQUEST}" != "false"  ]]; then
  echo -e '\n============== deploy will not be started (from the fork) ==============\n'
  exit 0
fi

if [[ $TRAVIS_BRANCH == 'master' ]]; then
    echo -e "\nThis is master/production branch - let's reset the CloudFront cache\n"
else
    echo -e "\nReset of CloudFront cache will not be started for non-production branch - exit.\n"
    exit 0
fi

#
# Install boto
#
echo -e "\nInstalling boto...\n"
git clone git://github.com/boto/boto.git
cd boto
sudo python setup.py install
cd ../
rm -rf boto

#
# Set up credentials for boto
#
echo -e "\nSet up boto credentials...\n"
cat > ~/.boto < 
# XNXNXXNNNNXNXN - distribution configured for the Web - in aws amazon cloudfront distributions.
#
echo -e "\nCloudFront Invalidating...\n"
cfadmin invalidate XNXNXXNNNNXNXN /yoursite.com
cfadmin invalidate XNXNXXNNNNXNXN /.yoursite.com
cfadmin invalidate XNXNXXNNNNXNXN /index.html /static/main.css /static/main.js
echo -e "\nInvalidating is in progress...\n"
echo -e "\nYou can check the status on the 'Invalidations' tab here https://console.aws.amazon.com/cloudfront/home?region=your_region#distribution-settings:XNXNXXNNNNXNXN\n"

#
# Clean up
#
echo -e "\nRemove boto config file\n"
rm ~/.boto

The main idea of this script is to use boto/cfadmin utility to reset specific paths in the selected distribution (this is library based on CloudFront rest API).
So we need to install boto first, then create ~/.boto config file (with aws credentials) and just to reset\invalidate the CoudFront cache with the following command:

cfadmin invalidate <distribution> <path>

That will start the Invalidation process that may last up to 10-30 minutes.

Social Share Toolbar

Travis branch specific variable

How to set different values for variables in travis travis.yml based on the current branch?
Let’s take a look at the part of the travis config:

lang: ruby
env:
  global:
  - GET_ENV_VAR=./.travis/get_env_specific_value.sh
  #
  # Keys:
  #
  # SITE_DEV_KEY
  - secure: "..."
  # SITE_DEV_KEY_SECRET
  - secure: "..."
  # SITE_PROD_KEY
  - secure: "..."
  # SITE_PROD_KEY_SECRET
  - secure: "..."
  #
  # Branch specific variables:
  #
  - ENV=$($GET_ENV_VAR develop master)
  - SITE_KEY=$($GET_ENV_VAR $SITE_DEV_KEY $SITE_PROD_KEY)
  - SITE_KEY_SECRET=$($GET_ENV_VAR $SITE_DEV_KEY_SECRET $SITE_PROD_KEY_SECRET)
  - AWS_BUCKET=$($GET_ENV_VAR dev-bucket prod-bucket)
  - AWS_REGION=$($GET_ENV_VAR us-west-2 us-west-1)

The magic happens in this line

GET_ENV_VAR=./.travis/get_env_specific_value.sh

To be more specific the very magic happens in this script “./.travis/get_env_specific_value.sh”, let’s take a look at the source code of this bash script:

#!/bin/bash
 
# If this is fork - just exit
if [[ -n "${TRAVIS_PULL_REQUEST}" && "${TRAVIS_PULL_REQUEST}" != "false"  ]]; then
  echo -e '\n============== deploy will not be started (from the fork) ==============\n'
  exit 0
fi
 
# Choose necessary argument according to the current branch.
if [[ $TRAVIS_BRANCH == 'develop' ]]; then
    echo $1
elif [[ $TRAVIS_BRANCH == 'master' ]]; then
    echo $2
else
    exit 0
fi

The idea of this script is just to return 1-st or 2-nd argument depends on the current branch name.

Social Share Toolbar

Travis ci deploy static files to amazon s3

How to deploy static files to aws s3 bucket?
Here is the example of travis.yml config:

lang: ruby
env:
  global:
  # env variables should be defined here:
  - AWS_BUCKET=...
before_script:
  # workaround to fix rake not found
  - gem install rake
deploy:
  provider: s3
  access_key_id: $SITE_KEY
  secret_access_key: $SITE_KEY_SECRET
  bucket: $AWS_BUCKET
  region: $AWS_REGION
  skip_cleanup: true
  acl: public_read
  on:
    all_branches: true

To workaround problem with Rakefile not found you need to add this Rakefile:

task :default do
  puts "default rake task"
end
Social Share Toolbar