How can I run a custom Go tool before Hugo build my site?

I have a monorepo on Github that looks something like this:

.
├── custom-go-tool
├──|--------------- main.go
├── archetypes
├── config.toml
├── netlify.toml
├── content
├── data
├── layouts
├── static
└── themes

At the moment my site is built by Netlify using the build command hugo --gc --minify and that part works fine.

What I need help with is how to run my custom-go-tool before Netlify builds by hugo site because my tool creates a JSON file under static/data.json that needs to be deployed for the site to work.

I have two questions I need help answering:

  1. How do I get Netlify to run my Go tool before Hugo build?
    a) Can Netlify compile a Go program and then run it?
    b) Do I need to use GitHub Actions to build the Go program and then make it available to Netlify during the build?

  2. Once my Go program runs on Netlify and the JSON file gets generated pre-Hugo build, I’m wondering if it’s necessary to commit the JSON file to the GitHub repo to have a version history. Can this be done?

I’ve googled and thought about this but I could really use some advice on the best approach :slight_smile:

Btw I thought about running the Go program locally before pushing to Github and then Netlify but that introduces the potential for human error which I would like to avoid by always having the JSON generated during building.

Hey @almostravioli

You can chain multiple commands together in the form

./custom-script && hugo --gc --minify

or if you need to specify go (or other program e.g. node)

go custom-script && hugo --gc --minify

In this case, if your custom-script doesn’t exit properly, the second command would not run.

Thanks for your quick reply! Am I right that your command assumes that my Go tool is compiled and the binary is stored in the same git repo as the Hugo site?

As mentioned in this documentation Go is part of the build image using v1.16 as mentioned here.

As I have not used Go, I cannot provide specifics on how to use it.

1 Like