I am trying to develop my automation build.
So what we want to achieve is whenever a developer push a code to a new branch, it will create a new namespace in kubernetes based on that branch name. then I will do all deployments in that namespace.
What is the best way to create namespace?
- name: 'gcr.io/cloud-builders/kubectl'
args:
- 'create'
- 'namespace'
- '${BRANCH_NAME}
will have a problem when namespace exist. So, I was planning to use file instead then I can apply it via
- name: 'gcr.io/cloud-builders/kubectl'
args:
- 'apply'
- '-f'
- 'filename.yaml'
and here is the filename.yaml:
apiVersion: v1
kind: Namespace
metadata:
name: '${<BRANCH>}'
Now my question is, how to pass this branch name if I go with second approach. If I go with my first approach I do not know how to check whether namespace exist or not.
Please help.
Thanks.
Second approach
Lets say, you are using following filename.yaml
file
apiVersion: v1
kind: Namespace
metadata:
name: $BRANCH
Here, $BRANCH
is ENV variable inside your YAML
Now all you need to substitute this variable with ENV value.
$ export BRANCH="demo"; cat filename.yaml | envsubst | kubectl create -f -
namespace "demo" created