diff --git a/config/config_helpers.go b/config/config_helpers.go index 15fddfd1f3..8fa36af8ff 100644 --- a/config/config_helpers.go +++ b/config/config_helpers.go @@ -284,16 +284,26 @@ func runCommand(args []string, trackInclude *TrackInclude, terragruntOptions *op } suppressOutput := false - if args[0] == "--terragrunt-quiet" { - suppressOutput = true - args = append(args[:0], args[1:]...) - } - currentPath := filepath.Dir(terragruntOptions.TerragruntConfigPath) + cachePath := currentPath + + checkOptions := true + for checkOptions && len(args) > 0 { + switch args[0] { + case "--terragrunt-quiet": + suppressOutput = true + args = append(args[:0], args[1:]...) + case "--terragrunt-global-cache": + cachePath = "_global_" + args = append(args[:0], args[1:]...) + default: + checkOptions = false + } + } // To avoid re-run of the same run_cmd command, is used in memory cache for command results, with caching key path + arguments // see: https://github.com/gruntwork-io/terragrunt/issues/1427 - cacheKey := fmt.Sprintf("%v-%v", currentPath, args) + cacheKey := fmt.Sprintf("%v-%v", cachePath, args) cachedValue, foundInCache := runCommandCache.Get(cacheKey) if foundInCache { if suppressOutput { diff --git a/config/config_helpers_test.go b/config/config_helpers_test.go index 022b88783e..3d398901d2 100644 --- a/config/config_helpers_test.go +++ b/config/config_helpers_test.go @@ -188,6 +188,24 @@ func TestRunCommand(t *testing.T) { "foo", nil, }, + { + []string{"--terragrunt-global-cache", "/bin/bash", "-c", "echo foo"}, + terragruntOptionsForTest(t, homeDir), + "foo", + nil, + }, + { + []string{"--terragrunt-global-cache", "--terragrunt-quiet", "/bin/bash", "-c", "echo foo"}, + terragruntOptionsForTest(t, homeDir), + "foo", + nil, + }, + { + []string{"--terragrunt-quiet", "--terragrunt-global-cache", "/bin/bash", "-c", "echo foo"}, + terragruntOptionsForTest(t, homeDir), + "foo", + nil, + }, { nil, terragruntOptionsForTest(t, homeDir), diff --git a/docs/_docs/04_reference/built-in-functions.md b/docs/_docs/04_reference/built-in-functions.md index 96ba34cda2..640c9b47ac 100644 --- a/docs/_docs/04_reference/built-in-functions.md +++ b/docs/_docs/04_reference/built-in-functions.md @@ -636,7 +636,7 @@ remote_state { } ``` -If the command you are running has the potential to output sensitive values, you may wish to redact the output from appearing in the terminal. To do so, use the special `--terragrunt-quiet` argument which must be passed as the first argument to `run_cmd()`: +If the command you are running has the potential to output sensitive values, you may wish to redact the output from appearing in the terminal. To do so, use the special `--terragrunt-quiet` argument which must be passed as one of the first arguments to `run_cmd()`: ``` hcl super_secret_value = run_cmd("--terragrunt-quiet", "./decrypt_secret.sh", "foo") @@ -685,6 +685,12 @@ carrot * Output contains multiple times `uuid3`, +1 more output comparing to `uuid1` and `uuid2` - because `uuid3` is declared in locals and inputs which add one more evaluation * Output contains only once `uuid4` since it is declared only once in `inputs`, `inputs` is not evaluated twice +You can modify this caching behavior to ignore the existing directory if you know the command you are running is not dependent on the current directory path. To do so, use the special `--terragrunt-global-cache` argument which must be passed as one of the first arguments to `run_cmd()` (and can be combined with `--terragrunt-quiet` in any order): + +``` hcl +value = run_cmd("--terragrunt-global-cache", "--terragrunt-quiet", "/usr/local/bin/get-account-map") +``` + ## read\_terragrunt\_config `read_terragrunt_config(config_path, [default_val])` parses the terragrunt config at the given path and serializes the