Skip to content

feat(cli): add --env and --env-file for local runtime env vars - #440

Open
mercael91 wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
mercael91:feat/add-env-env-file-cli-support
Open

feat(cli): add --env and --env-file for local runtime env vars#440
mercael91 wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
mercael91:feat/add-env-env-file-cli-support

Conversation

@mercael91

Copy link
Copy Markdown

Summary

Adds two new CLI options to the functions-framework entry point for passing runtime environment variables during local testing.

Changes

  • --env KEY=VALUE (repeatable): Set individual runtime environment variables before the function module is loaded.
  • --env-file PATH: Load runtime environment variables from a .env file. Supports comments (#), blank lines, and quoted values.

Behavior

  • Values from --env-file are applied first, then any --env flags override them.
  • This allows developers to maintain a base .env file while selectively overriding specific variables for different test scenarios.

.env file support

  • Comment lines (starting with #)
  • Blank lines
  • Double-quoted and single-quoted values
  • Values with leading/trailing whitespace (trimmed)

Usage examples

# Inline env vars
functions-framework --target=my_func --env DB_HOST=localhost --env DB_PORT=5432

# From a .env file
functions-framework --target=my_func --env-file .env.local

# File + overrides
functions-framework --target=my_func --env-file .env --env DEBUG=true

Tests

6 new tests covering:

  • --env sets environment variables
  • --env-file loads from file (with comments, quotes, whitespace)
  • --env overrides --env-file values
  • Invalid --env format error
  • Missing --env-file error
  • Invalid --env-file line format error

All 18 tests pass.

Fixes #215

CLA: signed

Add two new CLI options to the functions-framework entry point:

- --env KEY=VALUE (repeatable): set individual runtime environment
  variables before the function module is loaded.
- --env-file PATH: load runtime environment variables from a .env
  file. Supports comments (#), blank lines, and quoted values.

Values from --env-file are applied first, then any --env flags
override them. This allows developers to maintain a base .env file
while selectively overriding specific variables for different test
scenarios.

The .env parser handles:
- Comment lines (starting with #)
- Blank lines
- Double-quoted and single-quoted values
- Values with leading/trailing whitespace (trimmed)

Fixes GoogleCloudPlatform#215

Signed-off-by: mercael91 <mercael91@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to pass runtime environment variables for local testing

1 participant