This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
deployer.rb
168 lines (144 loc) · 4.24 KB
/
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
require "rake"
require 'cocaine'
module Kumade
class Deployer < Base
DEPLOY_BRANCH = "deploy"
attr_reader :git
def initialize
super()
@git = Git.new
@branch = @git.current_branch
end
def deploy
begin
ensure_heroku_remote_exists
pre_deploy
sync_heroku
heroku_migrate
rescue
ensure
post_deploy
end
end
def pre_deploy
ensure_clean_git
package_assets
sync_github
end
def sync_github
git.push(@branch)
end
def sync_heroku
git.create(DEPLOY_BRANCH)
git.push("#{DEPLOY_BRANCH}:master", Kumade.configuration.environment, true)
end
def heroku_migrate
heroku("rake db:migrate") unless Kumade.configuration.pretending?
success("Migrated #{Kumade.configuration.environment}")
end
def post_deploy
git.delete(DEPLOY_BRANCH, @branch)
end
def heroku(command)
heroku_command = if cedar?
"bundle exec heroku run"
else
"bundle exec heroku"
end
run_or_error("#{heroku_command} #{command} --remote #{Kumade.configuration.environment}",
"Failed to run #{command} on Heroku")
end
def cedar?
return @cedar unless @cedar.nil?
@cedar = Cocaine::CommandLine.new("bundle exec heroku stack --remote #{Kumade.configuration.environment}").run.split("\n").grep(/\*/).any? do |line|
line.include?("cedar")
end
end
def ensure_clean_git
git.ensure_clean_git
end
def package_assets
invoke_custom_task if custom_task?
package_with_jammit if jammit_installed?
package_with_more if more_installed?
end
def package_with_jammit
begin
success_message = "Packaged assets with Jammit"
if Kumade.configuration.pretending?
success(success_message)
else
Jammit.package!
success(success_message)
git_add_and_commit_all_assets_in(jammit_assets_path)
end
rescue => jammit_error
error("Error: #{jammit_error.class}: #{jammit_error.message}")
end
end
def package_with_more
success_message = "Packaged assets with More"
if Kumade.configuration.pretending?
success(success_message)
else
begin
run "bundle exec rake more:generate"
if git.dirty?
success(success_message)
git_add_and_commit_all_assets_in(more_assets_path)
end
rescue => more_error
error("Error: #{more_error.class}: #{more_error.message}")
end
end
end
def invoke_custom_task
success("Running kumade:before_asset_compilation task")
Rake::Task["kumade:before_asset_compilation"].invoke unless Kumade.configuration.pretending?
end
def git_add_and_commit_all_assets_in(dir)
git.add_and_commit_all_in(dir, DEPLOY_BRANCH, 'Compiled assets', "Added and committed all assets", "couldn't commit assets")
end
def jammit_assets_path
File.join(Jammit::PUBLIC_ROOT, Jammit.package_path)
end
def more_assets_path
File.join('public', ::Less::More.destination_path)
end
def jammit_installed?
@jammit_installed ||=
(defined?(Jammit) ||
begin
require 'jammit'
true
rescue LoadError
false
end)
end
def more_installed?
@more_installed ||=
(defined?(Less::More) ||
begin
require 'less/more'
true
rescue LoadError
false
end)
end
def custom_task?
load("Rakefile") if File.exist?("Rakefile")
Rake::Task.task_defined?("kumade:before_asset_compilation")
end
def ensure_heroku_remote_exists
if git.remote_exists?(Kumade.configuration.environment)
if git.heroku_remote?
success("#{Kumade.configuration.environment} is a Heroku remote")
else
error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not point to Heroku})
end
else
error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not exist})
end
end
end
end