How to pass gitlab ci/cd variables to kubernetes(AKS) deployment.yaml

6/21/2019

I have a node.js (express) project checked into gitlab and this is running in Kubernetes . I know we can set env variables in Kubernetes(on Azure, aks) in deployment.yaml file.

How can i pass gitlab ci/cd env variables to kubernetes(aks) (deployment.yaml file) ?

-- Fakeer
gitlab
kubernetes

3 Answers

6/21/2019

Another solution would be to create the thing you are deploying as a Helm Chart. This would allow you to have specific variables (called values) that you can use in the templating and override at install / upgrade time.

There are many articles around getting setup with something like this.

-- Andy Shinn
Source: StackOverflow

6/21/2019

I'm going to give you an easy solution that may or may not be "the solution".

To do what you want you could simply add your gitlab env variables in a secret during the cd before launching your deployment. This will allow you to use env secret inside the deployment.

If you want to do it like this you will need to think of how to delete them when you want to update them for idempotence.

-- night-gold
Source: StackOverflow

6/21/2019

You can develop your own helm charts. This will pay back in long perspective.

Other approach: there is an easy and versatile way is to put ${MY_VARIABLE} placeholders into the deployment.yaml file. Next, during the pipeline run, at the deployment job use the envsubst command to substitute vars with respective values and deploy the file.

Example deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment-${MY_VARIABLE}
  labels:
    app: nginx
spec:
  replicas: 3
(...)

Example job:

(...)
deploy:
  stage: deploy
  script:
    - envsubst < deployment.yaml > deployment-${CI_JOB_NAME}.yaml
    - kubectl apply -f deployment-${CI_JOB_NAME}.yaml
-- Janusz
Source: StackOverflow