This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
/
deployer.rb
153 lines (128 loc) · 3.48 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
class Kumade
class Deployer
def load_tasks
load 'kumade/tasks/deploy.rake'
end
def pre_deploy
ensure_clean_git
ensure_rake_passes
package_assets
git_push('origin')
end
def deploy_to_staging
pre_deploy
git_force_push(Kumade.staging)
heroku_migrate(:staging)
end
def deploy_to_production
pre_deploy
git_force_push(Kumade.production)
heroku_migrate(:production)
end
def git_push(remote)
run_or_raise("git push #{remote} master",
"Failed to push master -> #{remote}")
announce "Pushed master -> #{remote}"
end
def git_force_push(remote)
run_or_raise("git push -f #{remote} master",
"Failed to force push master -> #{remote}")
announce "Force pushed master -> #{remote}"
end
def heroku_migrate(environment)
app = if environment == :staging
Kumade.staging_app
elsif environment == :production
Kumade.production_app
end
run("bundle exec heroku rake db:migrate --app #{app}")
end
def ensure_clean_git
if git_dirty?
raise "Cannot deploy: repo is not clean."
end
end
def ensure_rake_passes
if default_task_exists?
raise "Cannot deploy: tests did not pass" unless rake_succeeded?
end
end
def package_assets
package_with_jammit if jammit_installed?
package_with_more if more_installed?
end
def package_with_jammit
Jammit.package!
announce("Successfully packaged with Jammit")
if git_dirty?
git_add_and_commit_all_assets_in(absolute_assets_path)
end
end
def package_with_more
Rake::Task['more:generate'].invoke
if git_dirty?
announce("Successfully packaged with More")
git_add_and_commit_all_assets_in(more_assets_path)
end
end
def git_add_and_commit_all_assets_in(dir)
announce "Committing assets"
run_or_raise("git add #{dir} && git commit -m 'Assets'",
"Cannot deploy: couldn't commit assets")
end
def git_add_and_commit_all_more_assets
announce "Committing assets"
run_or_raise("git add #{more_assets_path} && git commit -m 'Assets'",
"Cannot deploy: couldn't commit assets")
end
def absolute_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'
rescue LoadError
false
end)
end
def more_installed?
@more_installed ||=
(defined?(Less::More) ||
begin
require 'less/more'
rescue LoadError
false
end)
end
def default_task_exists?
Rake::Task.task_defined?('default')
end
def rake_succeeded?
begin
Rake::Task[:default].invoke
rescue
false
end
end
def git_dirty?
git_changed = `git status --short 2> /dev/null | tail -n1`
dirty = git_changed.size > 0
end
def run(command)
announce "+ #{command}"
announce "- #{system command}"
$?.success?
end
def run_or_raise(command, error_message)
raise(error_message) unless run(command)
end
def announce(message)
puts message
end
end
end