Forked from dansteele/create_review_app_subdomain.rake
Created
November 30, 2021 19:03
-
-
Save overture8/c20fe6e9c2e1e8e1d4e2d196bf815279 to your computer and use it in GitHub Desktop.
Use a sub-subdomain on Heroku review apps with DNSimple. Run this task from your app.json in your postdeploy script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :staging do | |
desc 'create subdomain DNS record for Heroku review app' | |
task :publish_dns do | |
require 'dnsimple' | |
require 'platform-api' | |
STAGING_DOMAIN = 'mystagingdomain.com'.freeze | |
DNSIMPLE_ACCOUNT_ID = .freeze | |
heroku_app_name = ENV['HEROKU_APP_NAME'] | |
subdomain = heroku_app_name.match(/.*(pr-\d+)/).captures.first | |
wildcard_subdomain = "*.#{subdomain}" | |
heroku_domain = "#{heroku_app_name}.herokuapp.com" | |
heroku_token = ENV['HEROKU_PLATFORM_TOKEN'] | |
type = { type: 'CNAME' } | |
standard_sub_opts = type.merge({ name: subdomain, content: heroku_domain }) | |
wildcard_sub_opts = type.merge({ name: wildcard_subdomain, content: heroku_domain }) | |
client = Dnsimple::Client.new access_token: ENV['YOUR_ACCESS_TOKEN'] | |
heroku = PlatformAPI.connect_oauth heroku_token | |
# Create records on DNSimple | |
[standard_sub_opts, wildcard_sub_opts].each do |opts| | |
client.zones.create_record DNSIMPLE_ACCOUNT_ID, STAGING_DOMAIN, opts | |
end | |
# Let Heroku know about records | |
[subdomain, wildcard_subdomain].each do |subdomain_type| | |
hostname = [subdomain_type, STAGING_DOMAIN].join('.') | |
heroku.domain.create(heroku_app_name, hostname: hostname) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment