Working with Environment Variables
You can define environment variables to use as values in your SML object files. Environment variables enable you to deploy models across different data warehouses and environments (for example, development, staging, and production).
Environment variables are currently only supported in connection files.
Environment variables are defined in an .env
file, which is stored at the root level of your repository. Once an environment variable has been defined, you can use it to replace values in other SML object files.
The following sections describe how to work with .env
files in AtScale.
Add an .env file
To add an .env
file to your repository:
- In Design Center, open the Repo Browser.
- Click the new object button and select .env.
The .env
file appears in the Repo Browser at the root level of your repository, and is automatically added to your .gitignore
file.
You can only have one .env
file in a repository at a time.
Define environment variables
Once you have created an .env
file, you can start defining your environment variables. Variables are defined using the following syntax:
VARIABLE="value"
You can define values of any data type. When defining string values, you must include quotes ("). You can also comment out lines in your file with #
.
For example, to define a variable for a database connection label:
LABEL="Postgres"
Within object files, you can reference your environment variables using Jinja syntax: {{variable-name}}
. For more information, refer to the Jinja documentation and npm nunjucks JS library.
To continue with our example, you could add the following to your connection file. This would set the label
property to Postgres
.
label: {{LABEL}}
You cannot use environment variables in object_type
or unique_name
SML properties.
You can also use environment variables to partially define properties. For example, if your database connection names all use the same base followed by numbers, you could define the base as an environment variable:
CONNECTION="db_con"
And then use the variable to partially define values in your connection files:
as_connection: {{CONNECTION}}01
Examples
The following is an example of an .env
file, with variables for a connection file:
# Database Connection Configuration
CONNECTION="con1"
DB_NAME="atscale"
DB_SCHEMA="as_adventure"
# Connection Metadata
LABEL="Postgres"
And below is a sample connection file using the variables defined above:
unique_name: Postgres
object_type: connection
as_connection: {{CONNECTION}}
label: {{LABEL}}
database: {{DB_NAME}}
schema: {{DB_SCHEMA}}