Make Laravel env variables visible to frontend via Kubernetes

2/16/2022

i have an App built with Laravel, and one of the env variables i want to make them accessible via frontend.

As the official Laravel documentation indicates: https://laravel.com/docs/master/mix#environment-variables

If we prefix the specific env variables with the MIX_ prefix they will be available and accessible from JavaScript. Locally this works perfectly.

Now, the thing is i want to setup the env variables via Kubernetes configmap when deploying to staging and production.

Here is my config-map.yaml file

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "common.fullname" . }}-app-config
  labels:
    {{- include "common.labels" . | indent 4 }}
data:
  .env: |+
    EVENT_LOGGER_API={{ .Values.app.eventLoggerApi }}
    MIX_EVENT_LOGGER_API="${EVENT_LOGGER_API}"

Now, my deployment.yaml file

  volumes:
   - name: app-config
      configMap:
        name: {{ template "common.fullname" . }}-app-config
  
  volumeMounts:
   - name: app-config
     mountPath: /var/www/html/.env
     subPath: .env

This env variables are visible in the backend Laravel, but cannot be accessed via JavaScript when running locally.

process.env.MIX_EVENT_LOGGER_API

Anyone had any experience before with setting this env variables via K8 configmap and them being accessible via JavaScript?

-- edimeri
configmap
environment-variables
kubernetes
laravel

1 Answer

2/16/2022

If you add variables after building your frontend, those variables will not be available

You need to include those variables in the job that does the build/push

-- Lk77
Source: StackOverflow