Skip to content

Instantly share code, notes, and snippets.

@dansteele
Last active September 15, 2022 23:45
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dansteele/7c083a1049dc462fcaddcd556b21d6ab to your computer and use it in GitHub Desktop.
Save dansteele/7c083a1049dc462fcaddcd556b21d6ab 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.
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
@dansteele
Copy link
Author

@t-harps check this out. It works nicely 👌

@dankmitchell
Copy link

Very good 👌

Might be worth stating the following

Add these into your gemfile:

gem 'dnsimple'
gem 'platform-api'

Add your DNS Account
DNSIMPLE_ACCOUNT_ID = ENV['DNSIMPLE_ACCOUNT_ID'].freeze

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment