Manage environment variables
Environment variables are a method for injecting information into a VIP Platform environment.
Environment variables are commonly used to:
- Provide sensitive values (such as API secrets) to an application without committing the values to a site’s GitHub repository or storing the values in a site’s database.
- Provide flags that can be used to vary behavior between environments. For example, an environment variable can be set to provide one value to a
production
environment and a different value to adevelop
environment.
Prerequisites
- VIP-CLI is installed and has been updated to the most current version.
- Environment variables can only be managed with VIP-CLI
- Environment variables can only be changed by users with an “Admin” Org role or an “Admin” App role. Users with lower capabilities will only have the ability to
list
the names of environment variables that have been defined.
Managing environment variables with VIP-CLI
Note
Each VIP Platform environment has its own discrete set of environment variables. For an environment variable to be available on all environments, it must be set on each environment separately.
VIP-CLI commands to set environment variables require the destination app and environment to be specified, following the syntax @<app-alias>.<env>
.
The examples below use the <app-alias>
value wpvip
, and the <env>
value production
.
set
Add or update an environment variable and assign it a value.
In this example, the new environment variable EXAMPLE_ENV_VAR
is added:
vip @wpvip.production config envvar set EXAMPLE_ENV_VAR
The value 1234567890
is assigned to the EXAMPLE_ENV_VAR
variable at the next prompt:
For multiline input, use the --from-file option. β Enter the value for EXAMPLE_ENV_VAR: Β· 1234567890
Confirm that the entered value is correct:
β Please confirm the input value above (y/N) Β· true
An environment variable and value that has been successfully added will return the output:
Successfully set environment variable "EXAMPLE_ENV_VAR"
--from-file
Accepted values: A relative or absolute path to a UTF-8-encoded text file
The --from-file
option extracts a string from the file existing at the relative or absolute path in the passed value.
Use of the --from-file
option is required for a string that has multiple lines created by return values.
For example, to assign the multiline contents of a *.pem
file as the value for the environment variable EXAMPLE_ENV_VAR
:
vip @wpvip.production config envvar set EXAMPLE_ENV_VAR --from-file=/path/to/public.pem
delete
Remove an environment variable.
vip @wpvip.production config envvar delete EXAMPLE_ENV_VAR
get
Retrieve the value of an environment variable.
vip @wpvip.production config envvar get EXAMPLE_ENV_VAR
get-all
Retrieve the values of all environment variables.
vip @wpvip.production config envvar get-all
If values exist, they will be returned in this format:
βββββββββββββββββββ¬βββββββββββββ
β name β value β
βββββββββββββββββββΌβββββββββββββ€
β EXAMPLE_ENV_VAR β 1234567890 β
βββββββββββββββββββ΄βββββββββββββ
list
List all environment variables.
vip @wpvip.production config envvar list
If values exist, they will be returned in this format:
βββββββββββββββββββ β name β βββββββββββββββββββ€ β EXAMPLE_ENV_VAR β βββββββββββββββββββ
Help menu
View the full list of commands, options, and examples for vip config envvar
:
vip config envvar --help
Accessing environment variables in an application
Environment variables are always provided as strings, even if their value resembles another data type (e.g., "true"
, "123"
, "[1, 2, 3]"
). String values can be converted or deserialized by an application into the desired data type, but they should be validated thoroughly.
Caution
New or updated environment variables will not be available until the application’s next code deployment.
On VIP, environment variables are accessed differently depending on the application type.
Node.js
Environment variables are provided as actual environment variables and are accessed using process.env
. For example:
const apiKey = process.env.EXAMPLE_ENV_VAR || null;
WordPress
Environment variables are accessed using the Environment::get_var()
helper method or vip_get_env_var
helper function. For example:
<?php
// Using class method
$api_key = Automattic\VIP\Environment::get_var( 'EXAMPLE_ENV_VAR', $default_value );
// Using helper function
$api_key = vip_get_env_var( 'EXAMPLE_ENV_VAR', $default_value );
Note
Both the theΒ Environment::get_var()
Β helper method and theΒ vip_get_env_var
Β helper function are available for use in vip-config.php.
Reserved names
Some environment variable names are reserved:
- To avoid conflicts, names reserved for WordPress constants (e.g.,
DB_NAME
) cannot be used as environment variables. - Names starting with
VIP_
are reserved for use by VIP.