Access environment variables in webpack.js react in kubernetes environment

2/21/2020

This a part of my webpack file.

output: {
    filename: "[name].[chunkhash:8].js",
    path: path.resolve(__dirname, "../../dist/static/"),
    publicPath: `${**process.env.STATIC_URL**}/xxxxxxx/static/`
  }

I want to access environment variables set in configmap of kubernetes here. Is there a way to do that?

-- Ekta Varshney
kubernetes
reactjs
webpack

1 Answer

2/21/2020

There is a dedicated envFrom: field in the PodSpec that allows injecting ConfigMap keys as environment variables (assuming they are "environment safe," so no integers or boolean or non-string values):

containers:
- image: whatever
  envFrom:
    configMapRef:
      name: your-configmap-name-goes-here

Or, if you just want that one key, then there is a similar valueFrom: field in the env: items themselves:

containers:
- image: whatever
  env:
  - name: STATIC_URL
    valueFrom:
      configMapKeyRef:
        name: your-configmap-name-goes-here
        key: whatever-key-holds-the-static-url-value
-- mdaniel
Source: StackOverflow