How to run different Terragrunt commands for changes in different directories without manual repetition of job definitions?

Hi everyone,

I have below repository structure:

.
├── envs
│   ├── prod
│   │   ├── env.tfvars
│   │   ├── module1
│   │   │   ├── terraform.tfvars
│   │   │   └── terragrunt.hcl
│   │   └── module2
│   │       ├── terraform.tfvars
│   │       └── terragrunt.hcl
│   └── dev
│       ├── env.tfvars
│       ├── module3
│       │   ├── terraform.tfvars
│       │   └── terragrunt.hcl
│       └── module4
│           ├── terraform.tfvars
│           └── terragrunt.hcl
├── README.md
└── terragrunt.hcl

When a merge request is created I need to execute a custom command based on the list of files which are changed in the mr. Example:

  1. envs/prod/module1/terragrunt.hcl changes I need to:
cd envs/prod/module1
terragrunt plan
  1. envs/prod/env.tfvars changes I need to:
cd envs/prod
terragrunt run-all plan
  1. envs/dev/module4/terraform.tfvars changes I need to:
cd envs/dev/module4
terragrunt plan

I am thinking about adding as a first job a custom script which would create a list of commands which need to be invoked in the next step. This script would use plain git or gitlab API (example) to fetch a list of changed files and generate proper sequence of commands.
Is there maybe some other and better way to implement the needed functionality?

Thanks in advance for all help!

Best Regards,
Rafal.

Hi, your idea with custom solution to get changed files (you can use plain git with already available Gitlab CI variables) and execute commands based on that is good approach.
I can’t think of other without job definition repetition, even with extend you would still need to define a job for each case.