jcanfield
3/18/2012 - 5:56 PM

Heroku Deployment Script

Heroku Deployment Script

#!/usr/bin/env ruby

# Usage
# ./deploy [staging|production] <options>
#
# Options
# -m or --maintenance - Put the application into maintenance mode
# -b or --branch - deploy from a branch other than "master"

if ['staging', 'production'].include? ARGV[0]
  remote = ARGV[0]
  maintenance = ARGV.include?('-m') || ARGV.include?('--maintenance')
  branch = "master"
  alt_branch = ARGV.index('-b') || ARGV.index('--branch')
  repo = '<YOUR MAIN REPO>'
  revision=`git rev-parse master`
  
  case remote
    when 'staging'
      app = '<STAGING APP NAME ON HEROKU>'
      hoptoad_env = 'review'
    when 'production'
      app = '<STAGING APP NAME ON HEROKU>'
      hoptoad_env = 'production'
  end
  
  config_vars = `heroku config --app #{app}`
  asset_host = config_vars.split(/\n/).select{|v| v =~ /ASSET_HOST/}.first
  
  if asset_host
    
    ENV["ASSET_BUCKET"] = asset_host.split("=>").last.split("/").last.strip
  
    puts "packaging assets into #{ENV["ASSET_BUCKET"]}...."
    `bundle exec jammit-s3`

    if maintenance
      puts 'putting app into maintenance mode'
      `heroku maintenance:on --app #{app}`
    end

    if alt_branch
      branch = ARGV[alt_branch.to_i + 1]
    end

    puts "pushing code from #{branch}..."

    `git push -f #{remote} master`
        
    puts "migrating database..."
    puts `heroku rake db:migrate --app #{app}`
    
    puts "seeding database..."
    puts `heroku rake db:seed --app #{app}`
    
    if maintenance
     puts 'bringin app back out of maintenance mode...'
     `heroku maintenance:off --app #{app}`
    end
    
    puts "notifying hoptoad (rake hoptoad:deploy TO=#{hoptoad_env} REVISION=#{revision} REPO=#{repo})"
    `bundle exec rake hoptoad:deploy TO=#{hoptoad_env} REVISION=#{revision} REPO=#{repo}`

    puts '.....done. Celebrate!'
    
  else
    puts "ERROR!: You must have an ASSET_HOST variable set on your deploy target"
  end
else
  puts "You must specify staging or production"
end