Skip to content

Commit 606ad1b

Browse files
author
何志翔
committed
deploy: support overrideing base URL to use HTTPS
1 parent 8861571 commit 606ad1b

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

.github/actions/deploy-to-github-pages/action.yml

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ inputs:
2020
cloudflare-zone:
2121
description: The Cloudflare zone to purge.
2222
required: false
23+
external-https:
24+
description: Whether HTTPS is set up externally, e.g. on Cloudflare instead of GitHub.
25+
default: false
26+
required: false
2327
outputs:
2428
url:
2529
description: The URL to which the site was deployed
@@ -41,6 +45,15 @@ runs:
4145
id: pages
4246
uses: actions/configure-pages@v5
4347

48+
- name: get base URL
49+
shell: bash
50+
run: |
51+
base_url="${{ steps.pages.outputs.base_url }}"
52+
if [ "${{ inputs.external-https }}" = true ];
53+
base_url="$(echo "$base_url" | sed 's/^http:/https:/')"
54+
fi
55+
echo "base_url=$base_url" >>"$GITHUB_ENV"
56+
4457
- name: configure Hugo and Pagefind version
4558
shell: bash
4659
run: |
@@ -59,7 +72,7 @@ runs:
5972
env:
6073
HUGO_RELATIVEURLS: false
6174
shell: bash
62-
run: hugo config && hugo --baseURL "${{ steps.pages.outputs.base_url }}/"
75+
run: hugo config && hugo --baseURL "${{ env.base_url }}/"
6376

6477
- name: run Pagefind ${{ env.PAGEFIND_VERSION }} to build the search index
6578
shell: bash
@@ -110,7 +123,6 @@ runs:
110123
id: remap
111124
shell: bash
112125
run: |
113-
base_url='${{ steps.pages.outputs.base_url }}'
114126
echo "result=$(echo "$base_url" |
115127
sed 's|^\(.*\)\(/git-scm\.com\)$|(\1)?\2(.*)|') file://$PWD/public\$2" \
116128
>>$GITHUB_OUTPUT
@@ -126,8 +138,9 @@ runs:
126138
with:
127139
args: >-
128140
--offline
141+
${{ inputs.external-https && '--require-https' }}
129142
--fallback-extensions html
130-
--base '${{ steps.pages.outputs.base_url }}'
143+
--base '${{ env.base_url }}'
131144
--remap '${{ steps.remap.outputs.result }}'
132145
${{ steps.remap.outputs.remap-dotdot }}
133146
--exclude file:///path/to/repo.git/
@@ -195,20 +208,17 @@ runs:
195208
- name: Edit /etc/hosts to map git-scm.com to GitHub
196209
shell: bash
197210
# This side-steps the Cloudflare caches
198-
run: sudo sh -c 'echo "185.199.108.153 git-scm.com" >>/etc/hosts'
211+
run: |
212+
domain="$(echo "$base_url" | sed -E 's|^https?://([^/]+).*|\1|'
213+
sudo sh -c 'echo "185.199.108.153 '"$domain"'" >>/etc/hosts'
199214
- name: Run Playwright tests
200215
shell: bash
201216
id: playwright
202217
env:
203-
PLAYWRIGHT_TEST_URL: ${{ steps.pages.outputs.base_url }}
204-
run: |
218+
PLAYWRIGHT_TEST_URL: ${{ env.base_url }}
205219
# avoid test failures when HTTPS is enforced half-way through
206-
case "$PLAYWRIGHT_TEST_URL" in
207-
https://*|http://git-scm.com) ;; # okay, leave as-is
208-
http://*) PLAYWRIGHT_TEST_URL="https://${PLAYWRIGHT_TEST_URL#http://}";;
209-
esac &&
210-
echo "result=$PLAYWRIGHT_TEST_URL" >>$GITHUB_OUTPUT &&
211-
npx playwright test --project=chrome
220+
PLAYWRIGHT_INSECURE_HTTPS: ${{ inputs.external-https }}
221+
run: npx playwright test --project=chrome
212222
- uses: actions/upload-artifact@v4
213223
if: always() && steps.playwright.outputs.result != ''
214224
with:

.github/workflows/deploy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ jobs:
2727
with:
2828
cloudflare-token: ${{ secrets.CLOUDFLARE_TOKEN }}
2929
cloudflare-zone: ${{ secrets.CLOUDFLARE_ZONE }}
30+
external-https: ${{ env.EXTERNAL_HTTPS }}

0 commit comments

Comments
 (0)