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

Viewed 4551

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) ?

5 Answers

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

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.

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.

For future readers. Another way is to use a template file and generate deployment.yaml from the template using envsubst.

Template file:

# template/deployment.tmpl
---
apiVersion: apps/v1
kind: deployment
metadata:
  name: strapi-deployment
  namespace: strapi
  labels:
    app: strapi

# deployment specifications
spec:
  replicas: 1
  selector:
    matchLabels:
      app: strapi
  serviceName: strapi

  # pod specifications
  template:
    metadata:
      labels:
        app: strapi

    # pod blueprints
    spec:
      containers:
        - name: strapi-container
          image: registry.gitlab.com/repo-name/image:${IMAGE_TAG}
          imagePullPolicy: Always
    imagePullSecrets:
    - name: gitlab-registry-secret

deploy stage in .gitlab-ci.yml

(...)
deploy:
  stage: deploy
  script:
    # deploy resources in k8s cluster
    - envsubst < strapi-deployment.tmpl > strapi-deployment.yaml
    - kubectl apply -f strapi-deployment.yaml

As defined here image: registry.gitlab.com/repo-name/image:${IMAGE_TAG}, IMAGE_TAG is an environment variable defined in gitlab. envsubst would go through strapi-deployment.tmpl and substitute any variable defined there and generate strapi-deployment.yaml file.

sed command helped me with this:

In Deployment.yaml use some placeholder, like this:

apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: myapp
    #Other configs bla-bla-bla
      spec:
          containers:
            - name: app
              image: my.registry./myapp:<VERSION>

And in .gitlab-ci.yml use sed:

deploy:
  stage: deploy
  image: kubectl-img
  script:
    # - kubectl bla-bla-bla whatever you want to do before the apply command
    - sed -i "s/<VERSION>/${CI_COMMIT_SHORT_SHA}/g" Deployment.yaml
    - kubectl apply -f Deployment.yaml

So the resulting Deployment.yaml will contain CI_COMMIT_SHORT_SHA value instead of <VERSION>

Source of the solution

Related