Referencing environment variables
Use case
We want to move important or frequently changing configuration values (e.g., passwords and other secrets) to environment variables and then reference them in the data model code and configuration files. It would prevent these values from being stored in a source code repository and being exposed to more people than needed. It would also allow to update the deployment configuration without changing the source code.Configuration
In Cube Core, you can put environment variables intodocker-compose.yml.
In Cube Cloud, you can manage environment variables via the Settings
page of your deployment.
cube.py file
In the cube.py configuration file, you can access environment variables either
via the getenv function or the environ dictionary from the os package.
getenv returns None if the variable is not set and allows to provide a default
value. environ raises a KeyError if the variable is not set.
cube.js file
In the cube.js configuration file, you can access environment variables via the
global process.env object. It returns undefined if the variable is not set.
You can use the || operator to provide a default value.
Data modeling
YAML files
In YAML data model files, you can access environment variables via the built-inenv_var Jinja function. It raises an error if the variable is not set and allows
to provide a default value.
globals.py file
In the globals.py file, similarly to the cube.py
file, you can access environment variables either via the getenv
function or the environ dictionary from the os package.
Consider the following file structure:
globals.py file and potentially
expose them to Jinja templates via TemplateContext:
JavaScript files
In JavaScript data model files, the globalprocess.env object is not
available. However, you can use that object in JavaScript
files outside of the data model directory and import from those files.
Consider the following file structure:
process.env in the my_cube.js file but you can do that in
the env.js file since it’s outside of the model directory: