-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.rb
More file actions
135 lines (118 loc) · 3.79 KB
/
deploy.rb
File metadata and controls
135 lines (118 loc) · 3.79 KB
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
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 current_rev
with_env do
`git rev-parse HEAD`[0..7]
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 send_webhook(before, after)
url = "https://github.com/codidact/qpixel/compare/#{before}...#{after}"
webhook_url = @config['webhook_url']
params = { content: "<@&794974327543300126> deployed [#{before}...#{after}](#{url})" }
headers = { 'Content-Type': 'application/json' }
begin
response = Net::HTTP.post(URI(webhook_url), JSON.dump(params), headers)
response.is_a? Net::HTTPSuccess
rescue
false
end
end
def trigger
if circle_status
Thread.new do
before = current_rev
git_pull
bundle_install
run_migrations
clear_cache
create_seeds
precompile_assets
copy_statics
update_crontab
restart_server
after = current_rev
send_webhook before, after
end
[true, 'Deploy started successfully.']
else
[false, 'CircleCI build failed - unable to deploy.']
end
end
end