release-env
is a small package that exposes environment variables from Release to your application at run time. Any environment variable that starts
with RELEASE_
will be exposed at run time via process.env
.
Release allows for the creation of ephemeral staging environments, meaning there are new URLs for services every time an environment is created. If your Javascript application needs to talk to a service which has been given a generated URL then that URL needs to be exposed via an environment variable. Without server side rendering, these environment variables need to be available and exposed when the application starts or when a static build is built.
The idea behind release-env
comes from
Create React App and how
REACT_APP_
environment variables are exposed via process.env
(see Adding Custom Environment Variables).
If you are using Create React App
it is not necessary to use release-env
as you
can achieve the functionality of this package by mapping Release environment variables to
REACT_APP_
variables. Please see the Release docs on Mapping Environment Variables.
# with npm
npm install release-env
# or with Yarn
yarn add release-env
In your webpack.config.js
file import the getReleaseEnvironmentVariables
function.
const getReleaseEnvironmentVariables = require("release-env");
Inside of the module.exports
, access the Release environment variables.
module.exports = function(webpackEnv) {
const releaseEnv = getReleaseEnvironmentVariables();
...
}
Define a new plugin with the Release environment variables.
plugins: [
new webpack.DefinePlugin(releaseEnv.stringified),
...
]
In your application the environment variables will be accessible via process.env.RELEASE_ENV_VAR
. An example
would be to be define a configuration file to store API paths.
// config.js
export default {
API_URL: process.env.RELEASE_BACKEND_INGRESS_URL,
};
// api.js
import axios from "axios";
import config from "./config";
const client = axios.create({
baseURL: config.API_URL,
headers: {
"Content-Type": "application/json"
}
});
export default client;