This repository has been archived by the owner on May 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
travis_deployer.rb
executable file
·79 lines (65 loc) · 1.88 KB
/
travis_deployer.rb
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
# Deploy the application to Heroku on a successful build
# First, identify the deploy application based on branch
# Don't deploy pull requests
if ENV['TRAVIS_PULL_REQUEST'] == "true"
puts "Pull request builds are not deployed"
exit
end
# Map the branch to a heroku application
apps = {
'master' => 'edbookfest-test',
'stage' => 'edbookfest-stage',
'production' => 'edbookfest' }
deploy_app = apps[ENV['TRAVIS_BRANCH']]
if deploy_app.nil?
puts "Branch #{ENV['TRAVIS_BRANCH']} is not mapped to a heroku application in travis_deployer.rb - not deploying"
exit
end
puts "Deploying build #{ENV['TRAVIS_JOB_NUMBER']} - branch #{ENV['TRAVIS_BRANCH']} at #{ENV['TRAVIS_COMMIT']} to #{deploy_app}"
# Install the Heroku gem
if !system 'gem install heroku'
puts "Could not install heroku gem"
exit $?.exitstatus
end
# Add the remote git branch
if !system "git remote add heroku [email protected]:#{deploy_app}.git"
puts "Could not add git remote"
exit $?.exitstatus
end
# Ignore SSH key verification for Heroku
known_hosts = File.expand_path("~/.ssh/config")
File.open(known_hosts, "a") do |f|
f.puts <<-EOF
Host heroku.com
StrictHostKeyChecking no
CheckHostIP no
UserKnownHostsFile=/dev/null
EOF
end
# Clear any existing heroku keys for our instance
if !system "heroku keys:clear"
puts "Could not clear heroku keys"
exit $?.exitstatus
end
# Add new heroku key
if !system "yes | heroku keys:add"
puts "Could not add heroku keys"
exit $?.exitstatus
end
# Deploy
if !system "git push heroku #{ENV['TRAVIS_BRANCH']}:master"
puts "Could not push new version"
exit $?.exitstatus
end
# Run db:migrate
if !system "heroku run rake db:migrate"
result = $?.exitstatus
puts "Could not migrate database - attempting rollback"
system "heroku rollback"
exit result
end
# Restart the app
if !system "heroku restart"
puts "Could not restart application"
exit $?.exitstatus
end