Blogging with Hugo

Friday, April 17, 2020

I had the desire to setup a website the other day. This lead me to looking into static blogs again which lead to me to hugo.

Here is the short guide to how I set it all up. I wanted to keep the version of the hugo runtime checked into git. I know from past experience that go modules have the capability to do this, if you use go run from within the directory. This seemed like a good starting point.

# Setup the repo
git init
go mod init

# Install hugo:
go get --tags extended

# Initialise hugo, with force because git and other files already exist in the
# current directory:
go run new site . --force

Now I just needed to make it make it a bit easier to run hugo commands. For this I created a file called ./scripts/hugo.


go run [email protected]

Next up we need to make it deployable. This meant creating an s3 bucket (or three).

the three buckets created

  • First we have the bucket. This is where the website is uploaded. This is configured as a static website via the s3 console.
  • Next there is the bucket. The first bucket is configured to have access logs go into this.
  • Finally there is the bucket. This one is configured as a static server with the redirect option configured to send to the main site.

It is a good idea to put a CDN in front of an S3 bucket that is serving static content. I being very cheap went with cloudflare. In order for cloudflare to be able to access the bucket we had to set an ACL. I shamelessly stole one that I found on the internet.

    "Version": "2012-10-17",
    "Statement": [
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": [

Then I had to configure hugo to deploy to the s3 bucket. This took a trivially small amount of Toml.

# An arbitrary name for this target.
name = "aws"
# S3; see
URL = "s3://"

Testing by running ./scripts/hugo deploy showed this all working. Using the cloudflare panel I setup my DNS and away I went. Testing showed everything working.

To simplify things I wrote a small makefile with the common commands.


.PHONY: run
	./scripts/hugo server

.PHONY: git-submodule
	git submodule update --init --recursive

.PHONY: build

.PHONY: deploy
	 ./scripts/hugo deploy

I had one last goal for all of this. Setting up github actions so that it would auto deploy on pushes to master. This required creating an AWS IAM policy and user to do the deploys. The policy is below. It is critical that when listing your resource you include both "" and "*". The /* on the second one is what allows the policy to work for items in the S3 bucket and not just the bucket itself. Can’t tell you how many times myself and coworkers have been bit by that.

    "Version": "2012-10-17",
    "Statement": [
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
            "Resource": "*"
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [

Once I created the policy I setup two secrets for the github repository.

two github repository secrets

Finally the github configuration to pull it all together:


name: Go

    branches: [ master ]
    branches: [ master ]

    name: Build and Deploy
    runs-on: ubuntu-latest

    - name: Set up Go 1.13
      uses: actions/[email protected]
        go-version: 1.13
      id: go

    - name: Check out code into the Go module directory
      uses: actions/[email protected]

    - name: Get dependencies
      run: |
        go get -v -t -d ./...
        if [ -f Gopkg.toml ]; then
            curl | sh
            dep ensure

    - name: Build
      run: make git-submodule && make build

    - name: "Configure AWS Credentials"
      uses: aws-actions/[email protected]
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: eu-west-1

    - name: Deploy
      run: make deploy

Then it was just a matter of testing and making sure it all worked together.

I would like to play around with doing the more ops/config side of this via terraform. It seems to have a Cloudflare provider, and I am already familiar with its AWS S3 provider. Normally I start with terraform but for whatever reason I didn’t on this project. Oh well maybe soon.