How to Use Environment Variables in App Platform

Define Runtime Variables During App Creation

When creating an app, click Advanced during the usual app creation process to access environment variable functionality. Click the checkbox labeled Secret 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

Go to https://cloud.digitalocean.com/apps, click on your app, and click on the Components tab. Click on 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, you will need to use an App Specification and create a variable with the scope set to BUILD_TIME.

Here's an example of build time and runtime environment variables being defined.

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.

Using Bindable Variables within Environment Variables

Bindable variables allow environment variables to reference dynamic app specific values on build and deploy.

App-Wide Variables

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

  • APP_DOMAIN: application primary domain
  • APP_URL: application 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
  • 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: routable path used for this service publicly
  • 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.

  • 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.