Environment Variables

Manage environment variables and state in GitHub Actions workflows.

API Reference

get_workflow_environment_variables()

Gets all environment variables from the GITHUB_ENV environment file which is available to the workflow. GitHub Actions Docs: set_env

example:

>> from github_action_toolkit import get_workflow_environment_variables

>> get_workflow_environment_variables()

# Output:
# {"my_env": "test value"}

get_env(name)

Gets all environment variables from os.environ or the GITHUB_ENV environment file which is available to the workflow. This can also be used to get environment variables set by GitHub Actions. GitHub Actions Docs: set_env

example:

>> from github_action_toolkit import get_env

>> get_env("my_env")
>> get_env("GITHUB_API_URL")

# Output:
# test value
# https://api.github.com

set_env(name, value)

Creates an environment variable by writing this to the GITHUB_ENV environment file which is available to any subsequent steps in a workflow job. GitHub Actions Docs: set_env

example:

>> from github_action_toolkit import set_env

>> set_env("my_env", "test value")

export_variable(name, value)

Sets an environment variable for your workflows (alias for set_env). This matches the naming convention from the Javascript @actions/toolkit.

example:

>> from github_action_toolkit import export_variable

>> export_variable("BUILD_NUMBER", "123")
>> export_variable("DEPLOY_ENV", "production")

with_env(**env_vars)

Context manager for temporarily setting environment variables. Variables are automatically restored to their original values (or removed if they didn’t exist) when the context exits.

example:

>> from github_action_toolkit import with_env
>> import os

>> # Temporarily set environment variables
>> with with_env(MY_VAR="value", ANOTHER="test"):
>>     print(os.environ["MY_VAR"])  # "value"
>>     print(os.environ["ANOTHER"])  # "test"
>> # Variables are restored here
>> print(os.environ.get("MY_VAR"))  # None (if it didn't exist before)