Skip to main content
env(name) reads the environment variable name and returns its value. An optional second argument provides a fallback when the variable is not set.

Signature

env(name string) string
env(name string, default string) string

Example

Configure the deployment target from the CI environment, with a safe fallback for local development:
computed "registry" {
  description = "Container registry from the CI environment"
  expression  = env("CI_REGISTRY", "localhost:5000")
}

computed "registry_user" {
  description = "Registry username from the CI environment"
  expression  = env("CI_REGISTRY_USER", "developer")
}

task "push" {
  description = "Push the image to the registry"
  commands = [
    "docker push ${computed.registry}/${var.app}:${var.tag}",
  ]
}
On CI, CI_REGISTRY is set to the real registry. Locally, images are pushed to localhost:5000.

Notes

  • When called with one argument and the variable is not set, env returns an empty string "".
  • Use a default to avoid empty strings in commands or expressions that require a value.
  • env reads the process environment at evaluation time. Changes to the environment after the playbook starts are not visible.