The env module provides functions for getting and setting environment variables on the host machine.

Allowed in

  • build
  • deploy
  • destroy
  • task
  • trigger_sync
  • policy

Usage

Load the module:

load("github.com/ocuroot/sdk/v0/env.star", "env")

Example

# Get environment variable with default value
api_key = env.get("API_KEY", "default-key")
print(api_key)

# Get required environment variable
database_url = env.get("DATABASE_URL")
if not database_url:
    fail("DATABASE_URL environment variable is required")

API Reference

get(key, default=None)

Returns the value of the environment variable key.

Arguments

  • key: The name of the environment variable
  • default: Optional default value to return if the environment variable is not set

Returns

  • Value of the environment variable if set, otherwise the default value

Stubs

# env is a Starlark module that provides functions for getting and setting 
# environment variables on the host machine.

# make_env returns a struct with get and set methods
# that can be used to get and set environment variables
def make_env():
    def get(key, default=None):
        """
        Returns the value of the environment variable `key`.

        Args:
            key: The name of the environment variable.
            default: The default value to return if the environment variable is not set.
        """
        pass

    return struct(
        get = get,
    )

# env is the importable struct
env = make_env()