Environment variable mappings

Why and how to use environment variable mappings

There are many reasons you may want to use environment variables in your application, for example, to provide a backend URL for your frontend.

Here is an example config.js for a React application:

export default {
  API: process.env.REACT_APP_BACKEND_BASE_URL,
  FRONTEND_URL: process.env.REACT_APP_FRONTEND_BASE_URL
};

In this example, Release will autogenerate ingress URLs for the application to expose ports:

- key: BACKEND_INGRESS_URL
  value: https://api.releaseapp.io
- key: FRONTEND_INGRESS_URL
  value: https://app.releaseapp.io

In order for the frontend application to use the URLs autogenerated by Release, you can map the application's environment variables to the autogenerated environment variables. You can map environment variables in an application's default environment variables or in the environment-specific environment variables.

Here is an example environment variable mapping:

mapping:
  REACT_APP_BACKEND_BASE_URL: BACKEND_INGRESS_URL
  REACT_APP_FRONTEND_BASE_URL: FRONTEND_INGRESS_URL

Mappings are expressed as key-value pairs, with the application's environment variable as the key and the Release autogenerated environment variable as the value.

Interpolation

Environment variable mappings are also the only place where interpolation can occur. You can try to interpolate variables together to create a new string:

mapping:
  DB_URL: "postgres://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}

You can also use bash interpolation rules for fallback if the variables are not present. As an example, your frontend may use an app import to connect to a backend. If the backend is not present, however, it can fallback to a staging environment.

mapping:
  BACKEND_URL: ${BACKEND_INGRESS_URL:-https://staging.example.com}

Deploy Your Changes

Once you have added your environment variable mapping, save the configuration and deploy.

Last updated