Skip to content

Instantly share code, notes, and snippets.

@georgebrock
Created February 18, 2010 23:32
Show Gist options
  • Select an option

  • Save georgebrock/308224 to your computer and use it in GitHub Desktop.

Select an option

Save georgebrock/308224 to your computer and use it in GitHub Desktop.

Revisions

  1. georgebrock created this gist Feb 18, 2010.
    53 changes: 53 additions & 0 deletions Rakefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    namespace :deploy do
    PRODUCTION_APP = 'myapp'
    STAGING_APP = 'myapp-staging'

    def run(*cmd)
    system(*cmd)
    raise "Command #{cmd.inspect} failed!" unless $?.success?
    end

    def confirm(message)
    print "\n#{message}\nAre you sure? [Yn] "
    raise 'Aborted' unless STDIN.gets.chomp == 'Y'
    end

    desc "Deploy to staging"
    task :staging do
    confirm('This will deploy your current HEAD to staging.')
    run "git push [email protected]:#{STAGING_APP}.git HEAD:master -f"
    run "heroku rake db:migrate --app #{STAGING_APP}"
    end

    desc "Deploy to production"
    task :production do
    iso_date = Time.now.strftime('%Y-%m-%dT%H%M%S')

    confirm('This will deploy the production branch to production.')

    puts "Backing up…"
    Dir.chdir(File.join(File.dirname(__FILE__), *%w[.. .. backups])) do
    run "heroku bundles:destroy deploybackup --app #{PRODUCTION_APP}" if `heroku bundles --app #{PRODUCTION_APP}` =~ /deploybackup/
    run "heroku bundles:capture deploybackup --app #{PRODUCTION_APP}"
    puts " … waiting for Heroku"
    while sleep(5) && `heroku bundles --app #{PRODUCTION_APP}` !~ /complete/
    puts " … still waiting"
    end
    puts " … downloading backup"
    run "heroku bundles:download deploybackup --app #{PRODUCTION_APP}"
    run "mv #{PRODUCTION_APP}{,_#{iso_date}}.tar.gz"
    end

    tag_name = "heroku-#{iso_date}"
    puts "Tagging as #{tag_name}…"
    run "git tag #{tag_name} production"

    puts "Pushing…"
    run "git push origin #{tag_name}"
    run "git push [email protected]:#{PRODUCTION_APP}.git #{tag_name}:master"

    puts "Migrating…"
    run "heroku rake db:migrate --app #{PRODUCTION_APP}"
    end

    end