Skip to content

Commit

Permalink
Add max_domain_length setting to control length of generated FQDN (#90)
Browse files Browse the repository at this point in the history
* Add max_domain_length setting to control length of generated FQDN

* Test with custom domain length
  • Loading branch information
crohr authored Sep 19, 2024
1 parent 7eecd65 commit d30cb3e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pullpreview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
instance_type: micro
# only required if using custom domain for your preview environments
dns: custom.pullpreview.com
max_domain_length: 30
# only required if fetching images from private registry
registries: docker://${{ secrets.GHCR_PAT }}@ghcr.io
# how long this instance will stay alive (each further commit will reset the timer)
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ inputs:
dns:
description: "Which DNS suffix to use"
default: "my.pullpreview.com"
max_domain_length:
description: "Maximum length of fully qualified domain name. Note that it cannot be greater than 62 characters due to LetsEncrypt restrictions."
default: "62"
label:
description: "Label to use for triggering preview deployments"
default: "pullpreview"
Expand Down Expand Up @@ -120,3 +123,4 @@ runs:
GITHUB_TOKEN: "${{ inputs.github_token }}"
PULLPREVIEW_LICENSE: "${{ inputs.license }}"
PULLPREVIEW_PROVIDER: "${{ inputs.provider }}"
PULLPREVIEW_MAX_DOMAIN_LENGTH: "${{ inputs.max_domain_length }}"
23 changes: 18 additions & 5 deletions lib/pull_preview/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

module PullPreview
class Instance
# https://community.letsencrypt.org/t/a-certificate-for-a-63-character-domain/78870/4
DEFAULT_MAX_DOMAIN_LENGTH = 62

include Utils

attr_reader :admins
Expand Down Expand Up @@ -79,14 +82,24 @@ def public_ip
access_details.ip_address
end

def max_domain_length
value = ENV.fetch("PULLPREVIEW_MAX_DOMAIN_LENGTH", DEFAULT_MAX_DOMAIN_LENGTH).to_i
value = DEFAULT_MAX_DOMAIN_LENGTH if value <= 0 || value > DEFAULT_MAX_DOMAIN_LENGTH
value
end

# Leave 8 chars for an additional subdomain that could be needed by the deployed app.
# Disabled if custom domain length is specified.
def reserved_space_for_user_subdomain
max_domain_length != DEFAULT_MAX_DOMAIN_LENGTH ? 0 : 8
end

def public_dns
reserved_space_for_user_subdomain = 8
# https://community.letsencrypt.org/t/a-certificate-for-a-63-character-domain/78870/4
remaining_chars_for_subdomain = 62 - reserved_space_for_user_subdomain - dns.size - public_ip.size - "ip".size - ("." * 3).size
remaining_chars_for_subdomain = max_domain_length - reserved_space_for_user_subdomain - dns.size - public_ip.size - "ip".size - ("." * 3).size
[
[subdomain[0..remaining_chars_for_subdomain], "ip", public_ip.gsub(".", "-")].join("-").squeeze("-"),
[subdomain[0..[(remaining_chars_for_subdomain - 1), 0].max], "ip", public_ip.gsub(".", "-")].join("-").squeeze("-"),
dns
].join(".")
].reject{|part| part.empty?}.join(".")
end

def url
Expand Down

0 comments on commit d30cb3e

Please sign in to comment.