cached-bundle 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env bash
  2. # Usage: cached-bundle install --deployment
  3. #
  4. # After running `bundle`, caches the `vendor/bundle` directory to S3.
  5. # On the next run, restores the cached directory before running `bundle`.
  6. # When `Gemfile.lock` changes, the cache gets rebuilt.
  7. #
  8. # Requirements:
  9. # - Gemfile.lock
  10. # - TRAVIS_REPO_SLUG
  11. # - TRAVIS_RUBY_VERSION
  12. # - AMAZON_S3_BUCKET
  13. # - script/s3-put
  14. # - bundle
  15. # - curl
  16. #
  17. # Author: Mislav Marohnić
  18. set -e
  19. compute_md5() {
  20. local output="$(openssl md5)"
  21. echo "${output##* }"
  22. }
  23. download() {
  24. curl --tcp-nodelay -qsfL "$1" -o "$2"
  25. }
  26. bundle_path="vendor/bundle"
  27. gemfile_hash="$(compute_md5 <"${BUNDLE_GEMFILE:-Gemfile}.lock")"
  28. #cache_name="${TRAVIS_RUBY_VERSION}-${gemfile_hash}.tgz"
  29. cache_name="${TRAVIS_RUBY_VERSION}.tgz"
  30. fetch_url="http://${AMAZON_S3_BUCKET}.s3.amazonaws.com/${cache_name}"
  31. if download "$fetch_url" "$cache_name"; then
  32. echo "Reusing cached bundle ${cache_name}"
  33. tar xzf "$cache_name"
  34. fi
  35. bundle "$@"
  36. if [ ! -f "$cache_name" ]; then
  37. echo "Caching \`${bundle_path}' to S3"
  38. tar czf "$cache_name" "$bundle_path"
  39. script/s3-put "$cache_name" "${AMAZON_S3_BUCKET}:${cache_name}"
  40. fi