Skip to content

Instantly share code, notes, and snippets.

@codeablehq
Created May 7, 2014 17:33
Show Gist options
  • Save codeablehq/2a96797577853f30100a to your computer and use it in GitHub Desktop.
Save codeablehq/2a96797577853f30100a to your computer and use it in GitHub Desktop.
Speed up Travis builds by reusing gem bundle
#!/usr/bin/env bash
# Usage: script/cached-bundle install --deployment
#
# After running `bundle`, caches the `vendor/bundle` directory to S3.
# On the next run, restores the cached directory before running `bundle`.
# When `Gemfile.lock` changes, the cache gets rebuilt.
#
# Requirements:
# - Gemfile.lock
# - TRAVIS_REPO_SLUG
# - TRAVIS_RUBY_VERSION
# - AMAZON_S3_BUCKET
# - aws-cli
# - bundle
#
# Author: Mislav Marohnić/Tomaž Zaman
set -e
compute_md5() {
local output="$(openssl md5)"
echo "${output##* }"
}
download() {
echo "Downloading cached bundle $2"
aws s3 cp "$1" "$2"
}
bundle_path="vendor/bundle"
gemfile_hash="$(compute_md5 <"${BUNDLE_GEMFILE:-Gemfile}.lock")"
cache_name="${TRAVIS_RUBY_VERSION}-${gemfile_hash}.tgz"
remote_file="s3://${AMAZON_S3_BUCKET}/${TRAVIS_REPO_SLUG}/${cache_name}"
if download "$remote_file" "$cache_name"; then
echo "Reusing cached bundle ${cache_name}"
tar xzf "$cache_name"
fi
echo "Bundling: $@"
bundle "$@"
if [ ! -f "$cache_name" ]; then
echo "Caching \`${bundle_path}' to S3"
tar czf "$cache_name" "$bundle_path"
aws s3 cp $cache_name $remote_file
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment