Setting Environment Variables for Site (Helm ERPNext)

Hello. I’m looking for a way to store environment variables to be accessed by client script or web page docs. Right now these values are hard-coded inside the scripts and we are trying to prevent configuring these variables for each environment, especially on start-up. Then I saw this post is this the only way to do it? Thanks in advance!

each deployment can take extra vars. pass them through values.yaml. you can use name/value or secrets or configmaps.

e.g.

use os.environ.get("VAR_NAME") in python code

As the other post says, common_site_config.json and site_config.json(s) are used to set configuration data

if you need variables for OS, runtime e.g. environment variable TZ or NODE_OPTIONS then use envVars: []

2 Likes

Thanks for the reply. It works, unfortunately, this doesn’t solve my problem just yet. It turns out I need to somehow access these values on the client side. Since both the webpage and client scripts load on the client side if I’m not mistaken. If that’s the case, do I need to set these values in the site_config? I tried following the post and adding it in the config then used frappe.conf but for some reason, it throws undefined. Also tried clearing the cache.

I found a workaround. I’m not sure if this is how you should do it, but it works for me. So, inside the custom app, I created a new python script called ‘utils.py,’ and then I used ‘frappe.call’ in the js to call the method and get the env variables.

utils.py:

import os
import frappe
from frappe import _

@frappe.whitelist()
def get_env(key):
        return os.environ.get(key)

javascript:

frappe.call({
    method: "custom_app.utils.get_env",
    args: {
        key: "var_name",
    },
    callback(r) {
        console.log(r)
    }
});

make sure it only exposes variables to privileged users.

if you don’t validate that any logged in user can fetch env vars

1 Like

Ah yes. Thank you for pointing that out.