Setup minikube as CI step in github actions

How to use minikube in github actions for testing your app

To install and start a minikube cluster, add the following step to your github action workflow.

  1. steps:
  2. - name: start minikube
  3. id: minikube
  4. uses: medyagh/setup-minikube@master

for more information see github actions marketplace setup-minikube.

Example: build image & deploy to minikube on each PR

Requirements:

  • a valid Dockerfile
  • a valid deployment.yaml file with imagePullPolicy: Never see below for an example

Create workflow:

  • copy this yaml to your workflow file for example in .github/workflows/pr.yml:

    1. name: CI
    2. on:
    3. - pull_request
    4. jobs:
    5. job1:
    6. runs-on: ubuntu-latest
    7. name: build example and deploy to minikube
    8. steps:
    9. - uses: actions/checkout@v2
    10. - name: Start minikube
    11. uses: medyagh/setup-minikube@master
    12. - name: Try the cluster !
    13. run: kubectl get pods -A
    14. - name: Build image
    15. run: |
    16. export SHELL=/bin/bash
    17. eval $(minikube -p minikube docker-env)
    18. docker build -f ./Dockerfile -t local/example .
    19. echo -n "verifying images:"
    20. docker images
    21. - name: Deploy to minikube
    22. run:
    23. kubectl apply -f deploy-to-minikube.yaml
    24. - name: Test service URLs
    25. run: |
    26. minikube service list
    27. minikube service example --url
    28. echo "------------------opening the service------------------"
    29. curl $(minikube service example --url)

The above example workflow yaml, will do the following steps on each coming PR:

  1. Checks out the the source code
  2. Installs & starts minikube
  3. Tries out the cluster just by running kubectl command
  4. Build the docker image using minikube’s docker-env feature
  5. Apply the deployment yaml file minikube
  6. Check the service been created in minikube

Example minikube deployment yaml with a service

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: example
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: example
  9. replicas: 2
  10. template:
  11. metadata:
  12. labels:
  13. app: example
  14. spec:
  15. containers:
  16. - name: example-api
  17. imagePullPolicy: Never
  18. image: local/example:latest
  19. resources:
  20. limits:
  21. cpu: 50m
  22. memory: 100Mi
  23. requests:
  24. cpu: 25m
  25. memory: 10Mi
  26. ports:
  27. - containerPort: 8080
  28. ---
  29. apiVersion: v1
  30. kind: Service
  31. metadata:
  32. name: example
  33. spec:
  34. type: NodePort
  35. selector:
  36. app: example
  37. ports:
  38. - port: 8080
  39. targetPort: 8080

Last modified August 2, 2021: added outlined bullets and filled bullets for selecte item (8c283b189)