Kubernetes Config File Environment Variable

Hello everyone.

I want to pass from my CircleCI project environment variable to kubernetes config file. How can I do that?

I have a file config.yml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: ${APP_NAME}-${APP_SUBGROUP}-${APP_GROUP}

And the variables APP_NAME, APP_SUBGROUP and APP_GROUP are defined in the environment variables on CircleCI.

My .circleci/config.yml version is 2.1 and I’m using:

orbs:
  gcp-cli: circleci/gcp-cli@1.8.3
  kubernetes: circleci/kubernetes@0.10.1

And in the middle of my .circleci/config.yml, I’m calling kubernetes orbs options:

      - kubernetes/install-kubectl
      - kubernetes/create-or-update-resource:
          resource-file-path: config/config.yml
          resource-name: deployment/config
          show-kubectl-command: true

So… What can I do to replace all my environment variables inside my k8s yamls configuration while my continuous deployment is running?

Thanks in advance.

1 Like

Anybody knows how to help me?

Hello,

I have the same problem. We’re you able to find a solution?

I just gave up. There aren’t documentation for this kind of problem and they only help us if we pay for it. So… I resolved to change for Gitlab.com

Hey, did you get it sorted out eventually? If not here is the solution.

Use this Circleci orb

Suppose you have a ConfigMap file in your repo in the k8s folder:

~\k8s\YourConfigMap.yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
  labels:
    app: app-staging
data:
  ENV1: $VAR1
  ENV2: $VAR2

and you declared Environment variables $VAR1 and $VAR2 for your project in Circleci.
Then your ~/.circleci/config.yml file would look like:

version: 2.1
orbs: 
  kubernetes: circleci/kubernetes@1.3.0
jobs:
  build-and-test:
    docker:
      - image: mcr.microsoft.com/dotnet/sdk:6.0
    steps:
      - checkout
      - run:
          #working_directory: ~/
          name: Run tests
          command: |
            dotnet test
      - kubernetes/install-kubectl
      - kubernetes/install-kubeconfig:
          kubeconfig: KUBECONFIG_DATA
      - kubernetes/create-or-update-resource:
          resource-file-path: k8s/YourConfigMap.yml
          envsubst: true
workflows:
  sample: 
    jobs:
      - build-and-test

The key parameter here is envsubst: true