Skip to content

Commit fbd0503

Browse files
authored
add block excludes (#2659)
* feat: block-level excludes
1 parent 7940bb7 commit fbd0503

8 files changed

Lines changed: 67 additions & 3 deletions

File tree

cli/pkg/github/github.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ func GitHubCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh
221221
continue
222222
}
223223
}
224+
if len(projectConfig.DriftIncludePatterns) > 0 || len(projectConfig.DriftExcludePatterns) > 0 {
225+
if !digger_config.MatchIncludeExcludePatternsToFile(projectConfig.Dir, projectConfig.DriftIncludePatterns, projectConfig.DriftExcludePatterns) {
226+
slog.Info("Project excluded by block-level drift patterns, skipping", "project", projectConfig.Name, "dir", projectConfig.Dir, "block", projectConfig.BlockName)
227+
continue
228+
}
229+
}
224230
workflow := diggerConfig.Workflows[projectConfig.Workflow]
225231

226232
stateEnvVars, commandEnvVars := digger_config.CollectTerraformEnvConfig(workflow.EnvVars, true)

docs/ce/drift/backendless-scoping-projects.mdx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,33 @@ generate_projects:
8282

8383
Only projects whose `dir` matches an include pattern and does not match an exclude pattern will run drift detection. Exclude patterns are evaluated after include patterns.
8484

85+
## Using per-block drift patterns
86+
87+
You can also place `drift_include_patterns` and `drift_exclude_patterns` **inside a block**. They apply only to projects generated by that block and are matched against each project's `dir`.
88+
89+
```yaml
90+
generate_projects:
91+
blocks:
92+
- block_name: crdb
93+
workflow: default
94+
include: "projects/CRDB/**"
95+
drift_exclude_patterns:
96+
- "projects/CRDB/aws/dev/staging/us-east-1/ec2/**"
97+
- block_name: dev
98+
workflow: default
99+
include: "infra/dev/**"
100+
```
101+
102+
Here, every project generated from the `crdb` block whose directory matches `projects/CRDB/aws/dev/staging/us-east-1/ec2/**` is skipped during drift detection, while everything else in the block runs as usual. The `dev` block is unaffected.
103+
104+
Per-block patterns and top-level patterns compose: a project must pass both checks to run drift detection.
105+
85106
## Notes
86107

