Cache Hugo modules in Drone CI
Today I modified my theme to use Twemoji for emojis. To add the SVG files to the theme, I used a Hugo module mount in the config of my theme:
module:
imports:
- path: github.com/twitter/twemoji
mounts:
- source: assets/svg
target: static/twemoji/svg
The problem now is that Hugo needs to download more then 3000 SVG files from GitHub when there is no cache. And because my blog builds on Drone CI, each time in a new container, there’s no cache and the build time increased by about 10 seconds.
To reduce this time and cache downloaded Hugo modules, it’s first necessary to make your user on your self-hosted Drone installation an admin. When using Docker-Compose it’s as easy as adding an environment variable:
DRONE_USER_CREATE=username:jlelse,admin:true
Then restart Drone, go to the repository and enable the option “trusted”.
After that you can modify the .drone.yml
file in your repository. My current build configuration for my blog looks like this:
kind: pipeline
name: default
steps:
- name: build
image: quay.io/jlelse/hugo:0.72.0
commands:
- hugo version
- hugo --minify --gc --cleanDestinationDir --forceSyncStatic
volumes:
- name: hugomodulecache
path: /tmp/hugo_cache/modules
- name: publish
image: drillster/drone-rsync
// ... more steps for publishing the build result and sending notifications to various services
volumes:
- name: hugomodulecache
host:
path: /tmp/hugomodulecache
The important and new parts of this config are the volumes
sections. The top-level volumes
config (the one at the bottom) defines the host volume (that’s the reason why the repository needs to be “trusted”, this feature is only available for trusted repositories). The volumes
config in the build step mounts the host volume to the build container.
Now the Hugo module cache is saved between runs (on the same Drone agent) and instead of downloading all the Twemoji SVGs again, they are read from the cache. Another benefit: Also my theme itself is now cached and the build time reduced about 10 seconds again.
For non-trusted repositories, there’s also a caching plugin available to save the cache in a cloud storage.