-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
47 lines (36 loc) · 1.1 KB
/
server.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
require 'sinatra'
require 'json'
def update_repo(branch, new_commit_id)
# Pull changes
system("git checkout -f #{branch}; git pull")
# Record commit id
system("echo '#{new_commit_id} | #{Time.now.to_s}'>> deploy_history")
end
post '/payload' do
push = JSON.parse(request.body.read)
new_commit_id = push["after"]
return if new_commit_id.nil?
branch = push["ref"].split('/').last
repo = push["repository"]["name"]
if branch != 'master'
return "IGNORE branch '#{branch}'. Received #{repo} : #{new_commit_id}"
end
EM.defer do
Dir.chdir("/var/lib/labwiki/labwiki") do
if repo =~ /plugin/
Dir.chdir("/var/lib/labwiki/labwiki/plugins/#{repo}") { update_repo(branch, new_commit_id) }
else
update_repo(branch, new_commit_id)
end
Bundler.with_clean_env do
# Bundle update
system("bundle update")
# Restart
system("bundle exec rake stop")
system("bundle exec rake start")
end
end
puts "Deployed #{repo} with #{new_commit_id} at #{Time.now.to_s}"
end
"Received #{repo} : #{new_commit_id}"
end