Skip to content

Instantly share code, notes, and snippets.

@derred
Forked from thegrubbsian/deploy.rake
Created June 14, 2012 05:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derred/2928120 to your computer and use it in GitHub Desktop.
Save derred/2928120 to your computer and use it in GitHub Desktop.
Heroku Deploy Rake Task
namespace :deploy do
HEROKU_ACCOUNT = "account_name"
MAINLINE_BRANCH = "master"
STAGING_REPO = "app-name-staging"
PRODUCTION_REPO = "app-name-production"
def make_git_timestamp
"#{@env}-deploy-#{Time.now.to_s.gsub(/:/, "-").gsub(/\s/, "-").gsub(/--/, "-")}"
end
def system_try_and_fail(command)
sh command do |ok, res|
if !ok
system_try_and_fail "git co master"
raise "DEPLOY FAILED: Error running #{command} (Pwd: #{Dir.pwd}, exit status:#{res.exitstatus}). Aborting"
end
end
end
def check_master_branch
branches = (`git branch`).split
branches.each_with_index do |br, i|
if br == "*" && branches[i+1] != MAINLINE_BRANCH
raise "DEPLOY FAILED: You are not on #{MAINLINE_BRANCH}, you cannot deploy from other branches."
end
end
end
def check_pending_changes
status = `git status`
raise "DEPLOY FAILED: You cannot deploy with pending changes." if status !~ /nothing to commit/
end
def set_heroku_account
system_try_and_fail "heroku accounts:set #{HEROKU_ACCOUNT}"
puts "******** Heroku account set to #{HEROKU_ACCOUNT}"
end
def run_deploy
timestamp = make_git_timestamp
deploy_tag(timestamp) if @strategy == "tag"
deploy_branch(timestamp) if @strategy == "branch"
end
def deploy_tag(timestamp)
puts "******** Tagging with #{timestamp} for #{@env} deploy"
system_try_and_fail "git tag #{timestamp}"
system_try_and_fail "git push origin master --tags"
puts "******** Tag #{timestamp} pushed to origin"
system_try_and_fail "git push #{@repo} master"
puts "******** Tag #{timestamp} pushed to heroku: #{@repo}"
end
def deploy_branch(timestamp)
puts "******** Branching with #{timestamp} for #{@env} deploy"
system_try_and_fail "git co -b #{timestamp}"
system_try_and_fail "git push origin #{timestamp}"
puts "******** Branch #{timestamp} pushed to origin"
system_try_and_fail "git push #{@repo} #{timestamp}:master"
puts "******** Branch #{timestamp} pushed to heroku: #{@repo}"
system_try_and_fail "git co master"
end
def run_specs
puts "******** Running specs"
system_try_and_fail "bundle exec rspec spec/"
puts "******** All specs passing"
end
def deploy
check_master_branch
check_pending_changes
set_heroku_account
puts "******** Deploying to #{@env}"
run_specs
run_deploy
puts "******** Successful deployment to #{@env}"
end
task :staging do
@repo = STAGING_REPO
@strategy = "tag"
@env = "staging"
deploy
end
task :production do
@repo = PRODUCTION_REPO
@strategy = "branch"
@env = "production"
deploy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment