How to Use Environment Variables in App Platform

Environment variables in App Platform provide a way to store environment information at the container or application levels that your app can access at build or run times. We also provide many bindable app-specific and component level variables, such as the app’s URL or database CA certificate values.

Define Runtime Variables During App Creation

When creating an app, click Edit to the right of Environment Variables during the usual app creation process to access environment variable functionality. Click the Encrypt checkbox to obscure the variable’s values from all build, deployment, and application logs.

You can also declare runtime variables in the app specification for your app by setting the scope to RUN_TIME. See the example below or the app specification reference for more details.

Define Runtime Variables After App Creation

In the the Apps section of the DigitalOcean Control Panel, click your app, then click the Settings tab. Click the component for which you’d like to provide runtime variables. Click the checkbox labeled Encrypt to obscure the variable’s values from all build, deployment, and application logs.

Define Build Time Environment Variables

To define environment variables that will be accessible at build time, define the variable in the app’s specification with the scope set to BUILD_TIME.

Here’s an example of build time and runtime environment variables in a specification.

services:
  - name: web
    instance_count: 4
    instance_size_slug: professional-xxl
    routes:
      - path: /
    git:
      repo: "https://github.com/example/repo"
      branch: master
    build_command: "go build ./"
    run_command: "/app/server"
    envs:
    - key: BUILD_ONLY_VAR
      scope: BUILD_TIME
      value: hello world
    - key: RUNTIME_ONLY_VAR
      scope: RUN_TIME
      value: i'm a little teapot

See the app specification reference for more details.

Define App-Level Environment Variables

Unlike runtime and build time variables which are component-specific, app-level variables can be accessed by all components in your app. App-level variables are available at both build and run time. If any component-level environment variables (such as runtime or build time variables) have the same name as an app-level environment variable, the component-level variable “wins” as it is considered more specific.

To specify app-level variables, go to the Apps section of the DigitalOcean Control Panel. Click your app, then click the Settings tab. Next to the App-Level Environment Variables heading, click the Edit link.

Encrypt Environment Variables

Click the Encrypt checkbox next to any variable to prevent its value from appearing in clear text in logging data.

Using Bindable Variables within Environment Variables

Bindable variables allow environment variables to reference dynamic app-specific values on build and deploy. These values are provided by DigitalOcean and are app wide or component specific.

To set a bindable variable as an environment variable, go to the Apps section of the DigitalOcean Control Panel. Click your app, then click the Settings tab. Next to the App-Level Environment Variables heading, click the Edit link.

In the KEYS field, enter a unique name for the new environment variable. In the Values field, enter the bindable variable you would like to reference.

For example, to set the app’s URL as an environment variable, set the KEY value to URL and its value to ${APP_URL}.

Bindable variables set as environmental variables

Once you enter the bindable variable, click Save. The app redeploys.

You can check that the environment variable works by opening the app’s Console tab and running:

echo $<name-of-environment-variable>
Console echo command running inside of it.

App-Wide Variables

These variables can be indicated in the format of ${BINDABLE_NAME}.

  • ${APP_DOMAIN}: Application’s primary domain.
  • ${APP_URL}: Application’s primary domain in http format (e.g. https://my-domain.com).

Component-Specific Variables

Component specific variables need to be prefixed by the component name, for example ${my-service.BINDABLE_NAME}. The _self prefix can be used to reference the current component, for example ${_self.BINDABLE_NAME}.

Services

  • ${PRIVATE_DOMAIN}: Internally used domain name used for communication between mulitple services.
  • ${PRIVATE_URL}: Internally used domain in HTTP format suffixed with the port.
  • ${PRIVATE_PORT}: Internally used HTTP port.
  • ${COMMIT_HASH}: git commit hash used for this build.
  • ${PUBLIC_ROUTE_PATH}: Routable path used for this service publicly.
  • ${PUBLIC_URL}: public url of this component in http format including the public route.

Static Sites

  • ${COMMIT_HASH}: git commit hash used for this build.
  • ${PUBLIC_ROUTE_PATH}: Public routable path used for this service.
  • ${PUBLIC_URL}: Public url of this component in HTTP format, including the public route.

Workers

  • ${COMMIT_HASH}: git commit hash used for this build.

Databases

Database values are not available during build time but are available at runtime.

  • ${HOSTNAME}: Fully qualified host name to the database.
  • ${PORT}: Port to the database.
  • ${USERNAME}: Username for the database.
  • ${PASSWORD}: Password for the database user.
  • ${DATABASE}: The database name used by the user.
  • ${DATABASE_URL}: The database connection string.
  • ${JDBC_DATABASE_URL}: The database connection string prefixed with jdbc: for Java.
  • ${REDIS_URL}: The Redis connection string (Redis only).
  • ${CA_CERT}: CA certificate for the managed databases in the users account.
Environment variables are in-memory key/value pairs that store important configuration details about an app.