diff --git a/.github/workflows/pullpreview.yml b/.github/workflows/pullpreview.yml index bd2e163..2c4bbdb 100644 --- a/.github/workflows/pullpreview.yml +++ b/.github/workflows/pullpreview.yml @@ -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) diff --git a/action.yml b/action.yml index 8c80a5b..302a0f9 100644 --- a/action.yml +++ b/action.yml @@ -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" @@ -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 }}" diff --git a/lib/pull_preview/instance.rb b/lib/pull_preview/instance.rb index ee870a9..975bdce 100644 --- a/lib/pull_preview/instance.rb +++ b/lib/pull_preview/instance.rb @@ -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 @@ -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