Google Cloud Build: Deploying to Google Kubernetes Engine

Cloud Build has made deploying to Google Kubernetes Engine very easy.

Here are the steps required to deploy a static site compiled with Hugo and served by Caddy with HTTPS to Kubernetes. For debugging purposes we make a file containing the first 7 characters of the commit SHA, this can be accessed at https://bana.io/VERSION, so that we can verify a new version was deployed on Kubernetes. If you're using Google Cloud Platform, this is very simple if you ask me.

# Prerequisites

Follow the Deploying artifacts steps so that you can use kubectl to deploy to Kubernetes Engine.

# cloudbuild.yaml

steps:
# Cloud Build isn't handling submodules at the moment, so we manually clone it into the themes directory.
- name: "gcr.io/cloud-builders/git"
  args: ["clone", "--depth", "1", "https://github.com/calintat/minimal.git"]
  dir: "/workspace/themes"
# Store the git sha so that we know what version of the site we are at
- name: "ubuntu"
  args: ["bash", "-c", "echo $SHORT_SHA > VERSION && date --iso-8601=seconds >> VERSION && ls -lah"]
# Double-chek that VERSION was persisted in the workspace
- name: "ubuntu"
  args: ["bash", "-c", "cat VERSION"]
# use Docker Compose to build instead of Docker
- name: "docker/compose:1.15.0"
  args: ["build"]
# push the images just before we update the Kubernetes deployment
- name: "gcr.io/cloud-builders/docker"
  args: ["push", "gcr.io/www-bana-io/www-bana-io:latest"]
# update the deployment
- name: "gcr.io/cloud-builders/kubectl"
  args: ["patch", "deployment", "www-bana-io", "-p", "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"SHORT_SHA\":\"$SHORT_SHA\"}}}}}"]
  env:
  - "CLOUDSDK_COMPUTE_ZONE=us-central1-a"
  - "CLOUDSDK_CONTAINER_CLUSTER=www-bana-io-cluster"

images:
- "gcr.io/www-bana-io/www-bana-io"

tags:
- "www-bana-io"
- "frontend"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# docker-compose.yaml

version: "3"
services:
  www-bana-io:
    image: gcr.io/www-bana-io/www-bana-io:latest
    container_name: www-bana-io
    hostname: www-bana-io
    build:
      context: .
      dockerfile: ./Dockerfile
    volumes:
      - caddy-certs:/root/.caddy/
    ports:
      - 80:80
      - 443:443
    labels:
      kompose.service.type: loadbalancer

volumes:
  caddy-certs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# Caddyfile

bana.io www.bana.io {
  tls bana@bana.io

  log stdout
  errors stderr

  browse
  gzip
}
1
2
3
4
5
6
7
8
9

# Dockerfile

FROM klakegg/hugo:0.45-alpine-onbuild AS hugo

FROM abiosoft/caddy:0.11.0-no-stats
# auto-agree to Let's Encrypt Subscriber Agreement
ENV ACME_AGREE true

COPY ./Caddyfile /etc/Caddyfile
COPY --from=hugo /onbuild /srv
COPY ./VERSION /srv/VERSION

RUN echo -e "\033[92m  ---> ls -lah /srv/ \033[0m"; ls -lah /srv/; true;
RUN echo -e "\033[92m  ---> cat VERSION \033[0m"; cat /srv/VERSION; true;
1
2
3
4
5
6
7
8
9
10
11
12

# Build Log

TODO
1
Last Updated: 9/30/2022, 6:21:49 PM