Skip to content

Environment variables

Environment variables play a crucial role in configuring and customizing Terrateam workflows and hooks. Terrateam provides a set of built-in environment variables that you can use to access information about the current workflow execution context and the results of Terrateam operations. Additionally, you can define your own custom environment variables to store and pass data between different steps and stages of your Terrateam pipelines.

Built-in environment variables

Terrateam provides the following built-in environment variables:

Workflow environment variables

NameDescription
TERRATEAM_PLAN_FILEThe path to the generated Terraform plan file.
TERRATEAM_DIRThe working directory relative to the root of the repository.
TERRATEAM_WORKSPACEThe Terraform workspace being executed against.
TERRATEAM_ROOTThe absolute path to the root of your checked-out repository.

Post-Hook environment variables

NameDescription
TERRATEAM_RESULTS_FILEThe path to a JSON file that contains the results of all executed Dirspaces.

GitHub Actions environment variables

In addition to the Terrateam-specific environment variables, you also have access to the default environment variables provided by GitHub Actions. Refer to the GitHub documentation for a complete list of available variables.

Custom environment variables

Terrateam allows you to define your own custom environment variables in your workflows and hooks. You can use custom environment variables to store and pass data between different steps, set configuration options, or provide dynamic values based on the execution context.

Defining custom environment variables in workflows

To define custom environment variables in your Terrateam workflows, you can use the env step type. Here’s an example workflow configuration that sets a custom environment variable:

workflows:
- tag_query: ""
plan:
- type: env
name: MY_CUSTOM_VAR
cmd: ['echo', 'Hello, World!']
- type: run
cmd: ['echo', 'The value of MY_CUSTOM_VAR is: $MY_CUSTOM_VAR']

In this example, the env step sets a custom environment variable named MY_CUSTOM_VAR with the value “Hello, World!“. The subsequent run step can then access the value of MY_CUSTOM_VAR using the $ prefix.

Defining custom environment variables in hooks

Similarly, you can define custom environment variables in your Terrateam hooks using the env hook type. Here’s an example hook configuration that sets a custom environment variable:

hooks:
plan:
pre:
- type: env
name: MY_HOOK_VAR
cmd: ['echo', 'Hello from the pre-plan hook!']
post:
- type: run
cmd: ['echo', 'The value of MY_HOOK_VAR is: $MY_HOOK_VAR']

In this example, the env hook in the pre section of the plan hook sets a custom environment variable named MY_HOOK_VAR with the value “Hello from the pre-plan hook!“. The run hook in the post section can then access the value of MY_HOOK_VAR.

Using custom environment variables

Once you have defined your custom environment variables, you can use them in your Terrateam configurations and scripts by referencing them with the $ prefix followed by the variable name. For example:

workflows:
- tag_query: ""
plan:
- type: run
cmd: ['echo', 'The value of MY_CUSTOM_VAR is: $MY_CUSTOM_VAR']

In this example, the $MY_CUSTOM_VAR variable will be expanded to the actual value of the MY_CUSTOM_VAR environment variable during execution.

Best practices

When using environment variables, keep the following best practices in mind:

  • Use meaningful and descriptive names for your own custom environment variables to avoid conflicts with Terrateam’s built-in variables or other system variables.
  • Be cautious when using environment variables to store sensitive information, such as secrets or credentials. Instead, consider using secure mechanisms like Terrateam’s integration with GitHub Secrets or a secrets management system.
  • Ensure that your scripts and configurations handle missing or empty environment variables gracefully to avoid unexpected errors or behavior.
We use cookies and similar technologies to provide certain features, enhance the user experience and deliver content that is relevant to your interests. Depending on their purpose, analysis and marketing cookies may be used in addition to technically necessary cookies. By clicking on "Agree and continue", you declare your consent to the use of the aforementioned cookies. Here you can make detailed settings or revoke your consent (in part if necessary) with effect for the future. For further information, please refer to our Privacy Policy .