Last active
September 24, 2024 06:44
-
-
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.
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 |
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
@t-harps check this out. It works nicely 👌