This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
/
deployer.rb
240 lines (207 loc) · 5.93 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
module Kumade
class Deployer < Thor::Shell::Color
DEPLOY_BRANCH = "deploy"
attr_reader :environment, :pretending
def initialize(options = { })
super()
default = {
:environment => 'staging',
:pretending => false,
:cedar => false,
:tests => false
}
options = default.merge(options)
@environment = options[:environment]
@pretending = options[:pretending]
@branch = current_branch
@cedar = options[:cedar]
@tests = options[:tests]
end
def deploy
ensure_heroku_remote_exists
pre_deploy
sync_heroku
heroku_migrate
post_deploy
end
def pre_deploy
ensure_clean_git
if @tests
run_tests
end
package_assets
sync_github
end
def sync_github
run_or_error("git push origin #{@branch}",
"Failed to push #{@branch} -> origin")
success("Pushed #{@branch} -> origin")
end
def sync_heroku
unless branch_exist?(DEPLOY_BRANCH)
run_or_error("git branch deploy", "Failed to create #{DEPLOY_BRANCH}")
end
run_or_error("git push -f #{environment} #{DEPLOY_BRANCH}:master",
"Failed to force push #{DEPLOY_BRANCH} -> #{environment}/master")
success("Force pushed #{@branch} -> #{environment}")
end
def heroku_migrate
app = Kumade.app_for(environment)
heroku("rake db:migrate", app) unless pretending
success("Migrated #{app}")
end
def post_deploy
run_or_error(["git checkout #{@branch}", "git branch -D #{DEPLOY_BRANCH}"],
"Failed to clean up #{DEPLOY_BRANCH} branch")
end
def heroku(command, app)
heroku_command = if @cedar
"bundle exec heroku run"
else
"bundle exec heroku"
end
run_or_error("#{heroku_command} #{command} --app #{app}",
"Failed to run #{command} on Heroku")
end
def ensure_clean_git
if ! pretending && git_dirty?
error("Cannot deploy: repo is not clean.")
else
success("Git repo is clean")
end
end
def package_assets
invoke_task("kumade:before_asset_compilation")
package_with_jammit if jammit_installed?
package_with_more if more_installed?
end
def run_tests
%w(spec
test
features
cucumber
).each {|task| invoke_task(task)}
end
def package_with_jammit
begin
success_message = "Packaged assets with Jammit"
if 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 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_task(task)
if task_exist?(task)
success "Running #{task} task"
Rake::Task[task].invoke unless pretending
end
end
def git_add_and_commit_all_assets_in(dir)
run_or_error ["git checkout -b #{DEPLOY_BRANCH}", "git add -f #{dir}", "git commit -m 'Compiled assets'"],
"Cannot deploy: couldn't commit assets"
success "Added and committed all 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 task_exist?(task)
load("Rakefile") if File.exist?("Rakefile")
Rake::Task.task_defined?(task)
end
def git_dirty?
`git diff --exit-code`
!$?.success?
end
def run_or_error(commands, error_message)
all_commands = [commands].flatten.join(' && ')
if pretending
say_status(:run, all_commands)
else
error(error_message) unless run(all_commands)
end
end
def run(command, config = {})
say_status :run, command
config[:capture] ? `#{command}` : system("#{command}")
end
def branch_exist?(branch)
branches = `git branch`
regex = Regexp.new('[\\n\\s\\*]+' + Regexp.escape(branch.to_s) + '\\n')
result = ((branches =~ regex) ? true : false)
return result
end
def error(message)
say("==> ! #{message}", :red)
exit 1
end
def success(message)
say("==> #{message}", :green)
end
def ensure_heroku_remote_exists
if remote_exists?(environment)
if app_name = Kumade.app_for(environment)
success("#{environment} is a Heroku remote")
else
error(%{Cannot deploy: "#{environment}" remote does not point to Heroku})
end
else
error(%{Cannot deploy: "#{environment}" remote does not exist})
end
end
def remote_exists?(remote_name)
if pretending
true
else
`git remote` =~ /^#{remote_name}$/
end
end
def current_branch
`git symbolic-ref HEAD`.sub("refs/heads/", "").strip
end
end
end