Using GitHub Actions to Deploy Continuously to Kyma/Kubernetes

Preparations

  • Clone this repo in a new WSL folder and start the included Dev Container

Dockerize the App

node ➜ /workspaces/sap-tech-bytes (2021-08-02-github-actions-kyma)
$ docker build -t sap-tech-bytes .
1
2

cap-kyma-app

  • Tag the image with your registry account user
node ➜ /workspaces/sap-tech-bytes (2021-08-02-github-actions-kyma) 
$ docker tag dr-sap-tech-bytes:latest draschke/dr-sap-tech-bytes

output--> draschke/dr-sap-tech-bytes:latest
1
2
3
4

cap-kyma-app

Update your .kube/config

  • Update your .kube/config (after 8 hours you will have to change it again)
 cp .\kubeconfig.yaml  ~/.kube/config
 #find your directory here
\\wsl$\Ubuntu-20.04\home\<youruser>\.kube
1
2
3

The Kyma Manifest

  • Make sure you are logged in to kubectl by running one of this commands
node ➜ /workspaces/sap-tech-bytes (2021-08-02-github-actions-kyma)
$ kubectl get ns
$ kubectl get pods
1
2
3
  • Change the dev_deployment.yaml
#image: ghcr.io/<user>/<repo name>:latest
1
  • Create secret “regcred“ (Take care that you have a namespace "tutorial")
node ➜ /workspaces/sap-tech-bytes (2021-08-02-github-actions-kyma)

$ kubectl -n tutorial create secret docker-registry regcred --docker-server=https://ghcr.io  --docker-username=<github user>  --docker-password=<github personal access token>
1
2
3
  • After creating the secret "regcred" you'll find it here

docker registry credentials

Set up the GitHub Repository

  • Go to your kubeconfig.yml and export a base64 value file and paste it to your git repository secrets
\\wsl$\Ubuntu-20.04\home\<user>\.kube\kubeconfig.yml
cat kubeconfig.yaml | base64 > base64-kubeconfig.txt
1
2
  • Create a repo secret DEV_KUBECONFIG with the base64 value

cap-kyma-app

Create Two GitHub Workflows

  • Change the name of your image
.github/workflows/deploy.yaml
  IMAGE_NAME: sap-samples/tech-bytes-kyma-cicd
1
2
  • You need your GITHUB_TOKEN
password: ${{ secrets.GITHUB_TOKEN }}
1
  • You need your DEV_KUBECONFIG
 config: ${{ secrets.DEV_KUBECONFIG }}
1

This is what happen when you trigger the deployment.yaml file (.github/workflows/deploy.yaml)

  1. Load metadata from the Dockerfile
  2. Build the image
  3. Push the image to the registry
  4. Read the kubeconfig from the secret we created and deploy the image to your Kyma cluster.
  • Packages on the right side

cap-kyma-app

  • Check if your app is running (If not keep an eye at your Pods (error logs / credentials))

cap-kyma-app

Trigger the Pipeline

  • Increase the version
node ➜ /workspaces/sap-tech-bytes (dr-sap-tech-bytes-2021-08-02) 
$ npm version patch
v1.1.3 
node ➜ /workspaces/sap-tech-bytes (dr-sap-tech-bytes-2021-08-02) 
$ git push --follow-tags
...
 * [new tag]         v1.1.3 -> v1.1.3
1
2
3
4
5
6
7
Have had this issue: 
node ➜ /workspaces/sap-tech-bytes (dr-sap-tech-bytes-2021-08-02 ✗) 
$ npm version patch 
Output #npm ERR! Git working directory not clean.
1
2
3
4
Solution:
node ➜ /workspaces/sap-tech-bytes (dr-sap-tech-bytes-2021-08-02 ✗) 
$ npm version --no-git-tag-version
After that you should be fine
1
2
3
4
  • Update the current deployment to make use of the latest image

The publish.yaml file includes a nice Action to keep your releases updated

cap-kyma-app

Kudos to Marius Obert for this great technical article