Using Husky to Build Hugo Site

I’ve been making some efforts to streamline the publishing process for this site. I’ve read about tools like Netlify and Lektor, but I wanted to keep things as close to a basic Hugo site as possible.

To that end, I wanted to use git hooks to run hugo whenever I ran git commit. That way to publish a new post I simply had to write it in markdown, save, commit and push. No remembering to run hugo. No remembering to add changes to the site. A plain and simple git workflow for publishing.

I have seen husky before and decided to give that a go for this project. It’s an npm package that allows you to define your git hooks in your package.json, which will make it easy for me to maintain the hooks I want to use.

To get started I first had to initialize my package.json file:

npm init

Then I installed husky:

npm install husky@next --save-dev

And finally update my package.json with the following scripts and husky config:

"scripts": {
  "build": "hugo",
  "dev": "hugo server"
},
"husky": {
  "hooks": {
    "pre-commit": "npm run build && git add docs/*"
  }
}

Now I can use npm to run the hugo server (not that that’s easier than hugo server) and build the site. The real value though is that any time I commit the build will automatically run and the resulting build will be added before the commit.

Aaron Crowder avatar
About Aaron Crowder
Husband, Father, Software Developer. In that order.
comments powered by Disqus