Skip to content

Commit 09b94a1

Browse files
authored
Merge pull request #940 from CodexRaunak/add-deployment-preview-gh-pages
Move off from netlify to gh pages for deployment previews
2 parents 43f5fb8 + 77bb2f9 commit 09b94a1

File tree

6 files changed

+132
-42
lines changed

6 files changed

+132
-42
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
name: Build and Preview Docs
2+
3+
on:
4+
pull_request_target:
5+
branches: [master]
6+
types: [opened, synchronize, reopened, closed]
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
concurrency:
13+
group: preview-${{ github.event.pull_request.number || github.run_id }}
14+
cancel-in-progress: true
15+
16+
defaults:
17+
run:
18+
shell: bash
19+
20+
jobs:
21+
build-and-deploy-preview:
22+
runs-on: ubuntu-24.04
23+
env:
24+
HUGO_VERSION: 0.157.0
25+
26+
steps:
27+
- name: Checkout PR code
28+
if: github.event.action != 'closed'
29+
uses: actions/checkout@v6
30+
with:
31+
repository: ${{ github.event.pull_request.head.repo.full_name }}
32+
ref: ${{ github.event.pull_request.head.sha }}
33+
persist-credentials: false
34+
submodules: recursive
35+
fetch-depth: 0
36+
37+
- name: Checkout for cleanup
38+
if: github.event.action == 'closed'
39+
uses: actions/checkout@v6
40+
with:
41+
ref: gh-pages
42+
fetch-depth: 0
43+
44+
- name: Install Hugo CLI
45+
if: github.event.action != 'closed'
46+
run: |
47+
wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
48+
&& sudo dpkg -i ${{ runner.temp }}/hugo.deb
49+
50+
- name: Install Dart Sass
51+
if: github.event.action != 'closed'
52+
run: sudo snap install dart-sass
53+
54+
- name: Setup Node
55+
if: github.event.action != 'closed'
56+
uses: actions/setup-node@v4
57+
with:
58+
node-version: "20"
59+
60+
- name: Install dependencies
61+
if: github.event.action != 'closed'
62+
run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
63+
64+
- name: Build PR preview
65+
if: github.event.action != 'closed'
66+
env:
67+
HUGO_ENVIRONMENT: production
68+
HUGO_ENV: production
69+
HUGO_PREVIEW: "true"
70+
run: |
71+
hugo \
72+
--gc \
73+
--minify \
74+
--baseURL "/"
75+
cat > public/robots.txt <<'EOF'
76+
User-agent: *
77+
Disallow: /
78+
EOF
79+
80+
- name: Deploy PR preview
81+
if: github.event.action != 'closed'
82+
uses: rossjrw/pr-preview-action@v1.6.3
83+
with:
84+
source-dir: ./public
85+
preview-branch: gh-pages
86+
umbrella-dir: pr-preview
87+
action: auto
88+
comment: false
89+
90+
- name: Comment PR with Preview URL
91+
if: github.event.action != 'closed'
92+
uses: marocchino/sticky-pull-request-comment@v2
93+
with:
94+
header: pr-preview
95+
message: |
96+
🚀 **Preview deployment for PR #${{ github.event.pull_request.number }}**
97+
98+
🌐 **Preview URL**: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-preview/pr-${{ github.event.pull_request.number }}/
99+
100+
_This preview will be updated automatically when you push new commits to this PR._
101+
- name: Cleanup PR preview on close
102+
if: github.event.action == 'closed'
103+
uses: rossjrw/pr-preview-action@v1.6.3
104+
with:
105+
preview-branch: gh-pages
106+
umbrella-dir: pr-preview
107+
action: remove

.github/workflows/hugo.yaml

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,20 @@ on:
1212
# Allows you to run this workflow manually from the Actions tab
1313
workflow_dispatch:
1414

15-
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
1615
permissions:
17-
contents: read
18-
pages: write
19-
id-token: write
16+
contents: write
2017

2118
concurrency:
22-
group: "pages-deployment"
23-
cancel-in-progress: true # Only latest deployment runs
19+
group: pages-deployment
20+
cancel-in-progress: true
2421

2522
# Default to bash
2623
defaults:
2724
run:
2825
shell: bash
2926

3027
jobs:
31-
# Build job
32-
build:
28+
deploy:
3329
runs-on: ubuntu-24.04
3430
env:
3531
HUGO_VERSION: 0.157.0
@@ -45,34 +41,27 @@ jobs:
4541
with:
4642
submodules: recursive
4743
fetch-depth: 0
48-
- name: Setup Pages
49-
id: pages
50-
uses: actions/configure-pages@v5
44+
- name: Setup Node
45+
uses: actions/setup-node@v4
46+
with:
47+
node-version: "20"
5148
- name: Install Node.js dependencies
5249
run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
5350
- name: Build with Hugo
5451
env:
55-
# For maximum backward compatibility with Hugo modules
5652
HUGO_ENVIRONMENT: production
5753
HUGO_ENV: production
5854
run: |
5955
hugo \
6056
--gc \
6157
--minify \
62-
--baseURL "${{ steps.pages.outputs.base_url }}/"
63-
- name: Upload artifact
64-
uses: actions/upload-pages-artifact@v3
65-
with:
66-
path: ./public
67-
68-
# Deployment job
69-
deploy:
70-
environment:
71-
name: github-pages
72-
url: ${{ steps.deployment.outputs.page_url }}
73-
runs-on: ubuntu-24.04
74-
needs: build
75-
steps:
58+
--baseURL "/"
59+
cp CNAME public/CNAME
60+
touch public/.nojekyll
7661
- name: Deploy to GitHub Pages
77-
id: deployment
78-
uses: actions/deploy-pages@v4
62+
uses: peaceiris/actions-gh-pages@v4
63+
with:
64+
github_token: ${{ secrets.GITHUB_TOKEN }}
65+
publish_dir: ./public
66+
publish_branch: gh-pages
67+
keep_files: true

hugo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
baseURL = "https://docs.layer5.io"
22
title = "Layer5 Documentation"
3+
relativeURLs = true
4+
canonifyURLs = false
35

46
# Language settings
57
contentDir = "content/en"

layouts/partials/head.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
{{ end -}}
66

77
{{ $outputFormat := partial "outputformat.html" . -}}
8-
{{ if and hugo.IsProduction (ne $outputFormat "print") -}}
8+
{{ if eq (getenv "HUGO_PREVIEW") "true" -}}
9+
<meta name="robots" content="noindex, nofollow">
10+
{{ else if and hugo.IsProduction (ne $outputFormat "print") -}}
911
<meta name="robots" content="index, follow">
1012
{{ else -}}
1113
<meta name="robots" content="noindex, nofollow">

layouts/robots.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
User-agent: *
2+
Allow: /
3+
Disallow: /pr-preview/

netlify.toml

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)