Run specif pipeline based on changes

Problem to solve

I have a project with some subfolders, here is the project structure with an ls -R command line:
README.md docs environments tools

./docs:
use-case-name.md

./environments:
dev rec

./environments/dev:
my-new-case terraform your-second-wonderfull-use-case your-wonderfull-use-case

./environments/dev/my-new-case:
application.conf log4j.properties movOpKeyMapping.conf version.semver

./environments/dev/terraform:
backend.tf main.tf variables.tf

./environments/dev/your-second-wonderfull-use-case:
app.properties myapp.conf version.semver your-wonderfull-use-case.conf

./environments/dev/your-wonderfull-use-case:
app.properties myapp.conf version.semver your-wonderfull-use-case.conf

./environments/rec:
logistar-enricher-stock-mvt my-new-case terraform

./environments/rec/logistar-enricher-stock-mvt:
application.conf log4j.properties movOpKeyMapping.conf version.semver

./environments/rec/my-new-case:
application.conf log4j.properties movOpKeyMapping.conf version.semver

./environments/rec/terraform:
backend.tf main.tf variables.tf

./tools:
create_jar.sh

under each subfolders (related to the env) i have a terraform with env specific variables and a .gitlab-ci.yml with cache definition and an env related variable which is the terraform path.

I am not able to tell gitlab ci to run only the dev .gitlab-ci.yml if any file has been changed under /environments/dev/**/* and the same for /environments/rec/ ,
i tried the include, include with rules ,…etc , i tried the centralize the work on one .gitlab-ci.yml in the root with some variable detectection (i detect the file changed using git command but it is not stable as a solution as sometines in works for merge request but afyter merging it is not working).
here is my root .gitlab-ci.yml:

  - local: environments/dev/.dev.gitlab-ci.yml
  - local: environments/rec/.rec.gitlab-ci.yml

cache:
  key: spark_serverless_${CI_COMMIT_SHA}
  paths:
    - ${TF_ROOT}

default:
  tags:
    - phenix

image: url/folder/testings/tools/terraform-builder:1293553986
variables:
  TF_ROOT: "${CI_PROJECT_DIR}/environments/${ENVIRONMENT}"
  SERVICE_ACCOUNT_DIRECTORY: "${HOME}/gcloud"
  TF_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/gitlab-projects
  KUBERNETES_CPU_REQUEST: 1
  KUBERNETES_CPU_LIMIT: 1
  KUBERNETES_MEMORY_REQUEST: 2Gi
  KUBERNETES_MEMORY_LIMIT: 2Gi

stages:
  - validate_hocon
  - prepare
  - validate
  - plan
  - apply

init:
  stage: prepare
  script:
    - terraform init -no-color -var "base_root=${CI_PROJECT_DIR}"
  artifacts:
    name: init
    paths:
      - ${TF_ROOT}/.terraform

validate:
  stage: validate
  script:
    - terraform validate -no-color
  dependencies:
    - validate_hocon
    - init
  cache:
    key: spark_serverless_${CI_COMMIT_SHA}
    paths:
      - ${TF_ROOT}/.terraform

plan:
  stage: plan
  script:
    - terraform plan -lock=false -no-color -var "base_root=${CI_PROJECT_DIR}"
  artifacts:
    name: plan
    paths:
      - ${TF_ROOT}/plan.cache
    reports:
      terraform: ${TF_ROOT}/plan.json

apply:
  stage: apply
  script:
    - terraform apply -auto-approve -no-color -var "base_root=${CI_PROJECT_DIR}"
  dependencies:
    - plan
  only:
    - main```
my dev gitlab-ci:
```variables:
  environment: "dev"

before_script:
  - mkdir -p ${SERVICE_ACCOUNT_DIRECTORY}
  - echo $GCP_TAT_KEY_FILE_BASE64 | base64 -d > $SERVICE_ACCOUNT_DIRECTORY/key.json
  - export GOOGLE_APPLICATION_CREDENTIALS=${SERVICE_ACCOUNT_DIRECTORY}/key.json
  - cd ${TF_ROOT}

stages:
  - validate_hocon
  - prepare
  - validate
  - plan
  - apply


on:
  merge_requests:
    only:
      - branches:
          - /^dev/
      - variables:
          - ENVIRONMENT: dev


validate_hocon:
  stage: validate_hocon
  script:
    - cd ${CI_PROJECT_DIR}
    - validator environments/dev
  rules:
    - changes:
        paths:
          - environments/dev/**/*```

For rec it is the same just changing the dev by rec. 

my terraform main.tf: 
```provider "google" {
  project = var.project_id
  region  = var.region
}

# Convert folder_paths list to a map for use with for_each
locals {
  folder_paths_map = { for idx, val in var.folder_paths : idx => val }
}

# Run the external script to create JARs and capture their names
data "external" "create_jars" {
  for_each = local.folder_paths_map
  program  = ["bash", "-c", "cd ${var.base_root} && bash tools/create_jar.sh ${each.value}"]

  query = {
    folder_path = each.value
  }
}

# Extract the jar file names into a map for each folder path
locals {
  jar_file_names = { for k, v in data.external.create_jars : k => v.result.jar_file_name }
}

output "jar_file_names" {
  value = local.jar_file_names
}

resource "google_storage_bucket_object" "jar_objects" {
  for_each = local.folder_paths_map

  bucket = var.bucket_id
  name   = "${basename(each.value)}/${local.jar_file_names[each.key]}"
  source = "${var.base_root}/${local.jar_file_names[each.key]}"
}```


the tools/create_jar.sh:
```#!/bin/bash

# Function to generate JAR file name
generate_jar_file_name() {
  local folder_path="$1"

  # Extract the last directory name from the folder path
  last_dir=$(basename "$folder_path")

  # Check if version file exists
  local version_file="${folder_path}/version.semver"
  if [ ! -f "$version_file" ]; then
    echo "{\"error\": \"Version file '$version_file' not found.\"}"
    exit 1
  fi

  # Read current version
  current_version=$(head -n 1 "$version_file")

  # Validate version format
  if [[ ! "$current_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    echo "{\"error\": \"Invalid version format in file '$version_file'.\"}"
    exit 1
  fi

  # Generate JAR file name
  jar_file_name="${last_dir}-${current_version}-configs.jar"

  # Return the jar file name as JSON
  echo "{\"jar_file_name\": \"$jar_file_name\"}"
}

# Function to create JAR file
create_jar() {
  local base_path="$1"

  # Use the generate_jar_file_name function to get the jar file name
  jar_file_name_json=$(generate_jar_file_name "$base_path")

  # Check if base path exists
  if [ ! -d "$base_path" ]; then
    echo "{\"error\": \"Base path '$base_path' does not exist.\"}"
    exit 1
  fi

  # Create JAR file using the jar command
  jar -cf "$(echo $jar_file_name_json | jq -r '.jar_file_name')" -C "$base_path" .

  # Return the jar file name as JSON
  echo "$jar_file_name_json"
}

# Check if arguments are provided
if [ $# -eq 0 ]; then
  echo "{\"error\": \"No arguments provided. Usage: $0 <base_path>\"}"
  exit 1
fi

# Call create_jar function
create_jar "$1"

please if anyone can help.