-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.rb
112 lines (98 loc) · 3.15 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
require 'bundler'
require 'json'
require 'net/http'
class Deploy
def self.get_config
JSON.load(File.read('config.json'))
end
def initialize
@config = Deploy.get_config
end
def with_env
Bundler.with_unbundled_env do
Dir.chdir '/var/apps/qpixel' do
yield
end
end
end
def circle_status
begin
pipelines = Net::HTTP.get_response(URI('https://circleci.com/api/v2/pipeline?org-slug=gh/codidact'),
{ 'Circle-Token' => @config['api_token'] })
last_pipeline = JSON.load(pipelines.body)['items'].filter { |i| i['project_slug'] == 'gh/codidact/qpixel' }[0]
workflow = Net::HTTP.get_response(URI("https://circleci.com/api/v2/pipeline/#{last_pipeline['id']}/workflow"),
{ 'Circle-Token' => @config['api_token'] })
last_workflow = JSON.load(workflow.body)['items'][0]
jobs = Net::HTTP.get_response(URI("https://circleci.com/api/v2/workflow/#{last_workflow['id']}/job"),
{ 'Circle-Token' => @config['api_token'] })
all_jobs = JSON.load(jobs.body)['items']
all_jobs.filter { |job| !['rubocop', 'deploy'].include? job['name'] }
.all? { |job| job['status'] == 'success' }
rescue
nil
end
end
def git_pull
with_env do
`git pull origin develop > deploy_output.log 2>&1`
end
end
def bundle_install
with_env do
`/home/ubuntu/.rbenv/shims/bundle check || /home/ubuntu/.rbenv/shims/bundle install --without development test >> deploy_output.log 2>&1`
end
end
def run_migrations
with_env do
`RAILS_ENV=production /home/ubuntu/.rbenv/shims/bundle exec rails db:migrate >> deploy_output.log 2>&1`
end
end
def clear_cache
with_env do
`RAILS_ENV=production /home/ubuntu/.rbenv/shims/bundle exec rails r scripts/clear_cache.rb >> deploy_output.log 2>&1`
end
end
def create_seeds
with_env do
`RAILS_ENV=production /home/ubuntu/.rbenv/shims/bundle exec rails db:seed >> deploy_output.log 2>&1`
end
end
def precompile_assets
with_env do
`RAILS_ENV=production /home/ubuntu/.rbenv/shims/bundle exec rails assets:precompile >> deploy_output.log 2>&1`
end
end
def copy_statics
with_env do
`cp app/assets/images/* public/assets >> deploy_output.log 2>&1`
end
end
def update_crontab
with_env do
`/home/ubuntu/.rbenv/shims/bundle exec whenever --update-crontab >> deploy_output.log 2>&1`
end
end
def restart_server
with_env do
`/home/ubuntu/.rbenv/shims/bundle exec pumactl -P tmp/pids/server.pid restart >> deploy_output.log 2>&1`
end
end
def trigger
if circle_status
Thread.new do
git_pull
bundle_install
run_migrations
clear_cache
create_seeds
precompile_assets
copy_statics
update_crontab
restart_server
end
[true, 'Deploy started successfully.']
else
[false, 'CircleCI build failed - unable to deploy.']
end
end
end