Skip to content

Commit d30cb3e

Browse files
authored
Add max_domain_length setting to control length of generated FQDN (#90)
* Add max_domain_length setting to control length of generated FQDN * Test with custom domain length
1 parent 7eecd65 commit d30cb3e

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

.github/workflows/pullpreview.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
instance_type: micro
3333
# only required if using custom domain for your preview environments
3434
dns: custom.pullpreview.com
35+
max_domain_length: 30
3536
# only required if fetching images from private registry
3637
registries: docker://${{ secrets.GHCR_PAT }}@ghcr.io
3738
# how long this instance will stay alive (each further commit will reset the timer)

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ inputs:
1515
dns:
1616
description: "Which DNS suffix to use"
1717
default: "my.pullpreview.com"
18+
max_domain_length:
19+
description: "Maximum length of fully qualified domain name. Note that it cannot be greater than 62 characters due to LetsEncrypt restrictions."
20+
default: "62"
1821
label:
1922
description: "Label to use for triggering preview deployments"
2023
default: "pullpreview"
@@ -120,3 +123,4 @@ runs:
120123
GITHUB_TOKEN: "${{ inputs.github_token }}"
121124
PULLPREVIEW_LICENSE: "${{ inputs.license }}"
122125
PULLPREVIEW_PROVIDER: "${{ inputs.provider }}"
126+
PULLPREVIEW_MAX_DOMAIN_LENGTH: "${{ inputs.max_domain_length }}"

lib/pull_preview/instance.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
module PullPreview
55
class Instance
6+
# https://community.letsencrypt.org/t/a-certificate-for-a-63-character-domain/78870/4
7+
DEFAULT_MAX_DOMAIN_LENGTH = 62
8+
69
include Utils
710

811
attr_reader :admins
@@ -79,14 +82,24 @@ def public_ip
7982
access_details.ip_address
8083
end
8184

85+
def max_domain_length
86+
value = ENV.fetch("PULLPREVIEW_MAX_DOMAIN_LENGTH", DEFAULT_MAX_DOMAIN_LENGTH).to_i
87+
value = DEFAULT_MAX_DOMAIN_LENGTH if value <= 0 || value > DEFAULT_MAX_DOMAIN_LENGTH
88+
value
89+
end
90+
91+
# Leave 8 chars for an additional subdomain that could be needed by the deployed app.
92+
# Disabled if custom domain length is specified.
93+
def reserved_space_for_user_subdomain
94+
max_domain_length != DEFAULT_MAX_DOMAIN_LENGTH ? 0 : 8
95+
end
96+
8297
def public_dns
83-
reserved_space_for_user_subdomain = 8
84-
# https://community.letsencrypt.org/t/a-certificate-for-a-63-character-domain/78870/4
85-
remaining_chars_for_subdomain = 62 - reserved_space_for_user_subdomain - dns.size - public_ip.size - "ip".size - ("." * 3).size
98+
remaining_chars_for_subdomain = max_domain_length - reserved_space_for_user_subdomain - dns.size - public_ip.size - "ip".size - ("." * 3).size
8699
[
87-
[subdomain[0..remaining_chars_for_subdomain], "ip", public_ip.gsub(".", "-")].join("-").squeeze("-"),
100+
[subdomain[0..[(remaining_chars_for_subdomain - 1), 0].max], "ip", public_ip.gsub(".", "-")].join("-").squeeze("-"),
88101
dns
89-
].join(".")
102+
].reject{|part| part.empty?}.join(".")
90103
end
91104

92105
def url

0 commit comments

Comments
 (0)