andreplacido / hugo-gcp-deploy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hugo Static Site Running on GCP Free Tier VM

This project is to create a site with Hugo and deploy it to GCP in < 5min.

Accompanying Blog Posts:

  1. The Making of devopsdirective.com (Hugo, Caddy, + GCP)
  2. CI/CD for devopsdirective.com (Hugo + Cloud Build)

NOTE: For a static site like this, the built in hugo deploy approach to host directly within a GCP/AWS/Azure bucket might be easier/more scalable, but I wanted to familiarize myself with various server options (first I used nginx, and then switched to caddy b/c of its automatic https config!) and eventually want to do some benchmarking on the f1-micro instance to see what kind of load it can handle.

Prerequisites

  • Install hugo (brew install hugo)
  • Install docker (brew cask install docker)
  • Install google cloud sdk (brew cask install google-cloud-sdk && gcloud init)

Site Setup Instructions

Before starting, update the following:

    <SITE_NAME> (within ./Makefile, e.g. my-website)
    <DOMAIN(S)> (within ./Caddyfile, e.g. my-website.com www.my-website.com)
    <EMAIL_ADDRESS> (within ./Caddyfile, e.g. my.name@gmail.com)

to their appropriate values.

  1. Run $make create-site (uses https://gohugo.io/getting-started/quick-start/)
    • Creates the hugo site and moves it into this directory
  2. Run $make create-project
    • Creates a new GCP project
  3. enable billing using the project link output by previous command
  4. Run $make vm-setup
    • Enables compute.googleapis.com and containerregistry.googleapis.com
    • Reserves a static IP for the site
    • Adds http and https firewall rules
    • Creates f1-micro vm,
    • Configures your local docker install to use google container registry (gcr)
    • Configures VM docker install to be able to use google container registry
  5. Run $make deploy
    • Builds hugo site
    • Builds caddy based docker container containing site files
    • Tags and pushes to gcr
    • Removes currently running containers on VM
    • Starts container on VM using new container image
  6. Run $make list-vms --> create A record for domain (not automated)
    • Go to your DNS provider and point your domain to the VM static IP
    • It's also useful to add a www CNAME record (to automatiaclly point requests like www.my-website.com to my-website.com)
    • You also need to update the Caddyfile to reflect your domain and enable TLS

CI/CD Setup Instructions

Before starting, update the following:

    <SITE_NAME> (within ./Makefile, e.g. my-website)
    <REPO_NAME> (within ./Makefile, e.g. my-cloud-source-repo-mirroring-a-github-repo)
    <USER_NAME> (within ./Makefile, e.g. myusername -- this shoud be the username of the user when you SSH into the VM)

to their appropriate values.

  1. Mirror GitHub repo in Cloud Source (https://cloud.google.com/source-repositories/docs/mirroring-a-github-repository) -- assuming primary site repo is in GitHub
  2. Run `make setup-cloud-build
    • Enables the Cloud Build API
    • Adds the necessary IAM roles to the Cloud Build service account
    • Creates the Cloud build Trigger

TODO:

  • Tune caddy config
  • Add CDN (eventually)

About


Languages

Language:Makefile 98.2%Language:Dockerfile 1.8%