Gitlab CI and Digital Ocean Kubernetes Config File

I started working on a new side project a few months ago. I wanted to build an app to help me and other people find the best events or things to do near me. Yes, it's been done before but I believed that my approach would be better and I wanted to give it a try and learn some new things along the way too.

The tech stack of choice for this was Golang (Go), Kubernetes (K8s), MongoDb - kinda regretting this one so might be a whole refactor on this and a blog post - GraphQL, and React Native. I am using Gitlab as repo, container registry and to manage my build and deployment pipeline as well.

The infracsture for this project is managed by Digital Ocean Kubernetes cluster service. I won't go into much detail but to communicate with this cluster you need a .yaml file that has all the configuration needed to do so: hostnames, tokens and other bits and pieces. I was surprised to find this key would reset every week, requiring you to update it on your CI/CD pipeline or risking having all your projects broken.

To automate this process I wrote a few simple commandline statements and help me save a lot of time and annoyance.

variables:
  KUBECONFIG: /etc/deploy/config
  DO_KUBE_CLUSTER_ID: [your_digital_ocean_cluster_id]
  DO_TOKEN: [your_digital_ocean_token]

deploy_env_name:
  stage: deploy
  environment:
    name: [env_name]
    url: [url to your app in this env_name]
  script:
    - apk add --no-cache curl
    - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
    - chmod +x ./kubectl
    - mv ./kubectl /usr/local/bin/kubectl
    - mkdir -p /etc/deploy
    # get DO kubeconfig
    - >-
      curl -X GET 
      -H "Content-Type: application/json" 
      -H "Authorization: Bearer ${DO_TOKEN}" "https://api.digitalocean.com/v2/kubernetes/clusters/${DO_KUBE_CLUSTER_ID}/kubeconfig" > ${KUBECONFIG}
    # set context
    - kubectl config use-context [context_name] #can be found in your kube_config.yaml

That's it, replace <env_name> with the environment you want to deploy to - this depends on how you have your project setup though. I ommitted some other steps because they are out of the scope of this post but, with the steps above you should be able authenticated and able perform actions against your kubernetes cluster.

I really love the integration between Digital Ocean and Gitlab. There are some things I still don't quite get well but overall the experience is better and simpler than other cloud providers. If you would like to give try Digital Ocean you can use my code https://m.do.co/c/d2ed5318c53a