Skip to content

Commit

Permalink
feat: Adding TG_CTX_TF_PATH to hook context (#3323)
Browse files Browse the repository at this point in the history
  • Loading branch information
yhakbar authored Aug 6, 2024
1 parent 032afde commit c89bba5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cli/commands/terraform/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

const (
HookCtxTFPathEnvName = "TG_CTX_TF_PATH"
HookCtxCommandEnvName = "TG_CTX_COMMAND"
HookCtxHookNameEnvName = "TG_CTX_HOOK_NAME"
)
Expand Down Expand Up @@ -180,6 +181,7 @@ func executeTFLint(ctx context.Context, terragruntOptions *options.TerragruntOpt
func terragruntOptionsWithHookEnvs(opts *options.TerragruntOptions, hookName string) *options.TerragruntOptions {
newOpts := *opts
newOpts.Env = util.CloneStringMap(opts.Env)
newOpts.Env[HookCtxTFPathEnvName] = opts.TerraformPath
newOpts.Env[HookCtxCommandEnvName] = opts.TerraformCommand
newOpts.Env[HookCtxHookNameEnvName] = hookName

Expand Down
5 changes: 3 additions & 2 deletions docs/_docs/02_features/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ In this example configuration, whenever Terragrunt runs `tofu apply` or `tofu pl

Any type of hook adds extra environment variables to the hook's run command:

- `TG_CTX_TF_PATH`
- `TG_CTX_COMMAND`
- `TG_CTX_HOOK_NAME`

Expand All @@ -58,10 +59,10 @@ terraform {
``` bash
#!/bin/sh

echo "COMMAND=${TG_CTX_COMMAND} HOOK_NAME=${TG_CTX_HOOK_NAME}"
echo "TF_PATH=${TG_CTX_TF_PATH} COMMAND=${TG_CTX_COMMAND} HOOK_NAME=${TG_CTX_HOOK_NAME}"
```

In this example, whenever Terragrunt runs `tofu apply`/`terraform apply`, the `hook.sh` script will print "COMMAND=apply HOOK_NAME=test_hook"
In this example, whenever Terragrunt runs `tofu apply`/`terraform apply`, the `hook.sh` script will print "TF_PATH=tofu COMMAND=apply HOOK_NAME=test_hook"

You can have multiple before and after hooks. Each hook will execute in the order they are defined. For example:

Expand Down
2 changes: 1 addition & 1 deletion test/fixture-hooks/before-and-after/hook.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

echo "COMMAND=${TG_CTX_COMMAND} HOOK_NAME=${TG_CTX_HOOK_NAME}"
echo "TF_PATH=${TG_CTX_TF_PATH} COMMAND=${TG_CTX_COMMAND} HOOK_NAME=${TG_CTX_HOOK_NAME}"
3 changes: 2 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,8 @@ func TestTerragruntBeforeAndAfterHook(t *testing.T) {

assert.Equal(t, 1, strings.Count(output, "AFTER_TERRAGRUNT_READ_CONFIG"), "Hooks on terragrunt-read-config command executed more than once")

assert.Equal(t, 1, strings.Count(output, "COMMAND=terragrunt-read-config HOOK_NAME=after_hook_3"))
expectedHookOutput := fmt.Sprintf("TF_PATH=%s COMMAND=terragrunt-read-config HOOK_NAME=after_hook_3", wrappedBinary())
assert.Equal(t, 1, strings.Count(output, expectedHookOutput))

assert.NoError(t, beforeException)
assert.NoError(t, afterException)
Expand Down

0 comments on commit c89bba5

Please sign in to comment.