Environment variables

Set configuration and secrets for a Hangar service, and share values across services with project and environment variables.

Updated

Configuration reaches your app as environment variables. Each service has its own set, edited in its Variables tab as KEY=value lines.

Variables are stored encrypted, and values stay masked on screen until you choose to reveal them.

Hangar also sets PORT on every service, to the container port of its first domain, unless you set it. See Domains and HTTPS.

Three scopes

ScopeBelongs toUse it for
ServiceOne serviceSettings only that service needs
EnvironmentOne environment of a project, such as productionValues every service in that environment shares, such as a database URL
ProjectThe whole projectValues that are the same in every environment

References

Project and environment variables are not injected into services on their own. A service takes one by referencing it:

DATABASE_URL=${{environment.DATABASE_URL}}
SENTRY_DSN=${{project.SENTRY_DSN}}

${{environment.KEY}} reads a variable of the service's environment. ${{project.KEY}} reads a project variable.

This keeps each value in one place. Rotate a database password by changing the environment variable once, and every service that references it picks up the new value on its next deploy. It also means staging and production can use the same service configuration while pointing at different databases.

When changes apply

Variables are read when a service starts. After changing one, redeploy the service for it to take effect.

Build-time values

Some frameworks read variables during the build, not only at runtime; public keys embedded in a frontend bundle are the usual case. Set those as build arguments in the service's environment settings, so they are available while the image is built.

Per environment

Each environment has its own variables. Production and staging can run the same code with different credentials, and a preview deployment of a pull request gets its own set, so a preview never touches production data.

Deploy your app on Hangar

Connect a repository and it is live in minutes.

Start deploying