Home > Blockchain >  How to access kubernetes environment variables from package.json?
How to access kubernetes environment variables from package.json?

Time:07-29

I am passing some test variables to my pod which has code in nodejs like this:

apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
  labels:
    purpose: demonstrate-envars
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    send:
    - name: DEMO_GREETING
      value: "Hello from the environment"
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"

I am trying to access the value of these variables from the package.json that I have inside my pod but I'm not sure how to do it, if I don't have an .env file from which I can read these variables using grep

CodePudding user response:

No matter how you have set the variable through Kubernetes, export, or just before calling a command. You can get access to it as in the usual bash script.

"scripts": {
  "envvar": "echo $TEST_ENV_VAR"
},

Then we can run it

➜ TEST_ENV_VAR=4342 npm run envvar

> envvar
> echo $TEST_ENV_VAR

4342
  • Related