-
Notifications
You must be signed in to change notification settings - Fork 5
/
Rakefile
67 lines (53 loc) · 1.68 KB
/
Rakefile
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
require 'net/http'
require 'uri'
require 'json'
task :default => :serve
desc 'Cleanup generated files'
task :clean do
sh 'rm -rf _site'
end
desc 'Build the site'
task build: [:clean] do
jekyll :build
end
desc 'Serve the site locally and watch for changes'
task serve: [:clean] do
jekyll 'serve --drafts --incremental --watch'
end
task :purge_cloudflare_html_files do
html_files = Dir.glob('**/*.html', base: '_site')
urls = html_files.map { |file| "https://hugotunius.se/#{file}" }
urls.push "https://hugotunius.se"
uri = URI(cloudflare_purge_cache_url(required_env_variable('CF_ZONE_ID')))
# Cloudflare's API only handles 30 entries at the time
urls.each_slice(30) do |slice|
payload = {
files: slice
}
(api_key, api_email) = required_env_variables(['CF_API_KEY', 'CF_API_EMAIL'])
headers = {
'Authorization': "Bearer #{api_key}",
'X-Auth-Email': api_email,
'Content-Type': 'application/json',
}
response = Net::HTTP.post uri, payload.to_json, headers
raise "Failed to purge cloudflare cache" if Net::HTTPResponse::CODE_TO_OBJ[response.code] != Net::HTTPOK
end
end
def jekyll(command)
with_bundler "jekyll #{command}"
end
def with_bundler(command)
sh "bundle exec #{command}"
end
def required_env_variable(name)
required_env_variables([name])[0]
end
def required_env_variables(names)
any_missing = names.any? { |name| ENV[name].nil? || ENV[name].empty? }
raise "The #{names.map { |name| "`#{name}`" }.join(', ')} environment variable(s) are required" if any_missing
names.map { |name| ENV[name] }
end
def cloudflare_purge_cache_url(zone_id)
"https://api.cloudflare.com/client/v4/zones/#{zone_id}/purge_cache"
end