Created — forked from thegrubbsian/deploy.rake

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Heroku Deploy Rake Task

View deploy.rake
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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
Something went wrong with that request. Please try again.