From 78c7a9858130ce07e2d8395d5b4a2bfb1a8f042d Mon Sep 17 00:00:00 2001 From: Jason Stirnaman Date: Mon, 22 Dec 2025 09:31:03 -0600 Subject: [PATCH 1/3] 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). --- .github/workflows/pr-preview.yml | 3 ++- config/pr-preview/params.yml | 8 ++++++++ layouts/shortcodes/api-endpoint.html | 9 ++++++--- layouts/shortcodes/children.html | 11 +++++++---- layouts/shortcodes/influxdb/host.html | 9 ++++++--- 5 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 config/pr-preview/params.yml diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 17516f7a4d..d0aba7ecc6 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -90,7 +90,8 @@ jobs: PREVIEW_BASE_URL: https://influxdata.github.io/docs-v2/pr-preview/pr-${{ github.event.number }}/ run: | START_TIME=$(date +%s) - npx hugo --minify --baseURL "$PREVIEW_BASE_URL" + # Use pr-preview environment for path offset config + npx hugo --minify --baseURL "$PREVIEW_BASE_URL" -e pr-preview END_TIME=$(date +%s) DURATION=$((END_TIME - START_TIME)) echo "build-time=${DURATION}s" >> $GITHUB_OUTPUT diff --git a/config/pr-preview/params.yml b/config/pr-preview/params.yml new file mode 100644 index 0000000000..8e97197245 --- /dev/null +++ b/config/pr-preview/params.yml @@ -0,0 +1,8 @@ +# PR Preview environment parameters +# Used when building with: npx hugo -e pr-preview + +# Number of path segments to skip when parsing RelPermalink for product detection +# For baseURL like https://influxdata.github.io/docs-v2/pr-preview/pr-6657/ +# The path has 3 extra segments: docs-v2, pr-preview, pr-XXXX +# Workaround to correctly detect products in preview builds for Hugo templates relying on relPermalink to set active product +prPreviewPathOffset: 3 diff --git a/layouts/shortcodes/api-endpoint.html b/layouts/shortcodes/api-endpoint.html index 1203a87a4a..bb48c4f786 100644 --- a/layouts/shortcodes/api-endpoint.html +++ b/layouts/shortcodes/api-endpoint.html @@ -1,13 +1,16 @@ {{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}} -{{- $currentVersion := index $productPathData 1 -}} +{{- /* Support deployments (such as CI tools) with subdirectory baseURL */ -}} +{{- $pathOffset := .Site.Params.prPreviewPathOffset | default 0 -}} +{{- $currentVersion := index $productPathData (add $pathOffset 1) -}} {{- $endpoint := .Get "endpoint" -}} {{- $isOSS := ne (len (findRE `^v[0-9]` $currentVersion)) 0 -}} {{- $parsedProductKey := cond $isOSS "oss" $currentVersion -}} {{- $productKey := .Get "influxdb_host" | default $parsedProductKey -}} {{- $productAliases := dict "oss" "influxdb" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}} {{- $productRef := index $productAliases $productKey -}} -{{- $productData := index .Site.Data.products $productRef -}} -{{- $placeholderHost := $productData.placeholder_host }} +{{- $productData := dict -}} +{{- with $productRef }}{{- $productData = index $.Site.Data.products . | default dict -}}{{- end -}} +{{- $placeholderHost := $productData.placeholder_host | default "localhost:8086" }} {{- $method := .Get "method" | upper -}} {{- $methodStyle := .Get "method" | lower -}} {{- $apiRef := .Get "api-ref" | default "" -}} diff --git a/layouts/shortcodes/children.html b/layouts/shortcodes/children.html index 1bea94f941..67d2921c33 100644 --- a/layouts/shortcodes/children.html +++ b/layouts/shortcodes/children.html @@ -42,14 +42,17 @@ {{ end }} {{ end }} {{ if .Params.list_code_example }} - {{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}} - {{- $currentVersion := index $productPathData 1 -}} + {{- $productPathData := findRE "[^/]+.*?" $.Page.RelPermalink -}} + {{- /* Support deployments (such as CI tools) with subdirectory baseURL */ -}} + {{- $pathOffset := $.Site.Params.prPreviewPathOffset | default 0 -}} + {{- $currentVersion := index $productPathData (add $pathOffset 1) -}} {{- $isOSS := ne (len (findRE `^v[0-9]` $currentVersion)) 0 -}} {{- $productKey := cond $isOSS "oss" $currentVersion -}} {{- $productAliases := dict "oss" "influxdb" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}} {{- $productRef := index $productAliases $productKey -}} - {{- $productData := index .Site.Data.products $productRef -}} - {{- $placeholderHost := $productData.placeholder_host }} + {{- $productData := dict -}} + {{- with $productRef }}{{- $productData = index $.Site.Data.products . | default dict -}}{{- end -}} + {{- $placeholderHost := $productData.placeholder_host | default "localhost:8086" }} {{ .Params.list_code_example | replaceRE `\{\{[<\%] influxdb/host [>%]\}\}` $placeholderHost | .RenderString }} {{ end }} diff --git a/layouts/shortcodes/influxdb/host.html b/layouts/shortcodes/influxdb/host.html index 0ed5f7b84c..61494e80b0 100644 --- a/layouts/shortcodes/influxdb/host.html +++ b/layouts/shortcodes/influxdb/host.html @@ -1,9 +1,12 @@ {{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}} -{{- $currentVersion := index $productPathData 1 -}} +{{- /* Support deployments (such as CI tools) with subdirectory baseURL */ -}} +{{- $pathOffset := .Site.Params.prPreviewPathOffset | default 0 -}} +{{- $currentVersion := index $productPathData (add $pathOffset 1) -}} {{- $isOSS := ne (len (findRE `^v[0-9]` $currentVersion)) 0 -}} {{- $parsedProductKey := cond $isOSS "oss" $currentVersion -}} {{- $productKey := .Get 0 | default $parsedProductKey -}} {{- $productAliases := dict "oss" "influxdb" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}} {{- $productRef := index $productAliases $productKey -}} -{{- $productData := index .Site.Data.products $productRef -}} -{{ $productData.placeholder_host }} \ No newline at end of file +{{- $productData := dict -}} +{{- with $productRef }}{{- $productData = index $.Site.Data.products . | default dict -}}{{- end -}} +{{ $productData.placeholder_host | default "localhost:8086" }} \ No newline at end of file From 21ece19be7027f59ea2c3e30023d7c400a772f48 Mon Sep 17 00:00:00 2001 From: Jason Stirnaman Date: Mon, 22 Dec 2025 12:54:47 -0600 Subject: [PATCH 2/3] 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. --- layouts/partials/sidebar.html | 6 ++++-- layouts/shortcodes/product-name.html | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/layouts/partials/sidebar.html b/layouts/partials/sidebar.html index 0dd150396b..85caa0d2f2 100644 --- a/layouts/partials/sidebar.html +++ b/layouts/partials/sidebar.html @@ -1,8 +1,10 @@ {{ $currentPage := . }} {{ $productPathData := findRE "[^/]+.*?" .RelPermalink }} -{{ $product := index $productPathData 0 }} +{{/* Support deployments (such as CI tools) with subdirectory baseURL */}} +{{ $pathOffset := .Site.Params.prPreviewPathOffset | default 0 }} +{{ $product := index $productPathData (add $pathOffset 0) }} {{ $productName := (index .Site.Data.products $product).name }} -{{ $currentVersion := index $productPathData 1 }} +{{ $currentVersion := index $productPathData (add $pathOffset 1) }} {{ .Scratch.Set "menuKey" "menu"}} diff --git a/layouts/shortcodes/product-name.html b/layouts/shortcodes/product-name.html index 355ea5c25e..d71e5bea6f 100644 --- a/layouts/shortcodes/product-name.html +++ b/layouts/shortcodes/product-name.html @@ -1,5 +1,7 @@ {{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}} -{{- $currentProduct := index $productPathData 1 -}} +{{- /* Support deployments (such as CI tools) with subdirectory baseURL */ -}} +{{- $pathOffset := .Site.Params.prPreviewPathOffset | default 0 -}} +{{- $currentProduct := index $productPathData (add $pathOffset 1) -}} {{- $length := .Get 0 | default "long" -}} {{- $omit := .Get "omit" | default "" -}} {{- $scratch := newScratch -}} From d66329dd559320e6493a2e7035fdee5f12017d28 Mon Sep 17 00:00:00 2001 From: Jason Stirnaman Date: Mon, 22 Dec 2025 13:53:55 -0600 Subject: [PATCH 3/3] 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 --- .github/workflows/pr-preview.yml | 35 +++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index d0aba7ecc6..0eb5d6b42e 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -35,12 +35,45 @@ jobs: echo "should-run=true" >> $GITHUB_OUTPUT fi + # Notify fork PRs that preview is not available + fork-notice: + needs: check-draft + if: | + needs.check-draft.outputs.should-run == 'true' && + github.event.action != 'closed' && + github.event.pull_request.head.repo.full_name != github.repository + runs-on: ubuntu-latest + steps: + - name: Post fork notice comment + uses: actions/github-script@v7 + with: + script: | + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number + }); + + const marker = ''; + const existing = comments.find(c => c.body.includes(marker)); + + if (!existing) { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + 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.` + }); + } + # Build and deploy preview preview: needs: check-draft + # Skip fork PRs - GITHUB_TOKEN doesn't have write access to push to gh-pages if: | needs.check-draft.outputs.should-run == 'true' && - github.event.action != 'closed' + github.event.action != 'closed' && + github.event.pull_request.head.repo.full_name == github.repository runs-on: ubuntu-latest steps: - name: Checkout