Skip to content

Commit 5c715f9

Browse files
authored
Stack: detection of nested stacks (#4052)
* Netsted stack detection fixes * Add test for multiple stacks generation * Documentation update * Docs sync * Markdown update * fixed lint issues * Removed wrong character
1 parent 29de7b2 commit 5c715f9

8 files changed

Lines changed: 73 additions & 5 deletions

File tree

config/stack.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ func GenerateStacks(ctx context.Context, opts *options.TerragruntOptions) error
6464
// stop worker pool on exit
6565
defer wp.Stop()
6666
// initial files setting as stack file
67-
foundFiles := []string{opts.TerragruntStackConfigPath}
67+
68+
foundFiles, err := listStackFiles(opts, opts.WorkingDir)
69+
if err != nil {
70+
return errors.Errorf("Failed to list stack files in %s %v", opts.WorkingDir, err)
71+
}
6872

6973
for {
7074
// check if we have already processed the files

docs-starlight/src/data/commands/stack/generate.mdx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ examples:
1616
terragrunt stack generate
1717
---
1818

19-
import { FileTree } from '@astrojs/starlight/components';
19+
import { Aside, FileTree } from '@astrojs/starlight/components';
2020

2121
## Generating a stack
2222

@@ -67,3 +67,13 @@ Generates the following stack:
6767
- terragrunt.hcl
6868

6969
</FileTree>
70+
71+
<Aside type="note">
72+
Parallel Execution: Stack generation runs concurrently to improve performance. The number of parallel tasks is determined by the `GOMAXPROCS` environment variable and can be explicitly controlled using the `--parallelism` flag:
73+
74+
```bash
75+
terragrunt stack generate --parallelism 4
76+
```
77+
78+
Automatic Discovery: The command automatically discovers all `terragrunt.stack.hcl` files within the directory structure and generates them in parallel.
79+
</Aside>

docs/_docs/04_reference/02-cli-options.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,16 @@ Will create the following directory structure:
338338
└── terragrunt.hcl
339339
```
340340

341-
**Note**: Stack generation is executed in parallel. By default, the number of parallel tasks is limited by the `GOMAXPROCS` environment variable.
342-
You can override this limit using the `--parallelism` CLI flag:
341+
**Notes**:
342+
343+
- Parallel Execution: Stack generation runs concurrently to improve performance. The number of parallel tasks is determined by the `GOMAXPROCS` environment variable and can be explicitly controlled using the `--parallelism` flag:
343344

344345
```bash
345-
terragrunt stack generate --parallelism 4
346+
terragrunt stack generate --parallelism 4
346347
```
347348

349+
- Automatic Discovery: The command automatically discovers all `terragrunt.stack.hcl` files within the directory structure and generates them in parallel.
350+
348351
#### stack run
349352

350353
The `stack run *` command allows users to execute IaC commands across all units defined in a `terragrunt.stack.hcl` file.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
unit "v2-unit1" {
2+
source = "../unit"
3+
path = "unit1"
4+
}
5+
6+
unit "v2-unit2" {
7+
source = "../unit"
8+
path = "unit2"
9+
}
10+
11+
unit "v2-unit3" {
12+
source = "../unit"
13+
path = "unit3"
14+
}
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
unit "v1-unit1" {
2+
source = "../unit"
3+
path = "unit1"
4+
}
5+
6+
unit "v1-unit2" {
7+
source = "../unit"
8+
path = "unit2"
9+
}
10+
11+
unit "v1-unit3" {
12+
source = "../unit"
13+
path = "unit3"
14+
}
15+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
output "output" {
3+
value = "unit"
4+
}

test/fixtures/stacks/multiple-stacks/unit/terragrunt.hcl

Whitespace-only changes.

test/integration_stacks_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
testFixtureNoStack = "fixtures/stacks/no-stack"
3737
testFixtureStackCycles = "fixtures/stacks/errors/cycles"
3838
testFixtureNoStackNoDir = "fixtures/stacks/no-stack-dir"
39+
testFixtureMultipleStacks = "fixtures/stacks/multiple-stacks"
3940
)
4041

4142
func TestStacksGenerateBasic(t *testing.T) {
@@ -892,6 +893,22 @@ func TestStacksNoStackDirNoTerragruntStackDirectoryCreated(t *testing.T) {
892893
assert.NoDirExists(t, path)
893894
}
894895

896+
func TestStacksGenerateMultipleStacks(t *testing.T) {
897+
t.Parallel()
898+
899+
helpers.CleanupTerraformFolder(t, testFixtureMultipleStacks)
900+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureMultipleStacks)
901+
rootPath := util.JoinPath(tmpEnvPath, testFixtureMultipleStacks)
902+
903+
helpers.RunTerragrunt(t, "terragrunt stack generate --experiment stacks --terragrunt-working-dir "+rootPath)
904+
905+
devStack := util.JoinPath(rootPath, "dev", ".terragrunt-stack")
906+
validateStackDir(t, devStack)
907+
908+
liveStack := util.JoinPath(rootPath, "live", ".terragrunt-stack")
909+
validateStackDir(t, liveStack)
910+
}
911+
895912
// validateNoStackDirs check if the directories outside of stack are created and contain test files
896913
func validateNoStackDirs(t *testing.T, rootPath string) {
897914
t.Helper()

0 commit comments

Comments
 (0)