Access variables in a shell
Project and environment variables with the prefixenv:
are available as Unix environment variables in all caps.
Access these variables and Upsun-provided variables directly like this:
PLATFORM_VARIABLES variable as a base64-encoded JSON object.
Access them like this:
jq processor:
build hooks and those available at runtime can be accessed in deploy hooks.
Access variables in your app
To access environment variables in your app, use a built-in method for the given language.- PHP: The
getenv()function - Python: The
os.environobject - Node.js: The
process.envobject - Ruby: The
ENVaccessor - Java: The
System.getenv()method
- PHP
- Python
- Node.js
- Ruby
- Java
Access complex values
Variables can have nested structures. The following example shows nested structures in an app configuration: You can access these nested variables as follows:- Shell
- PHP
- Python
- Node.js
Use provided variables
Upsun also provides a series of variables to inform your app about its runtime configuration. Many of them have aPLATFORM_ prefix to differentiate them from user-provided values.
You can’t set or update them directly.
The following table presents all available variables
and whether they’re available at build time (during build hooks)
and at runtime.
PLATFORM_APPLICATION
The PLATFORM_APPLICATION variable is available both at build time and in the runtime environment.
But the specific attributes it contains differ in each case.
Each environment’s build is associated with a configuration ID that identifies it uniquely so builds can be reused.
The ID is a product of your app code and some of its configuration for Upsun.
Not every attribute your app configuration is relevant to the build.
Only those attributes that are relevant to builds are accessible at build time from PLATFORM_APPLICATION.
Attributes that are not available in PLATFORM_APPLICATION during builds:
- Everything under
access - Everything under
relationship hooks.deployandhooks.post_deploy- Everything under
crons - Everything under
web, exceptweb.mounts - Everything under
workers, exceptworkers.mounts
Use variables in static files
Some apps require configuration values to be specified in a static, non-executable file (such as a.ini, .xml, or .yaml file)
and don’t support reading from environment variables.
To populate these files with variables you set yourself,
make sure the variables are set to be visible at build time.
The files can’t be populated with Upsun-provided variables not available at build time (such as PLATFORM_RELATIONSHIPS or service environment variables).
You also can’t write to them in a deploy hook as the file system is read only.
One workaround is to create a symbolic link to a writable location and then write to it in a deploy hook.
The following example shows the process, though you have to modify it to fit your needs.
- Create a mount that isn’t accessible to the web in your app configuration:
-
Create a symbolic link from the config file the application wants to a location in that mount:
This example assumes the app wants a
db.yamlfile in its root for configuration. -
Commit the symbolic link and an empty
configdirectory to Git. -
Configure a script to read from environment variables and write to
config/db.yamlthrough the service environment variables themselves, or through thePLATFORM_RELATIONSHIPSenvironment variable.
Create a file with a shell script similar to this:
- Service environment variables
- `PLATFORM_RELATIONSHIPS` environment variable
export-config.sh
- Call the script from the
deployhook your app configuration:
db.yaml, the symbolic link redirects it to config/db.yaml.
Your script writes to that file on each deploy with updated information.
Your app reads the exported values and proceeds as expected.