Skip to content

Commit 0fbf9e5

Browse files
authored
fix(ci): complete PR preview path offset and fork PR handling (#6667)
* fix(ci): add path offset for PR preview subdirectory baseURL When PR preview builds use a subdirectory baseURL like /docs-v2/pr-preview/pr-XXXX/, shortcodes that parse .RelPermalink to detect product context fail because the path has extra segments. This fix: - Adds config/pr-preview/params.yml with prPreviewPathOffset: 3 - Updates workflow to use -e pr-preview environment - Updates api-endpoint, influxdb/host, and children shortcodes to use the offset when indexing path segments - Adds nil-safety with default fallback for placeholder_host Normal builds are unaffected (offset defaults to 0). * fix(ci): add path offset to product-name and sidebar for PR previews Apply the same prPreviewPathOffset fix to product-name.html and sidebar.html that was applied in the initial PR #6665. These templates parse RelPermalink to detect product context, but when baseURL includes a subdirectory path (e.g., /docs-v2/pr-preview/pr-XXXX/), the path indices shift. This fix uses the configurable offset to skip extra path segments in PR preview builds. * fix(ci): skip PR preview for fork PRs and add notice comment Fork PRs cannot deploy to gh-pages because GITHUB_TOKEN has read-only access to the base repository. This is a GitHub security feature. Changes: - Add condition to skip preview job for fork PRs - Add fork-notice job to post helpful comment explaining limitation - Include local preview instructions for contributors
1 parent 8045b67 commit 0fbf9e5

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

.github/workflows/pr-preview.yml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,45 @@ jobs:
3535
echo "should-run=true" >> $GITHUB_OUTPUT
3636
fi
3737
38+
# Notify fork PRs that preview is not available
39+
fork-notice:
40+
needs: check-draft
41+
if: |
42+
needs.check-draft.outputs.should-run == 'true' &&
43+
github.event.action != 'closed' &&
44+
github.event.pull_request.head.repo.full_name != github.repository
45+
runs-on: ubuntu-latest
46+
steps:
47+
- name: Post fork notice comment
48+
uses: actions/github-script@v7
49+
with:
50+
script: |
51+
const { data: comments } = await github.rest.issues.listComments({
52+
owner: context.repo.owner,
53+
repo: context.repo.repo,
54+
issue_number: context.issue.number
55+
});
56+
57+
const marker = '<!-- pr-preview-fork-notice -->';
58+
const existing = comments.find(c => c.body.includes(marker));
59+
60+
if (!existing) {
61+
await github.rest.issues.createComment({
62+
owner: context.repo.owner,
63+
repo: context.repo.repo,
64+
issue_number: context.issue.number,
65+
body: `${marker}\n## 📝 PR Preview Not Available\n\nPR previews are not available for pull requests from forks due to GitHub Actions security restrictions.\n\nTo preview your changes locally, run:\n\`\`\`bash\nnpx hugo server\n\`\`\`\n\nOnce merged, your changes will be visible on the production site.`
66+
});
67+
}
68+
3869
# Build and deploy preview
3970
preview:
4071
needs: check-draft
72+
# Skip fork PRs - GITHUB_TOKEN doesn't have write access to push to gh-pages
4173
if: |
4274
needs.check-draft.outputs.should-run == 'true' &&
43-
github.event.action != 'closed'
75+
github.event.action != 'closed' &&
76+
github.event.pull_request.head.repo.full_name == github.repository
4477
runs-on: ubuntu-latest
4578
steps:
4679
- name: Checkout

layouts/partials/sidebar.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{{ $currentPage := . }}
22
{{ $productPathData := findRE "[^/]+.*?" .RelPermalink }}
3-
{{ $product := index $productPathData 0 }}
3+
{{/* Support deployments (such as CI tools) with subdirectory baseURL */}}
4+
{{ $pathOffset := .Site.Params.prPreviewPathOffset | default 0 }}
5+
{{ $product := index $productPathData (add $pathOffset 0) }}
46
{{ $productName := (index .Site.Data.products $product).name }}
5-
{{ $currentVersion := index $productPathData 1 }}
7+
{{ $currentVersion := index $productPathData (add $pathOffset 1) }}
68

79
<!-- Menu Key -->
810
{{ .Scratch.Set "menuKey" "menu"}}

layouts/shortcodes/product-name.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}}
2-
{{- $currentProduct := index $productPathData 1 -}}
2+
{{- /* Support deployments (such as CI tools) with subdirectory baseURL */ -}}
3+
{{- $pathOffset := .Site.Params.prPreviewPathOffset | default 0 -}}
4+
{{- $currentProduct := index $productPathData (add $pathOffset 1) -}}
35
{{- $length := .Get 0 | default "long" -}}
46
{{- $omit := .Get "omit" | default "" -}}
57
{{- $scratch := newScratch -}}

0 commit comments

Comments
 (0)