Variable types
You can set variables at different levels. All variables can be strings or base64-encoded JSON-serialized values. The following table defines what types of variables are available to you:| Type | Definer | Scope | Precedence | Build | Runtime | Uses |
|---|---|---|---|---|---|---|
| Application | Application | Application | 4 | Yes | Yes | Non-secret values that are the same across all environments |
| Project | User | Project | 3 | Yes | Yes | Secret values that are the same across all environments, such as database credentials |
| Environment | User | Environment | 2 | Some | Yes | Values that vary by environment, such as which database to connect to or which payment API keys to use |
| Upsun | Pre-defined | Environment | 1 | Some | Yes | For information about your Upsun project |
Choosing a variable type
Choose how to set the variable based on what you are trying to do. Some environment variables should be the same for all environments. For example:- Build tool versions. If you have scripts that use specific versions of build tools (such as a specific Node.js version), You want the tools to be versioned along with your code so you can track the impact of changes. Set those variables in the application.
- Credentials for common services. If you have credentials for services shared across your environments, you don’t want to commit these secrets to code. Set them as sensitive project variables.
- Service configuration for databases and such.
This information be read from the service environment variables,
or the Upsun-provided
PLATFORM_RELATIONSHIPSvariable. It varies by environment automatically. - Mode toggles such as enabling
debugmode, disabling certain caches, and displaying more verbose errors. This information might vary by environment type and should be set on the environment level. - API keys for remote services, especially payment gateways. If you have a different payment gateway for production and for testing, set its keys on the environment level.
Overrides
If the names of variables at different levels match, an environment variable overrides a variable with the same name in a parent environment and both override a project variable. All variables can also be overridden via script. For an example of how the different levels work, suppose you have the following inheritable variables defined for themain environment:
feature-x environment, a child environment of main:
main environment, you can access $PLATFORM_VARIABLES:
feature-x environment, it looks like this:
feature-x environment:

Variable prefixes
Certain variable name prefixes have special meanings. Some are defined by Upsun and apply automatically. Others are just available as a convention for your application code to follow.Top-level environment variables
By default, project and environment variables are only added to thePLATFORM_VARIABLES environment variable.
You can also expose a variable as its own environment variable by giving it the prefix env:.
For example, the variable env:foo creates an environment variable called FOO.
(Note the automatic upper-casing.)
env: prefix aren’t added to the PLATFORM_VARIABLES environment variable.
PHP-specific variables
Any variable with the prefixphp is added to the PHP configuration for all PHP-based application containers in the project.
Using variables, you can use the same files for all your environments and override values on any given environment if needed.
You can set the PHP memory limit to 256 MB on a specific environment by running the following CLI command:
Framework-specific variables
For specific frameworks, you can implement logic to override global configurations with environment-specific variables. So you can use the same codebase and settings for all your environments, but still adapt the behavior to each environment.Service environment variables
For each service defined via a relationship to your application, Upsun automatically generates corresponding environment variables within your application container, in the$<RELATIONSHIP-NAME>_<SERVICE-PROPERTY> format.
All the non-alphanumerical or _ characters ([^0-9A-Z_]) are transformed into an _ , such as MY_DATABASE_PASSWORD for a my-database relationship name.
Example:
For a relationship named database to a service named postgresql,
the following environment variables are automatically generated in your myapp container: