Skip to content

Commit

Permalink
Purge Bunny.net CDN on release (#43)
Browse files Browse the repository at this point in the history
* Create release phase command

* Create rake task to purge cache

* Skip for review apps
  • Loading branch information
johlym authored Apr 2, 2022
1 parent 104f6d7 commit f4e7ec1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
web: bin/bridgetown start
web: bin/bridgetown start
release: rake cache:purge
29 changes: 29 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require "bridgetown"
require 'uri'
require 'net/http'
require 'openssl'
require 'dotenv/tasks'

Bridgetown.load_tasks

Expand Down Expand Up @@ -37,6 +41,31 @@ namespace :frontend do
end
end

namespace :cache do
desc "Purge Bunny.net CDN cache"
task :purge => :dotenv do
puts "Purging the CDN cache"

if ENV['REVIEW'] != 'true'
url = URI("https://api.bunny.net/pullzone/#{ENV['BUNNY_ZONE_ID']}/purgeCache")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["AccessKey"] = ENV['BUNNY_API_KEY']

response = http.request(request)
if response.code != "204"
raise "Unexpected response from CDN. Expected 204, got #{response.code}\n Response body: #{response.body}"
end
puts "Cache purged"
else
puts "Skipping. In review app."
end
end
end

task "assets:precompile" do
Rake::Task[:deploy].invoke
end
Expand Down

0 comments on commit f4e7ec1

Please sign in to comment.