Skip to content

[site] Optimize Hugo CI workflow with caching and updates #15154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 76 additions & 6 deletions .github/workflows/hugo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,62 @@ name: test hugo
on: [pull_request]

env:
NODE_VERSION: 16
NODE_VERSION: 22
HUGO_VERSION: 0.126.1

jobs:
lint:
outputs:
source_content_hash: ${{ steps.calc-hash.outputs.result }}
changed_files_key_part: ${{ steps.changed-files-key.outputs.key_part }}
debug_changed_files_list: ${{ steps.changed-files-key.outputs.debug_changed_files_list }}
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: true # Fetch Hugo themes (true OR recursive)
fetch-depth: 2 # Fetch enough history for HEAD~1

- name: Get changed files key
id: changed-files-key
run: |
# Get list of changed files, sort them, then hash the list.
git diff --name-only HEAD~1 HEAD | LC_ALL=C sort | md5sum | head -c 32 > changed_files_key_part.txt
echo "key_part=$(cat changed_files_key_part.txt)" >> $GITHUB_OUTPUT
ACTUAL_FILES=$(git diff --name-only HEAD~1 HEAD | LC_ALL=C sort | tr '\n' ' ' | sed 's/ $//')
echo "debug_changed_files_list=${ACTUAL_FILES}" >> $GITHUB_OUTPUT

- name: Calc hash
id: calc-hash
run: |
# Calculate md5sum for each file, sort the output (for consistent ordering),
# then calculate md5sum of that list of hashes. head -c 32 takes the md5 hash string.
find . -path ./.git -prune -o -type f -print0 | xargs -0 md5sum | LC_ALL=C sort -k 2 | md5sum | head -c 32 > hash.txt
hash_value=$(cat hash.txt)
echo "result=$hash_value" >> $GITHUB_OUTPUT

- name: Debug Cache Key
run: |
echo "calc-hash output: ${{ steps.calc-hash.outputs.result }}"
echo "changed-files key part: ${{ steps.changed-files-key.outputs.key_part }}"
echo "changed-files actual list: ${{ steps.changed-files-key.outputs.debug_changed_files_list }}"

- name: Upload Hugo source artifact
uses: actions/upload-artifact@v4
with:
# Cache only the specific directories and files needed for the Hugo build, and not capturing the full .git folder.
path: |
archetypes
assets
config
content
data
layouts
static
themes
name: hugo-source-files

- name: Lint Filenames
uses: julie-ng/lowercase-linter@v1
Expand All @@ -22,15 +68,35 @@ jobs:
path: "."
pr-comment: true
repo-token: ${{ secrets.GITHUB_TOKEN }}

test-linux:
needs: lint
name: Build hugo on Linux
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Download Hugo source artifact
uses: actions/download-artifact@v4
with:
submodules: true # Fetch Hugo themes (true OR recursive)
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
name: hugo-source-files
path: . # Download to the root of the workspace

- name: Debug Cache Key
run: |
echo "Using source_content_hash from lint job: ${{ needs.lint.outputs.source_content_hash }}"
echo "Using changed_files_key_part from lint job: ${{ needs.lint.outputs.changed_files_key_part }}"
echo "Actual changed files list from lint job: ${{ needs.lint.outputs.debug_changed_files_list }}"

- name: Cache Hugo build outputs
id: cache-hugo-build
uses: actions/cache@v4
with:
# Cache Hugo's generated 'public' directory and 'resources' directory
path: |
public
resources
key: ${{ runner.os }}-hugo-build-${{ needs.lint.outputs.source_content_hash }}-${{ needs.lint.outputs.changed_files_key_part }}
restore-keys: |
${{ runner.os }}-hugo-build-${{ needs.lint.outputs.source_content_hash }}-

- name: Setup Hugo
uses: peaceiris/actions-hugo@v3
Expand All @@ -39,7 +105,11 @@ jobs:
extended: true

- name: Build Hugo
run: hugo --environment=production --minify --templateMetrics --logLevel info
# If cache hit for Hugo build outputs, this step might still run but Hugo might determine nothing needs to be rebuilt.
# For a more explicit skip, you could check `steps.cache-hugo-build.outputs.cache-hit`
# if: steps.cache-hugo-build.outputs.cache-hit != 'true'
run: |
hugo --environment=production --minify --templateMetrics --logLevel info

# test-windows:
# needs: lint
Expand Down
Loading