Skip to content

Deploy Hugo site to with Pagefind to Pages #191

Deploy Hugo site to with Pagefind to Pages

Deploy Hugo site to with Pagefind to Pages #191

Workflow file for this run

# Sample workflow for building and deploying a Hugo site to GitHub Pages
name: Deploy Hugo site to with Pagefind to Pages
on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Runs daily at 00:00 UTC to publish future-dated content
schedule:
- cron: "0 0 * * *"
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: true
# Default to bash
defaults:
run:
shell: bash
jobs:
# Build job
build:
runs-on: ubuntu-latest
env:
HUGO_VERSION: 0.155.1
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: "20" # Use a recent Node.js version
- name: Install Pagefind CLI
run: npm install -g pagefind
- name: Install Hugo CLI
run: |
wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
&& sudo dpkg -i ${{ runner.temp }}/hugo.deb
- name: Install Dart Sass
run: sudo snap install dart-sass
- name: Setup Pages
id: pages
uses: actions/configure-pages@v5
- name: Install Node.js dependencies
run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
- name: Cache Hugo build artifacts
uses: actions/cache@v5
with:
path: /tmp/hugo_cache
key: ${{ runner.os }}-hugo-${{ hashFiles('hugo.toml') }}
restore-keys: |
${{ runner.os }}-hugo-
- name: Build with Hugo
env:
HUGO_CACHEDIR: /tmp/hugo_cache
HUGO_ENVIRONMENT: production
run: |
hugo \
--minify \
--baseURL "${{ steps.pages.outputs.base_url }}/"
- name: Run Pagefind Indexing
# Runs Pagefind on the 'public' directory generated by Hugo
run: npx pagefind --source "public"
- name: Remove originals superseded by WebP
# Deletes any PNG/JPG in public/ that has a .webp counterpart, so the
# deployed site only serves the optimized WebP version. Files without a
# WebP counterpart (e.g. favicons, apple-touch-icon) are left untouched.
run: |
find public/ -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | \
while IFS= read -r -d '' f; do
if [ -f "${f%.*}.webp" ]; then
rm "$f"
echo "Removed: $f"
fi
done
- name: Upload artifact
uses: actions/upload-pages-artifact@v4
with:
path: ./public
# Deployment job
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4