Skip to content

Commit

Permalink
Merge pull request #81 from RedSparr0w/gh-pages-non-base-dir
Browse files Browse the repository at this point in the history
Allow for serving GitHub pages from non root directory
rossjrw authored Jan 8, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 52ddf53 + 253e355 commit 85ebedd
Showing 7 changed files with 110 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/preview-example.yml
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ concurrency: preview-${{ github.ref }}
jobs:
deploy-preview:
runs-on: ubuntu-latest
# Only run if this PR is not from a fork
if: github.event.pull_request.head.repo.full_name == github.repository
steps:
- name: Checkout
uses: actions/checkout@v3
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Run tests
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Run tests
run: |
set -e
for testscript in test/test-*.sh; do
bash $testscript
done
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -220,6 +220,10 @@ the `with` parameter.

Default: `pr-preview`

- `pages-base-path`: Path that GitHub Pages is being served from, as configured in your repository settings. When generating the preview URL, this is removed from the beginning of the path.

Default: ` ` (repository root)

- `custom-url`: Base URL to use when providing a link to the preview site.

Default: Will attempt to calculate the repository's GitHub Pages URL
@@ -335,6 +339,7 @@ steps:
source-dir: build
preview-branch: main
umbrella-dir: docs/pr-preview
pages-base-path: docs
```

You should definitely limit this workflow to run only on changes to
20 changes: 17 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -25,6 +25,10 @@ inputs:
description: Name of the directory containing all previews.
required: false
default: pr-preview
pages-base-path:
description: Path that GitHub Pages is served from.
required: false
default: ""
source-dir:
description: Directory containing files to deploy.
required: false
@@ -76,6 +80,7 @@ runs:
env:
action: ${{ inputs.action }}
umbrella: ${{ inputs.umbrella-dir }}
pagesbase: ${{ inputs.pages-base-path }}
pr: ${{ github.event.number }}
actionref: ${{ github.action_ref }}
actionrepo: ${{ github.action_repository }}
@@ -84,7 +89,6 @@ runs:
token: ${{ inputs.token }}
run: |
echo "action=$action" >> $GITHUB_ENV
echo "targetdir=$umbrella/pr-$pr" >> $GITHUB_ENV
echo "pr=$pr" >> $GITHUB_ENV
repo_org=$(echo "$deployrepo" | cut -d "/" -f 1)
@@ -100,6 +104,16 @@ runs:
echo "pagesurl=$pagesurl" >> $GITHUB_ENV
targetdir="$umbrella/pr-$pr"
echo "targetdir=$targetdir" >> $GITHUB_ENV
pagespath=$("$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh" -b "$pagesbase" -o "$targetdir")
if [ -n "$pagesbase" ] && [ "$("$GITHUB_ACTION_PATH/lib/remove-prefix-path.sh" -b "" -o "$targetdir")" = "$pagespath" ]; then
echo "::warning title=pages-base-path doesn't match::pages-base-path directory ($pagesbase) does not contain umbrella-dir ($umbrella). pages-base-path has been ignored."
pagespath=$targetdir
fi
echo "pagespath=$pagespath" >> $GITHUB_ENV
echo "emptydir=$(mktemp -d)" >> $GITHUB_ENV
echo "datetime=$(date '+%Y-%m-%d %H:%M %Z')" >> $GITHUB_ENV
@@ -134,7 +148,7 @@ runs:

- name: Expose deployment URL
id: url
run: echo "url=https://${{ env.pagesurl }}/${{ env.targetdir }}/" >> $GITHUB_OUTPUT
run: echo "url=https://${{ env.pagesurl }}/${{ env.pagespath }}/" >> $GITHUB_OUTPUT
shell: bash

- name: Leave a comment after deployment
@@ -150,7 +164,7 @@ runs:
:---:
:rocket: Deployed preview to
https://${{ env.pagesurl }}/${{ env.targetdir }}/
https://${{ env.pagesurl }}/${{ env.pagespath }}/
on branch [`${{ inputs.preview-branch }}`](\
${{ github.server_url }}/${{ env.deployrepo }}\
12 changes: 6 additions & 6 deletions lib/find-current-git-tag.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

helpFunction() {
help() {
echo ""
echo "Usage: $0 -p github_repository -f git_ref"
echo -e "\t-p GitHub repository to clone, format: owner/repo"
@@ -10,21 +10,21 @@ helpFunction() {

while getopts "p:f:" opt; do
case "$opt" in
p) github_repository="$OPTARG" ;;
f) git_ref="$OPTARG" ;;
?) helpFunction ;;
p) github_repository="$OPTARG" ;;
f) git_ref="$OPTARG" ;;
?) help ;;
esac
done

if [ -z "$github_repository" ] || [ -z "$git_ref" ]; then
echo >&2 "some parameters are empty"
helpFunction
help
fi

echo >&2 "Determining Git tag for $github_repository/$git_ref"

echo >&2 "Cloning repository $github_repository at ref $git_ref"
git clone --bare "https://github.com/$github_repository" bare_pr_preview
git clone --bare "https://github.com/$github_repository" bare_pr_preview

cd bare_pr_preview || exit 1

31 changes: 31 additions & 0 deletions lib/remove-prefix-path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

set -e

help() {
echo ""
echo "Removes a base path from the start of another path."
echo "Usage: $0 -b base_path -o original_path"
echo -e "\t-b Base path to remove"
echo -e "\t-o Path to remove base path from; must start with base path"
exit 1
} >&2

while getopts "b:o:" opt; do
case "$opt" in
b) base_path="$OPTARG" ;;
o) original_path="$OPTARG" ;;
?) help ;;
esac
done

# Remove leading dotslash, leading/trailing slash; collapse multiple slashes
normalise_path() {
echo "$1" | sed -e 's|^\./||g' -e 's|^/||g' -e 's|/*$||g' -e 's|//*|/|g'
}

base_path=$(normalise_path "$base_path")
original_path=$(normalise_path "$original_path")

echo "${original_path#"$base_path"/}"
exit 0
35 changes: 35 additions & 0 deletions test/test-remove-prefix-path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

set -e

echo >&2 "$0: start"

testscript=$(dirname "$0")/../lib/remove-prefix-path.sh

assert() {
echo >&2 "$1" = "$2"
if [ "$1" != "$2" ]; then
echo >&2 "$0: fail"
exit 1
fi
}

assert "$($testscript -b "" -o "")" ""
assert "$($testscript -b "" -o a/b/c/d)" a/b/c/d
assert "$($testscript -b "/" -o a/b/c/d)" a/b/c/d
assert "$($testscript -b "/" -o /a/b/c/d)" a/b/c/d
assert "$($testscript -b "//" -o /a/b/c/d)" a/b/c/d
assert "$($testscript -b a/b -o a/b/c/d)" c/d
assert "$($testscript -b ./a/b/ -o a/b/c/d)" c/d
assert "$($testscript -b a/b -o ./a/b/c/d/)" c/d
assert "$($testscript -b ./a//b// -o ./a//b//c//d/)" c/d
assert "$($testscript -b .//a/b -o ./a/b/c/d)" c/d
assert "$($testscript -b /a/b -o a/b/c/d)" c/d
assert "$($testscript -b /a/b/ -o /a/b/c/d/)" c/d
assert "$($testscript -b a/b -o /a/b/c/d)" c/d
assert "$($testscript -b a/b -o c/d/a/b)" c/d/a/b

# If there is no match, replacement with nothing should return the same result
assert "$($testscript -b "e/f" -o a/b/c/d)" "$($testscript -b "" -o a/b/c/d)"

echo >&2 "$0: ok"

0 comments on commit 85ebedd

Please sign in to comment.