Created
March 6, 2012 21:05
-
-
Save parrish/1988963 to your computer and use it in GitHub Desktop.
Bundle App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'aws-sdk' | |
is_clean = `git diff-index HEAD`.chomp.empty? | |
unless is_clean | |
puts 'You currently have uncommitted changes. Stash or commit them before bundling.' | |
exit | |
end | |
app_name = 'your_app' | |
bucket_name = 'your_bucket' | |
s3_key = 'your_key' | |
s3_secret_key = 'your_secret_key' | |
# hang onto the working branch | |
working_branch = `git rev-parse --abbrev-ref HEAD`.chomp | |
# switch to the deploy branch | |
`git checkout -b bundled_deploy` | |
root = Dir.pwd | |
`mkdir -p vendor/cache` | |
# find all gemspecs in vendor, build them, cache them, and add to the commit | |
Dir['vendor/gems/**/*.gemspec'].each do |gemspec| | |
dirname = File.dirname gemspec | |
specname = File.basename gemspec | |
name = specname.sub /\.gemspec$/, '' | |
Dir.chdir dirname | |
`gem build #{ specname }` | |
gemfile = Dir["#{ name }*.gem"][0] | |
Dir.chdir root | |
gempath = File.join(dirname, gemfile) | |
`mv #{ gempath } vendor/cache` | |
`git add -f vendor/cache/#{ gemfile }` | |
end | |
`bundle install --local --without test development` | |
# precompile assets | |
`rm -rf public/assets` | |
`rake assets:precompile` | |
# package the gems | |
`bundle package` | |
# commit the changes | |
`git add -f public/assets` | |
`git add -f vendor/cache` | |
`git commit -a -m "deploying"` | |
# export the app | |
`git archive -o #{ app_name }.tar HEAD` | |
`gzip #{ app_name }.tar` | |
# S3 setup | |
AWS.config access_key_id: s3_key, secret_access_key: s3_secret_key | |
s3 = AWS::S3.new | |
bucket = s3.buckets[bucket_name] | |
old_bundle = bucket.objects["#{ app_name }.tar.gz"] | |
# move the old bundle if it exists | |
if old_bundle.exists? | |
timestamp = old_bundle.last_modified.strftime('%y-%m-%d-%H-%M') | |
old_bundle.move_to "#{ app_name }-#{ timestamp }.tar.gz" | |
end | |
# upload the new bundle | |
print 'Uploading...' | |
new_bundle = bucket.objects["#{ app_name }.tar.gz"] | |
new_bundle.write file: "#{ app_name }.tar.gz" | |
puts '...done' | |
# cleanup | |
`rm -f #{ app_name }.tar.gz` | |
`rm -rf .bundle` | |
`rm -rf vendor/cache` | |
# checkout the working branch and remove the deploy branch | |
`git checkout #{ working_branch }` | |
`git branch -D bundled_deploy` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment