Skip to content

Instantly share code, notes, and snippets.

@dbalexandre
Forked from rafaelp/deploy.rake
Created March 22, 2012 03:41

Revisions

  1. @rafaelp rafaelp created this gist Mar 21, 2012.
    95 changes: 95 additions & 0 deletions deploy.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    # -*- encoding : utf-8 -*-
    namespace :heroku do
    namespace :deploy do
    PRODUCTION_APP = 'nomedoprojeto-production'
    STAGING_APP = 'nomedoprojeto-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 'Ok. Bye...' unless STDIN.gets.chomp.downcase == 'y'
    end

    desc "Deploy to staging"
    task :staging do
    # This constant is defined to avoid problemd of copying and pasting from one environment to another
    APP = STAGING_APP

    if ENV['SKIP_BACKUP'] != "true"
    puts "-----> Backing up database via Heroku…"
    run "heroku pgbackups:capture --expire --app #{APP}"
    end

    puts "-----> Pushing…"
    run "git push [email protected]:#{APP}.git HEAD:master -f"

    puts "-----> Migrating…"
    run "heroku run rake db:migrate --app #{APP}"

    puts "-----> Seeding…"
    run "heroku run rake db:seed --app #{APP}"

    puts "-----> Restarting…"
    run "heroku restart --app #{APP}"
    end

    desc "Deploy to production"
    task :production do
    # This constant is defined to avoid problemd of copying and pasting from one environment to another
    APP = PRODUCTION_APP

    confirm("Going deploy to production.")

    if ENV['SKIP_TESTS'] != "true"
    puts "-----> Running all specs…"
    Rake::Task['spec'].invoke
    end

    print "\nPut in maintenance mode? [Yn] "
    maintenance = (ENV['MAINTENANCE'] == "true" or (STDIN.gets.chomp.downcase == 'y'))

    if maintenance
    puts "-----> Setting Maintenance on…"
    run "heroku maintenance:on --app #{APP}"

    puts "-----> Restarting…"
    run "heroku restart --app #{APP}"

    puts "-----> Waiting 20 seconds to app come back (in maintenance mode)…"
    sleep(20)
    end

    if ENV['SKIP_BACKUP'] != "true"
    puts "-----> Backing up database via Heroku…"
    run "heroku pgbackups:capture --expire --app #{APP}"
    end

    iso_date = Time.now.strftime('%Y-%m-%dT%H%M%S')
    tag_name = "production-#{iso_date}"
    puts "-----> Tagging as #{tag_name}…"
    run "git tag #{tag_name} master"

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

    puts "-----> Migrating…"
    run "heroku run rake db:migrate --app #{APP}"

    puts "-----> Seeding…"
    run "heroku run rake db:seed --app #{APP}"

    if maintenance
    puts "Setting Maintenance off…"
    run "heroku maintenance:off --app #{APP}"
    end

    puts "-----> Restarting…"
    run "heroku restart --app #{APP}"
    end
    end
    end