Kubernetes autodeploy with Gitab CI
There are two ways for upgrade service in kubernetes , use directly api or kubectl , i perefer kubectl due readable yaml files .
for rancher v2 api call should be like this example :
you can create an script and run in your ci for upgrading your service but as i mention above reading and edit this type of apiCall can be painful , but if you want to use api just replace your script with below example of kubectl.
for using kubectl as command in gitlab runner you most create a dockerfile , here the last version of kubectl and alpine image and gettext
if you access to your gitlab runner you can mount your kubectl config file into container in /etc/gitlab-runner/config.toml in [runners.docker] section like this
but if you use gitlab shared runner and dont use your private runner create your image with kubectl config file in it its easy just put copy command in your dockerfile
and push your image to private repository.
for staging and production you need template for your service and change it with your pipeline variables so its a template example of deployment for kubernetes
in this simple example we just update container image and nothing change in configmap or anything else but if you get the key of this article you can automate everything
now in gitlab-ci you most add a stage for staging
i use shell executer for create and push images due privilege problem can DinD( docker in docker) can happen
i use envsubst for changing variable in file and create temporary staging deployment file you can use sed or any text manipulation tool you like.