Skip to content

Commit b094bc4

Browse files
authored
feat: Supporting no_run in exclude block (#4574)
1 parent 5ed5a7a commit b094bc4

File tree

14 files changed

+157
-8
lines changed

14 files changed

+157
-8
lines changed

cli/commands/run/run.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,12 @@ func runTerragruntWithConfig(
308308
r *report.Report,
309309
target *Target,
310310
) error {
311-
// Add extra_arguments to the command
311+
if cfg.Exclude != nil && cfg.Exclude.ShouldPreventRun(opts.TerraformCommand) {
312+
l.Infof("Early exit in terragrunt unit %s due to exclude block with no_run = true", opts.WorkingDir)
313+
314+
return nil
315+
}
316+
312317
if cfg.Terraform != nil && cfg.Terraform.ExtraArgs != nil && len(cfg.Terraform.ExtraArgs) > 0 {
313318
args := FilterTerraformExtraArgs(l, opts, cfg)
314319

config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ func (cfg *TerragruntConfig) WriteTo(w io.Writer) (int64, error) {
491491
excludeBody.SetAttributeValue("actions", excludeAsCty.GetAttr("actions"))
492492
}
493493

494+
if cfg.Exclude.NoRun != nil {
495+
excludeBody.SetAttributeValue("no_run", excludeAsCty.GetAttr("no_run"))
496+
}
497+
494498
excludeBody.SetAttributeValue("if", excludeAsCty.GetAttr("if"))
495499

496500
rootBody.AppendBlock(excludeBlock)

config/exclude.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ const (
1717
)
1818

1919
// bool values to be used as booleans.
20-
var boolFlagValues = []string{"if", "exclude_dependencies"}
20+
var boolFlagValues = []string{"if", "exclude_dependencies", "no_run"}
2121

2222
// ExcludeConfig configurations for hcl files.
2323
type ExcludeConfig struct {
2424
ExcludeDependencies *bool `cty:"exclude_dependencies" hcl:"exclude_dependencies,attr" json:"exclude_dependencies"`
25+
NoRun *bool `cty:"no_run" hcl:"no_run,attr" json:"no_run"`
2526
Actions []string `cty:"actions" hcl:"actions,attr" json:"actions"`
2627
If bool `cty:"if" hcl:"if,attr" json:"if"`
2728
}
@@ -49,12 +50,18 @@ func (e *ExcludeConfig) IsActionListed(action string) bool {
4950
return false
5051
}
5152

53+
// ShouldPreventRun checks if the unit should be prevented from running based on the no_run attribute and current action.
54+
func (e *ExcludeConfig) ShouldPreventRun(action string) bool {
55+
return e.NoRun != nil && *e.NoRun && e.If && e.IsActionListed(action)
56+
}
57+
5258
// Clone returns a new instance of ExcludeConfig with the same values as the original.
5359
func (e *ExcludeConfig) Clone() *ExcludeConfig {
5460
return &ExcludeConfig{
5561
If: e.If,
5662
Actions: e.Actions,
5763
ExcludeDependencies: e.ExcludeDependencies,
64+
NoRun: e.NoRun,
5865
}
5966
}
6067

@@ -67,6 +74,7 @@ func (e *ExcludeConfig) Merge(exclude *ExcludeConfig) {
6774
}
6875

6976
e.ExcludeDependencies = exclude.ExcludeDependencies
77+
e.NoRun = exclude.NoRun
7078
}
7179

7280
// evaluateExcludeBlocks evaluates the exclude block in the parsed file.

docs-starlight/src/content/docs/04-reference/01-hcl/02-blocks.mdx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,9 +1488,10 @@ Syntax:
14881488
# terragrunt.hcl
14891489
14901490
exclude {
1491-
if = <boolean expression> # Boolean expression to determine exclusion.
1492-
actions = ["<action>", ...] # List of actions to exclude (e.g., "plan", "apply", "all", "all_except_output").
1493-
exclude_dependencies = <boolean> # Boolean to determine if dependencies should also be excluded.
1491+
if = <boolean> # Boolean to determine exclusion.
1492+
no_run = <boolean> # Boolean to prevent the unit from running (even when not using `--all`).
1493+
actions = ["<action>", ...] # List of actions to exclude (e.g., "plan", "apply", "all", "all_except_output").
1494+
exclude_dependencies = <boolean> # Boolean to determine if dependencies should also be excluded.
14941495
}
14951496
```
14961497

@@ -1501,6 +1502,7 @@ Attributes:
15011502
| `if` | boolean | Condition to dynamically determine whether the unit should be excluded. |
15021503
| `actions` | list(string) | Specifies which actions to exclude when the condition is met. Options: `plan`, `apply`, `all`, `all_except_output` etc. |
15031504
| `exclude_dependencies` | boolean | Indicates whether the dependencies of the excluded unit should also be excluded (default: `false`). |
1505+
| `no_run` | boolean | When `true` and `if` is `true`, prevents the unit from running entirely for both single unit commands and `run --all` commands, but only when the current action matches the `actions` list. |
15041506

15051507
Examples:
15061508

@@ -1542,6 +1544,18 @@ exclude {
15421544

15431545
This setup is useful for scenarios where output evaluation is still needed, even if other actions like `plan` or `apply` are excluded.
15441546

1547+
```hcl
1548+
# terragrunt.hcl
1549+
1550+
exclude {
1551+
if = true
1552+
no_run = true
1553+
actions = ["plan"]
1554+
}
1555+
```
1556+
1557+
This configuration prevents the unit from running when `if` is `true` AND the current action is "plan". The `no_run` attribute works for both single unit commands (like `terragrunt plan`) and `run --all` commands (like `terragrunt run --all plan`), but only when the current action matches the `actions` list.
1558+
15451559
Consider using this for units that are expensive to continuously update, and can be opted in when necessary.
15461560

15471561
## errors

docs/_docs/04_reference/04-config-blocks-and-attributes.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,9 +1494,10 @@ Syntax:
14941494

14951495
```hcl
14961496
exclude {
1497-
if = <boolean expression> # Boolean expression to determine exclusion.
1498-
actions = ["<action>", ...] # List of actions to exclude (e.g., "plan", "apply", "all", "all_except_output").
1499-
exclude_dependencies = <boolean> # Boolean to determine if dependencies should also be excluded.
1497+
if = <boolean> # Boolean to determine exclusion.
1498+
no_run = <boolean> # Boolean to prevent the unit from running (even when not using `--all`).
1499+
actions = ["<action>", ...] # List of actions to exclude (e.g., "plan", "apply", "all", "all_except_output").
1500+
exclude_dependencies = <boolean> # Boolean to determine if dependencies should also be excluded.
15001501
}
15011502
```
15021503

@@ -1507,6 +1508,7 @@ Attributes:
15071508
| `if` | boolean | Condition to dynamically determine whether the unit should be excluded. |
15081509
| `actions` | list(string) | Specifies which actions to exclude when the condition is met. Options: `plan`, `apply`, `all`, `all_except_output` etc. |
15091510
| `exclude_dependencies` | boolean | Indicates whether the dependencies of the excluded unit should also be excluded (default: `false`). |
1511+
| `no_run` | boolean | When `true` and `if` is `true`, prevents the unit from running entirely for both single unit commands and `run --all` commands, but only when the current action matches the `actions` list. |
15101512

15111513
Examples:
15121514

@@ -1542,6 +1544,16 @@ exclude {
15421544

15431545
This setup is useful for scenarios where output evaluation is still needed, even if other actions like `plan` or `apply` are excluded.
15441546

1547+
```hcl
1548+
exclude {
1549+
if = true
1550+
no_run = true
1551+
actions = ["plan"]
1552+
}
1553+
```
1554+
1555+
This configuration prevents the unit from running when `if` is `true` AND the current action is "plan". The `no_run` attribute works for both single unit commands (like `terragrunt plan`) and `run --all` commands (like `terragrunt run --all plan`), but only when the current action matches the `actions` list.
1556+
15451557
Consider using this for units that are expensive to continuously update, and can be opted in when necessary.
15461558

15471559
### errors
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
source = "."
3+
}
4+
5+
feature "enable_unit" {
6+
default = false
7+
}
8+
9+
exclude {
10+
if = !feature.enable_unit.value
11+
no_run = true
12+
actions = ["all"]
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
source = "."
3+
}
4+
5+
exclude {
6+
if = true
7+
no_run = true
8+
actions = ["plan"]
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)