How to cache Hugo modules in a GitHub Action?

EDIT: I think I’ve found the answer to this, see the first reply to this topic.

Hello!

I’m using this GitHub Action: GitHub - peaceiris/actions-hugo: GitHub Actions for Hugo ⚡️ Setup Hugo quickly and build your site fast. Hugo extended, Hugo Modules, Linux (Ubuntu), macOS, and Windows are supported.
I’m running on Hugo v0.127.0
And I’m also using a Hugo module (Blowfish).

From the docs of this GitHub Action:

    path: /home/runner/.cache/hugo_cache    # <-- with hugo version v0.116.0 and above
    # path: /tmp/hugo_cache                 # <-- with hugo version < v0.116.0

But, I’ve checked both folders in my CI, and don’t find any of them.
I’ve even fired a ls -R /home/runner/
And a ls -R .

And I don’t find any “hugo_cache”

It’s weird, since I’m not using the --gc flag at the build of my Hugo website.
I did everything exactly as it is documented.

Also, this is quite annoying, because the download of the module is likely 78% of the duration of my CI/CD…

hugo: collected modules in 17347 ms

Also:

ls: cannot access ‘/home/runner/.cache/hugo_cache’: No such file or directory
ls: cannot access ‘/home/runner/.cache’: No such file or directory

And ls /tmp gives:

clr-debug-pipe-1618-22959-in
clr-debug-pipe-1618-22959-out
clr-debug-pipe-1635-23181-in
clr-debug-pipe-1635-23181-out
clr-debug-pipe-592-844-in
clr-debug-pipe-592-844-out
dotnet-diagnostic-1618-22959-socket
dotnet-diagnostic-1635-23181-socket
dotnet-diagnostic-592-844-socket
hugo_cache_runner
snap-private-tmp
systemd-private-400d24fb912f435db1794cbdf1e0ff7c-chrony.service-PXMl8E
systemd-private-400d24fb912f435db1794cbdf1e0ff7c-haveged.service-QZT8c2
systemd-private-400d24fb912f435db1794cbdf1e0ff7c-systemd-logind.service-E0KxuY
systemd-private-400d24fb912f435db1794cbdf1e0ff7c-systemd-resolved.service-ZNa6Mx
www-data-temp-aspnet-0

There is a hugo_cache_runner here, but it seems to be pointless in this context…

Any clue?

Oh, okay, finally found the right way to do it. (I think…)
Just defining the env variable and rely on it seems to be perfectly safe.

name: GitHub Pages

on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    env:
      HUGO_VERSION: 0.127.0
      HUGO_CACHEDIR: /tmp/hugo_cache
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v4
      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v3
        with:
          hugo-version: ${{ env.HUGO_VERSION }}

      - uses: actions/cache@v4
        with:
          path: ${{ env.HUGO_CACHEDIR }}
          key: ${{ runner.os }}-hugomod-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-hugomod-

      - name: Build
        run: hugo --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        if: github.ref == 'refs/heads/main'
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./public
1 Like

Good approach!

In my experience, the cachedir depends on the environment when using GitHub action.

  1. With actions/setup-go, the default cachedir is /home/runner/.cache/hugo_cache.
  2. Without actions/setup-go, the default cachedir is /tmp/hugo_cache_runner
1 Like

Oooohhhh, so hugo_cache_runner wasn’t pointless!

Thank you for confirming that my approach is the right one. I prefer to hard code where the cache is in my GitHub Action to avoid any unexpected behavior. :smiley: (I hate to “Guess” things.)

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.