June 16, 2020

Setup google cloud build with hugo

When running a hugo web page it makes sense to somehow keep the markdown files version controlled and automatically deployed when changed. My preferred and somewhat lazy way is to use github together with google cloud build automatic deploys.

If you as I don’t want to change the markdown files directly in github you could use a editor such as stackedit and connect your github account to the app.

Downside with using stackedit is that it keeps all changes, and triggers the automatic build multiple times hence i prefer to trigger the build manually once i feel ready with my changes to be published.

Hugo is a static site generator, and should be hosted as such. Two examples of hosting it is directly in a google cloud storage or my currently preferred way with cloudflare worker with wrangler

Cloudflare + workers + wrangler static web page

A easy way to host a hugo site is by running it with Cloudflare workers. You can follow below steps to install the wrangler CLI and publish static content to the Cloudflare workers:

  1. Install wrangler CLI
  2. Publish static page

Deploy with google cloud build

As I don’t like to be dependant on a physical computer I try to keep as much as possible on remote machines or cloud providers. Below is a sample of working cloud build deploy for hugo together with cloudflare and workers.

# first step is to run hugo -D to generate all static content
- name: 'klakegg/hugo:0.74.1'
  args: ['-D']
# the second step is to install wrangler by npm and publish the static content from your wrangler.tompl config. 
# the CF_ env variables are auth info for your cloudflare worker
- name: 'node'
  args: ['bash', '-c', 'npm i @cloudflare/wrangler && ./node_modules/.bin/wrangler publish --env production']
  - 'USER=root'
  _CF_ZONE_ID: x

This file could be saved as cloudbuild.yaml into a github repository, and a trigger could be created in google cloud build.

comments powered by Disqus

© Anis Alekic 2020