This repository has been archived by the owner on Oct 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.rb
executable file
·164 lines (129 loc) · 4.55 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
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
154
155
156
157
158
159
160
161
162
163
164
require "sinatra"
require "json"
require "fileutils"
require "git"
require 'uri'
require 'zip'
CONFIG = {
"GIT_WEBHOOK_REF" => "refs/heads/master",
"GIT_REPO_URI" => "https://#{ENV['GIT_USERNAME']}:#{ENV['GIT_ACCESS_TOKEN']}@github.com/auto-wcag/auto-wcag.git",
"GIT_REPO_NAME" => "auto-wcag",
"GIT_REPO_BRANCH_MASTER" => "master",
"GIT_REPO_BRANCH_GH_PAGES" => "gh-pages",
"DIR_TMP" => "tmp"
}
# HELPER Methods
def verify_signature(payload_body)
signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), ENV['GIT_WEBHOOK_SECRET'], payload_body)
return halt 500, "Signatures didn't match!" unless Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE'])
end
def clone_repo(gitUri, branchName, destDir)
puts "log: cloning: #{gitUri}, branch #{branchName}"
u = URI(gitUri)
project_name = u.path.split('/').last
directory_name = project_name.split('.').first + "-#{branchName}"
Dir.chdir(destDir)
unless File.directory?("./#{directory_name}")
system "git clone --branch #{branchName} #{gitUri} #{directory_name}"
system "git fetch"
system "git pull"
end
cloned_dir = destDir + "/#{directory_name}"
cloned_dir
end
def remove_dir(dir)
puts "log: remove dir #{dir}"
FileUtils.rm_rf Dir.glob("#{dir}")
end
def clean_dir(dir)
puts "log: clean dir #{dir}"
FileUtils.rm_rf Dir.glob("#{dir}/*")
end
def run_deployer_in_background()
background_pid = Process.fork do
# global git config
system "git config --global user.email '[email protected]' "
system "git config --global user.name 'jkodu' "
system "git config --global push.default matching"
# base dir
base_dir = __dir__
# Make tmp directory
puts Dir.pwd
system ("mkdir #{CONFIG['DIR_TMP']}")
# Clean the tmp directory
tmp_dir = __dir__ + "/" + CONFIG['DIR_TMP']
clean_dir(tmp_dir)
# Clone gh-pages branch
cloned_gh_pages_dir = clone_repo(CONFIG["GIT_REPO_URI"], CONFIG["GIT_REPO_BRANCH_GH_PAGES"], tmp_dir)
# Clone master branch
puts "log: cloing master branch"
cloned_master_dir = clone_repo(CONFIG["GIT_REPO_URI"], CONFIG["GIT_REPO_BRANCH_MASTER"], tmp_dir)
puts cloned_master_dir
Dir.chdir(cloned_master_dir)
# Get test embed files directory from package json if exists
embeds_dir = JSON.parse(File.read('package.json'))['testcases-embeds-dir']
puts "logging >> embeds directory from package json if exists:: #{embeds_dir}"
exports_dir = JSON.parse(File.read('package.json'))['testcases-export-dir']
puts "logging >> exports directory from package json if exists:: #{exports_dir}"
# Generating site from master branch
puts "log: generating static site"
system "gem install bundler"
system "bundle install"
system "bundle exec jekyll build"
# reset gh-pages to previous commit & update
Dir.chdir(cloned_gh_pages_dir)
# remove any old test embed files
system "git rm -rf #{embeds_dir}"
system "git rm -rf #{exports_dir}"
system "git add ."
system "git commit -m 'chore: deprecate old test assets and exports' "
# Copy generated site to gh-pages directory
puts "log: copying contents"
FileUtils.cp_r "#{cloned_master_dir}/_site/.", ".", :verbose => true
# Add and commit changes
system "git status"
system "git add ."
# Create a nojekyll file to prevent page build error
system "touch .nojekyll"
# Add and commit changes
system "git status"
system "git add ."
system "git commit -m 'chore: re-generated static site' "
system "git push" # May be look into not doing a forced update.
# Change working dir to root.
Dir.chdir(base_dir)
# Clean tmp dir
clean_dir(tmp_dir)
# # Remove tmp dir
remove_dir(tmp_dir)
# return
result = "Completed!!!"
puts result
Process.exit
end
background_pid
end
# API methods
get "/" do
returnValue = "Auto WCAG Deployer: Which listens to GitHub Webhook to rebuild gh-pages."
puts returnValue
status 200
body returnValue
end
post "/deploy" do
request.body.rewind
body = request.body.read
# verify_signature(body)
data = JSON.parse body
STDOUT.sync = true
returnValue = nil
if(data && data["ref"] && data["ref"] == CONFIG["GIT_WEBHOOK_REF"])
Process.detach run_deployer_in_background
returnValue = "log: webhook for master branch - executing in background thread - check https://dashboard.heroku.com/apps/secret-sea-89054/logs for updates."
else
returnValue = "Webhook triggered for non master branch. Ignoring re-build for gh-pages."
end
puts returnValue
status 200
body returnValue
end