Skip to content

Instantly share code, notes, and snippets.

@ArturT

ArturT/.travis.yml

Last active Jul 12, 2017
Embed
What would you like to do?
Speed up Travis CI builds by caching the bundle to ftp. Based on s3 example: https://gist.github.com/matiaskorhonen/5203327 See README.md
---
language: ruby
rvm:
- 2.0.0
bundler_args: --without development --path=~/.bundle
env:
global:
- BUNDLE_ARCHIVE="app-bundle-archive"
- RAILS_ENV=test
- secure: "A_VERY_LONG_SERIES_OF_CHARACTERS_HERE"
before_install:
- "echo 'gem: --no-ri --no-rdoc' > ~/.gemrc"
- ./script/travis/bundle_install.sh
before_script:
- cp config/database.yml.example config/database.yml
- mysql -e 'create database app_test;'
- bundle exec rake db:test:load
script:
- bundle exec rspec spec
after_script:
- ruby script/travis/bundle_cache.rb
notifications:
email: false

Travis CI

Speed up Travis CI builds by caching the bundle to ftp. Based on s3 example: https://gist.github.com/matiaskorhonen/5203327

Edit .travis.yml if you like.

In development run:

$ gem install travis

Log into Travis from inside your project respository directory:

$ travis login --auto

Encrypt your ftp credentials inside the double quotes. Remember to create sudomain ftp-app in your example.com domain where you'll be able to store archived bundle. CI_FTP_URL is a url which will be used to download archived bundle tgz file.

$ travis encrypt CI_FTP_URL="ftp-app.example.com" CI_FTP_HOST="ftp.example.com" CI_FTP_USER="YOUR_FTP_USER" CI_FTP_PASS="YOUR_FTP_PASSWORD"

Put secure token in env.global.secure in .travis.yml.

Notice: Last generated secure token must be in .travis.yml. All previous tokens will be invalid.

# encoding: UTF-8
# script/travis/bundle_cache.rb
require "digest"
require "net/ftp"
architecture = `uname -m`.strip
file_name = "#{ENV['BUNDLE_ARCHIVE']}-#{architecture}.tgz"
file_path = File.expand_path("~/#{file_name}")
lock_file = File.join(File.expand_path(ENV["TRAVIS_BUILD_DIR"]), "Gemfile.lock")
digest_filename = "#{file_name}.sha2"
digest_path = File.expand_path("~/#{digest_filename}")
old_digest = File.expand_path("~/remote_#{digest_filename}")
puts "Checking for changes"
bundle_digest = Digest::SHA2.file(lock_file).hexdigest
old_digest = File.exists?(old_digest) ? File.read(old_digest).delete("\n") : ""
if bundle_digest == old_digest
puts "=> There were no changes, doing nothing"
else
if old_digest == ""
puts "=> There was no existing digest, uploading a new version of the archive"
else
puts "=> There were changes, uploading a new version of the archive"
puts " => Old checksum: #{old_digest}"
puts " => New checksum: #{bundle_digest}"
end
puts "=> Preparing bundle archive"
`cd ~ && tar -cjf #{file_name} .bundle`
puts "=> Bundle archive completed"
puts "=> Create bundle digest file"
`cd ~ && echo '#{bundle_digest}' > #{digest_filename}`
puts "=> Bundle digest file created"
ftp = Net::FTP.new
ftp.passive = true
ftp.connect(ENV['CI_FTP_HOST'])
ftp.login(ENV['CI_FTP_USER'], ENV['CI_FTP_PASS'])
puts "=> Uploading the bundle"
ftp.putbinaryfile(file_path, file_name, 1024)
puts "=> Completing upload"
puts "=> Uploading the digest file"
ftp.putbinaryfile(digest_path, digest_filename)
puts "=> Completing upload"
ftp.close
end
puts "All done now."
exit 0
#!/bin/sh
# script/travis/bundle_install.sh
ARCHITECTURE=`uname -m`
FILE_NAME="$BUNDLE_ARCHIVE-$ARCHITECTURE.tgz"
cd ~
wget -O "remote_$FILE_NAME" "http://$CI_FTP_URL/$FILE_NAME" && tar -xf "remote_$FILE_NAME"
wget -O "remote_$FILE_NAME.sha2" "http://$CI_FTP_URL/$FILE_NAME.sha2"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.