Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@dansteele dansteele commented Oct 25, 2017

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

@dankmitchell

This comment has been minimized.

Copy link

@dankmitchell dankmitchell commented Sep 27, 2018

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
You can’t perform that action at this time.