Skip to content

Commit 102d878

Browse files
authored
Document how to set env vars from hook scripts (#63)
1 parent d4f90d4 commit 102d878

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

website/docs/cloud-docs/agents/hooks.mdx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,31 @@ Please note the following behavior when using hooks:
4545
- Each hook has a 60 second timeout. If a hook times out the Terraform run will
4646
fail immediately.
4747
- Environment variables do not persist across hooks. For example, if a
48-
`pre-plan` hook exports environment variables, they will not be available
49-
during `terraform plan` or during the `post-plan` hook. Similarly, if
50-
`terraform plan` exports environment variables, they will not be available
51-
during the `post-plan` hook.
48+
`pre-plan` hook exports environment variables, they will not be available
49+
during the `post-plan` hook. Similarly, if `terraform plan` exports
50+
environment variables, they will not be available during the `post-plan` hook.
51+
52+
## Setting environment variables
53+
54+
Hooks may be used to set environment variables for subsequent `terraform`
55+
commands to use. To set an environment variable, write lines in `KEY=value`
56+
format to the file pointed to by the `$TFC_AGENT_ENV` environment variable.
57+
Newlines are used to separate multiple environment variables. For example, to
58+
set the `FOO` and `BAR` environment variables for the `terraform plan` operation
59+
to consume, write the following in a `pre-plan` hook script:
60+
61+
```
62+
#!/bin/sh
63+
echo FOO=hello >> $TFC_AGENT_ENV
64+
echo BAR=world >> $TFC_AGENT_ENV
65+
```
66+
67+
Environment variables set by a hook script in this way are only applicable to
68+
the current operation, and are not persisted for later operations. For example,
69+
if `FOO=bar` is set in the `pre-plan` hook, then `FOO=bar` will be set during
70+
the `terraform plan` command, but will not be set during `terraform apply`. To
71+
use the variable during the apply phase, write the variable to the
72+
`$TFC_AGENT_ENV` file again in the `pre-apply` hook script.
5273

5374
## Configuration
5475

0 commit comments

Comments
 (0)