Back to Node IV: Kubernetes Deploy

Featured image

The final part in my quest to write a URL shortener in Typescript. If you haven’t read the previous parts, please check out Part 1, Part 2 and Part 3.

This final post sets out how I’m deploying ziz-shortener to my MicroK8s cluster using GitHub Actions.

I’m not 100% sure why, but I decided to split out the deployment configuration from the existing repo. I had an idea that if others wanted to use the URL shortener they didn’t need my Kubernetes config files as well. It’s unlikely anyone else is going to use the URL shortener so probably overkill.

Triggering Workflows between Repos

I was hoping to use a simple webhook to trigger a build in the deployment repo but GitHub doesn’t allow that at this time. You have to use the GitHub API which means authenticating with a personal access token and submitting a specific payload. Thankfully there’s a GitHub action I can use without having to craft the request myself.

This code goes in the repo you want to trigger the workflow from (i.e. ziz-shortener for me).

- name: Repository Dispatch
  uses: peter-evans/[email protected]
  with:
    token: ${{ secrets.REPO_ACCESS_TOKEN }}
    repository: ezzizzle/shortener-deploy
    event-type: shortener-build

In the repo you want to trigger the build on (i.e. shortener-deploy for me) you need to listen for the specified repository dispatch event.

on:
  repository_dispatch:
    types: [shortener-build]

Now when the ziz-shortener repo finishes building on the main branch the shortener-deploy repo builds and deploys to my cluster.

Wrapping Up

I’ve written more words on this project than I expected to but I’ve enjoyed it. I don’t know how much I’ll use the shortener but it was a good small project to get back into Node.js development and play around more with GitHub Actions.

The final process for deploying changes to my URL shortener looks like this:

  • Create a branch off main in ziz-shortener
  • Open a pull request which triggers a test workflow
  • Merge into main which triggers a test, build, and push workflow
  • This triggers the deploy workflow in shortener-deploy which deploys the updated Docker image onto my cluster.

I still find GitHub workflows a little clunky but I’m getting more used to them. It might be that I’ve been doing a bit with Jenkins and GitLab lately.1

ToDo

There’s a few things that I would like to add or change at some stage:

  • Build and deploy each branch of the shortener
  • Consolidate the API functionality of the shortener behind a single /api endpoint
  • I’m using oauth2-proxy to secure the API which means I need to sign in before adding new URLs. I need to generate tokens I can use in a Bearer header so I can generate shortened URLs through Shortcuts more easily.

  1. Jenkins under protest. ↩︎