Skip to content

Instantly share code, notes, and snippets.

@j4rs
Last active March 10, 2020 17:29
Show Gist options
  • Save j4rs/cc8cb928c5885ee97b763504f2ede575 to your computer and use it in GitHub Desktop.
Save j4rs/cc8cb928c5885ee97b763504f2ede575 to your computer and use it in GitHub Desktop.
Script to setup custom domains for review apps in Get on Board.
task :review_app_setup do
GOB_DEV_DOMAIN = #<our-custom-domain-for-dev>
require "dnsimple"
require "platform-api"
heroku_app_name = ENV["HEROKU_APP_NAME"]
dnsimple_account_id = ENV["DNSIMPLE_DEV_ACCOUNT_ID"]
type = { type: 'CNAME' }
dnsimple_client = Dnsimple::Client.new(access_token: ENV["DNSIMPLE_ACCESS_TOKEN"])
heroku_client = PlatformAPI.connect_oauth(ENV["HEROKU_API_TOKEN"])
# set DEFAULT_HOST env var
heroku_client.config_var.update(
heroku_app_name,
{ DEFAULT_HOST: "#{heroku_app_name}.#{GOB_DEV_DOMAIN}"}
)
# enable ACM (https)
heroku_client.app.enable_acm(heroku_app_name)
# Configure the custom domains in Heroku making sure the list
# correspond with the tenants at `test/fixtures/tenants.yml`
%w(home cl pe mx ar co re).each do |tenant|
subdomain = if tenant === "home"
"#{heroku_app_name}"
else
"#{heroku_app_name}-#{tenant}"
end
hostname = [subdomain, GOB_DEV_DOMAIN].join('.')
# create the custom domain in Heroku
heroku_client.domain.create(heroku_app_name, hostname: hostname)
heroku_domain = heroku_client.domain.info(heroku_app_name, hostname)["cname"]
# Create the CNAME record in DNSimple
opts = type.merge({ name: subdomain, content: heroku_domain })
# Query DNSimple to check whether the record already exist.
resp = dnsimple_client.zones.zone_records(
dnsimple_account_id,
GOB_DEV_DOMAIN,
{ filter: { name_like: subdomain } }
)
# only create it if not found
dnsimple_client.zones.create_zone_record(
dnsimple_account_id,
GOB_DEV_DOMAIN,
opts
) if resp.data.empty?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment