Skip to content

Commit 4d027bf

Browse files
fix cname issue
Signed-off-by: saurabhraghuvanshii <saurabhsraghuvanshi@gmail.com>
1 parent 04d2839 commit 4d027bf

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

.github/workflows/build-and-deploy-site.yml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,19 @@ jobs:
5151
# - If PUBLIC_SITE_URL is set (custom domain), build at root (no repo prefix).
5252
# - Otherwise use GitHub Pages URL; project repos need pathPrefix=<repo>.
5353
if [[ -n "${PUBLIC_SITE_URL:-}" ]]; then
54-
export GATSBY_SITE_URL="${PUBLIC_SITE_URL%/}"
55-
unset PATH_PREFIX
56-
npm run build
54+
base="${PUBLIC_SITE_URL%/}"
55+
site_origin="$(printf '%s' "$base" | sed -E 's|(https?://[^/]+).*|\1|')"
56+
site_path="$(printf '%s' "$base" | sed -E 's|https?://[^/]+||' | sed 's|^/||;s|/$||')"
57+
site_host="$(printf '%s' "$site_origin" | sed -E 's|https?://||')"
58+
59+
export GATSBY_SITE_URL="$site_origin"
60+
if [[ -n "$site_path" ]]; then
61+
export PATH_PREFIX="$site_path"
62+
npm run build:gh-pages
63+
else
64+
unset PATH_PREFIX
65+
npm run build
66+
fi
5767
else
5868
if [[ "$repo_name" == "${owner}.github.io" ]]; then
5969
export GATSBY_SITE_URL="https://${owner}.github.io"
@@ -67,6 +77,11 @@ jobs:
6777
fi
6878
touch public/.nojekyll
6979
80+
# Preserve custom domain in GitHub Pages settings by committing CNAME.
81+
if [[ -n "${site_host:-}" ]]; then
82+
printf '%s\n' "$site_host" > public/CNAME
83+
fi
84+
7085
- name: Deploy to GitHub Pages
7186
uses: peaceiris/actions-gh-pages@v4
7287
with:

.github/workflows/build-and-preview-site.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,16 @@ jobs:
5454
base="${PREVIEW_SITE_URL:-${PUBLIC_SITE_URL:-}}"
5555
5656
if [[ -n "$base" ]]; then
57-
pages_origin="${base%/}"
58-
path_prefix="pr-preview/${pr}"
59-
preview_url="${pages_origin}/pr-preview/${pr}/"
57+
base="${base%/}"
58+
pages_origin="$(printf '%s' "$base" | sed -E 's|(https?://[^/]+).*|\1|')"
59+
base_path="$(printf '%s' "$base" | sed -E 's|https?://[^/]+||' | sed 's|^/||;s|/$||')"
60+
if [[ -n "$base_path" ]]; then
61+
path_prefix="${base_path}/pr-preview/${pr}"
62+
preview_url="${pages_origin}/${base_path}/pr-preview/${pr}/"
63+
else
64+
path_prefix="pr-preview/${pr}"
65+
preview_url="${pages_origin}/pr-preview/${pr}/"
66+
fi
6067
else
6168
pages_origin="https://${owner}.github.io"
6269
if [[ "$repo_name" == "${owner}.github.io" ]]; then

0 commit comments

Comments
 (0)