forked from demarches-simplifiees/demarches-simplifiees.fr
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeploy.rb
149 lines (129 loc) · 4.17 KB
/
deploy.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
require 'mina/bundler'
require 'mina/git'
require 'mina/rails'
require 'mina/rbenv'
SHARED_WORKER_FILE_NAME = 'i_am_a_worker'
SHARED_WEBSERVER_FILE_NAME = 'i_am_a_webserver'
# Basic settings:
# domain - The hostname to SSH to.
# deploy_to - Path to deploy into.
# repository - Git repo to clone from. (needed by mina/git)
# branch - Branch name to deploy. (needed by mina/git)
#
# Advanced settings:
# forward_agent - SSH forward_agent
# user - Username in the server to SSH to
# shared_dirs - Manually create these paths in shared/ on your server.
# They will be linked in the 'deploy:link_shared_paths' step.
deploy_to = '/var/www/ds'
shared_dirs = [
'log',
'sockets',
'tmp/cache',
'tmp/pids',
'vendor/bundle'
]
set :domain, ENV.fetch('DOMAINS')
set :deploy_to, deploy_to
# rubocop:disable DS/ApplicationName
set :repository, 'https://github.com/betagouv/demarches-simplifiees.fr.git'
# rubocop:enable DS/ApplicationName
set :branch, ENV.fetch('BRANCH')
set :forward_agent, true
set :user, 'ds'
set :shared_dirs, shared_dirs
set :rbenv_path, "/home/ds/.rbenv/bin/rbenv"
puts "Deploy to #{ENV.fetch('DOMAINS')}, branch: #{ENV.fetch('BRANCH')}"
# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :setup do
shared_dirs.each do |dir|
command %[mkdir -p "#{deploy_to}/shared/#{dir}"]
command %[chmod g+rx,u+rwx "#{deploy_to}/shared/#{dir}"]
end
end
namespace :yarn do
desc "Install package dependencies using yarn."
task :install do
command %{
echo "-----> Installing package dependencies using yarn"
#{echo_cmd %[yarn install --non-interactive]}
}
end
end
namespace :after_party do
desc "Run after_party tasks."
task :run do
command %{
echo "-----> Running after_party"
#{echo_cmd %[bundle exec rake after_party:run]}
}
end
end
namespace :jobs_schedule do
desc "Run jobs_schedule tasks."
task :run do
command %{
echo "-----> Running jobs_schedule"
#{echo_cmd %[bundle exec rake jobs:schedule]}
}
end
end
namespace :service do
desc "Restart puma"
task :restart_puma do
webserver_file_path = File.join(deploy_to, 'shared', SHARED_WEBSERVER_FILE_NAME)
command %{
echo "-----> Restarting puma service"
#{echo_cmd %[test -f #{webserver_file_path} && sudo systemctl restart puma]}
}
end
desc "Reload nginx"
task :reload_nginx do
webserver_file_path = File.join(deploy_to, 'shared', SHARED_WEBSERVER_FILE_NAME)
command %{
echo "-----> Reloading nginx service"
#{echo_cmd %[test -f #{webserver_file_path} && sudo systemctl reload nginx]}
}
end
desc "Restart delayed_job"
task :restart_delayed_job do
worker_file_path = File.join(deploy_to, 'shared', SHARED_WORKER_FILE_NAME)
command %{
echo "-----> Restarting delayed_job service"
#{echo_cmd %[test -f #{worker_file_path} && echo 'it is a worker marchine, restarting delayed_job']}
#{echo_cmd %[test -f #{worker_file_path} && sudo systemctl restart delayed_job]}
#{echo_cmd %[test -f #{worker_file_path} || echo "it is not a worker marchine, #{worker_file_path} is absent"]}
}
end
end
desc "Deploys the current version to the server."
task :deploy do
command 'export PATH=$PATH:/home/ds/.rbenv/bin:/home/ds/.rbenv/shims'
command 'source /home/ds/.profile'
# increase db timeout to 5 minutes to allow long migration
command 'export PG_STATEMENT_TIMEOUT=300000'
deploy do
# Put things that will set up an empty directory into a fully set-up
# instance of your project.
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'yarn:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
on :launch do
invoke :'service:restart_puma'
invoke :'service:reload_nginx'
invoke :'service:restart_delayed_job'
invoke :'deploy:cleanup'
end
end
end
task :post_deploy do
command 'export PATH=$PATH:/home/ds/.rbenv/bin:/home/ds/.rbenv/shims'
command 'source /home/ds/.profile'
command 'cd /home/ds/current'
invoke :'after_party:run'
invoke :'jobs_schedule:run'
end