87-
- Patterns use [doublestar](https://github.com/bmatcuk/doublestar) glob matching against the project directory path.
88-
- Both fields default to `[]`. If `drift_include_patterns` is empty, all projects are included.
108+
- `drift_include_patterns` / `drift_exclude_patterns` use [doublestar](https://github.com/bmatcuk/doublestar) glob matching against the project directory path.
109+
- Per-block patterns apply only to projects generated by that block. Explicit `projects:` entries and projects from other blocks are unaffected.
110+
- Top-level patterns under `generate_projects` apply to every generated project. When both are set, a project must satisfy each independently.
111+
- All fields default to `[]`. If the include list is empty, every project passes the include step.
89112

90113
## Related
91114

docs/ce/reference/digger.yml.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,14 @@ Automatically generate projects from directory structure using the `generate_pro
417417
List of directory glob patterns to exclude from generated projects.
418418
</ParamField>
419419

420+
<ParamField path="drift_include_patterns" type="array" default="[]">
421+
Glob patterns matched against the `dir` of each project generated by this block. Only matching projects run drift detection. If empty, every project the block generates passes the include step. Evaluated alongside the top-level `drift_include_patterns` — a project must pass both. See [Backendless: Scope Drift to Specific Projects](/ce/drift/backendless-scoping-projects).
422+
</ParamField>
423+
424+
<ParamField path="drift_exclude_patterns" type="array" default="[]">
425+
Glob patterns matched against the `dir` of each project generated by this block. Matching projects are skipped during drift detection. Evaluated after this block's `drift_include_patterns`.
426+
</ParamField>
427+
420428
<ParamField path="workflow" type="string" default="default">
421429
Workflow to use for projects in this block.
422430
</ParamField>

drift/controllers/drift.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ func (mc MainController) TriggerDriftRunForProject(c *gin.Context) {
9292
}
9393
}
9494

95+
// Apply drift include/exclude patterns from the project's source block
96+
if len(theProject.DriftIncludePatterns) > 0 || len(theProject.DriftExcludePatterns) > 0 {
97+
if !dg_configuration.MatchIncludeExcludePatternsToFile(theProject.Dir, theProject.DriftIncludePatterns, theProject.DriftExcludePatterns) {
98+
log.Printf("Project %v dir %v excluded by block-level drift patterns, skipping", project.Name, theProject.Dir)
99+
c.String(http.StatusOK, "project excluded by block-level drift patterns")
100+
return
101+
}
102+
}
103+
95104
projects := []dg_configuration.Project{*theProject}
96105

97106
jobsForImpactedProjects, err := generic.CreateJobsForProjects(projects, command, "drift", repoFullName, "digger", config.Workflows, &issueNumber, nil, branch, branch, false)

libs/digger_config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ type Project struct {
7272
AwsCognitoOidcConfig *AwsCognitoOidcConfig
7373
Generated bool
7474
PulumiStack string
75+
DriftIncludePatterns []string
76+
DriftExcludePatterns []string
7577
}
7678

7779
type Workflow struct {

libs/digger_config/converters.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ func copyProjects(projects []*ProjectYaml) []Project {
104104
awsCognitoOidc,
105105
p.Generated,
106106
workspace,
107+
p.DriftIncludePatterns,
108+
p.DriftExcludePatterns,
107109
}
108110
result[i] = item
109111
}

libs/digger_config/digger_config.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func HandleYamlProjectGeneration(config *DiggerConfigYaml, terraformDir string,
448448
"blockName", b.BlockName)
449449
return nil, err
450450
}
451-
if len(b.IncludePatterns) > 0 || len(b.ExcludePatterns) > 0 {
451+
if len(b.IncludePatterns) > 0 || len(b.ExcludePatterns) > 0 || len(b.DriftIncludePatterns) > 0 || len(b.DriftExcludePatterns) > 0 {
452452
for _, project := range config.Projects {
453453
if project.BlockName == b.BlockName && project.Terragrunt {
454454
if len(b.IncludePatterns) > 0 {
@@ -465,6 +465,12 @@ func HandleYamlProjectGeneration(config *DiggerConfigYaml, terraformDir string,
465465
"blockName", b.BlockName,
466466
"excludePatterns", project.ExcludePatterns)
467467
}
468+
if len(b.DriftIncludePatterns) > 0 {
469+
project.DriftIncludePatterns = append(project.DriftIncludePatterns, b.DriftIncludePatterns...)
470+
}
471+
if len(b.DriftExcludePatterns) > 0 {
472+
project.DriftExcludePatterns = append(project.DriftExcludePatterns, b.DriftExcludePatterns...)
473+
}
468474
}
469475
}
470476
}
@@ -499,6 +505,7 @@ func HandleYamlProjectGeneration(config *DiggerConfigYaml, terraformDir string,
499505
"projectName", projectName)
500506

501507
project := ProjectYaml{
508+
BlockName: b.BlockName,
502509
Name: projectName,
503510
Dir: dir,
504511
Workflow: workflow,
@@ -510,6 +517,8 @@ func HandleYamlProjectGeneration(config *DiggerConfigYaml, terraformDir string,
510517
WorkflowFile: b.WorkflowFile,
511518
IncludePatterns: b.IncludePatterns,
512519
ExcludePatterns: b.ExcludePatterns,
520+
DriftIncludePatterns: b.DriftIncludePatterns,
521+
DriftExcludePatterns: b.DriftExcludePatterns,
513522
}
514523
config.Projects = append(config.Projects, &project)
515524
}

libs/digger_config/yaml.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ type ProjectYaml struct {
6565
Generated bool `yaml:"generated"`
6666
AwsCognitoOidcConfig *AwsCognitoOidcConfig `yaml:"aws_cognito_oidc,omitempty"`
6767
PulumiStack string `yaml:"pulumi_stack"`
68+
DriftIncludePatterns []string `yaml:"drift_include_patterns,omitempty"`
69+
DriftExcludePatterns []string `yaml:"drift_exclude_patterns,omitempty"`
6870
}
6971

7072
type WorkflowYaml struct {
@@ -121,6 +123,9 @@ type BlockYaml struct {
121123
IncludePatterns []string `yaml:"include_patterns,omitempty"`
122124
ExcludePatterns []string `yaml:"exclude_patterns,omitempty"`
123125

126+
DriftIncludePatterns []string `yaml:"drift_include_patterns,omitempty"`
127+
DriftExcludePatterns []string `yaml:"drift_exclude_patterns,omitempty"`
128+
124129
// these flags are only for terragrunt
125130
Terragrunt bool `yaml:"terragrunt"`
126131
RootDir *string `yaml:"root_dir"`

0 commit comments

Comments
 (0)