diff --git a/.github/workflows/terraform-terragrunt.yml b/.github/workflows/terraform-terragrunt.yml index 014172e..0ad4b0f 100644 --- a/.github/workflows/terraform-terragrunt.yml +++ b/.github/workflows/terraform-terragrunt.yml @@ -31,10 +31,12 @@ jobs: CONFIG_FILE="${GITHUB_WORKSPACE}/config.json" TF_VERSION=$(jq -r '.terraform_version' $CONFIG_FILE) TG_VERSION=$(jq -r '.terragrunt_version' $CONFIG_FILE) + BRANCH_NAME=${GITHUB_REF#refs/heads/} BRANCH_CONFIG=$(jq -r --arg branch "$BRANCH_NAME" '.branches[$branch] // .branches.default' $CONFIG_FILE) TF_WORKSPACE=$(echo $BRANCH_CONFIG | jq -r '.TF_WORKSPACE') TG_WORKDIR=$(echo $BRANCH_CONFIG | jq -r '.TG_WORKDIR') + echo "TG_WORKDIR=$TG_WORKDIR" >> $GITHUB_ENV echo "TF_VERSION=$TF_VERSION" >> $GITHUB_ENV echo "TG_VERSION=$TG_VERSION" >> $GITHUB_ENV diff --git a/projects/stage/main.tf b/projects/stage/main.tf index d2f0de7..998870d 100644 --- a/projects/stage/main.tf +++ b/projects/stage/main.tf @@ -12,13 +12,13 @@ module "vpc" { tags = var.vpc_tags } -module "subnet" { - source = "./modules/subnet" +# module "subnet" { +# source = "./modules/subnet" - vpc_id = module.vpc.vpc_id - subnet_cidrs = var.subnet_cidrs - availability_zones = var.subnet_availability_zones - map_public_ip_on_launch = var.subnet_map_public_ip_on_launch - name_prefix = var.subnet_name_prefix - tags = var.subnet_tags -} +# vpc_id = module.vpc.vpc_id +# subnet_cidrs = var.subnet_cidrs +# availability_zones = var.subnet_availability_zones +# map_public_ip_on_launch = var.subnet_map_public_ip_on_launch +# name_prefix = var.subnet_name_prefix +# tags = var.subnet_tags +# } diff --git a/run.sh b/run.sh index 63e042b..59e38a1 100755 --- a/run.sh +++ b/run.sh @@ -4,7 +4,7 @@ function get_json_value() { local key="$1" local json_file="$2" - echo $(jq -r "$key" "$json_file") + jq -r "$key" "$json_file" } # File path to config.json @@ -23,14 +23,14 @@ done read -p "Select an environment: " ENVIRONMENT # Validate the selected environment -if ! jq -e ".branches.$ENVIRONMENT" "$CONFIG_FILE" > /dev/null; then +if ! jq -e ".branches.\"$ENVIRONMENT\"" "$CONFIG_FILE" > /dev/null; then echo "Error: Invalid environment '$ENVIRONMENT'. Please run the script again and choose a valid environment." exit 1 fi # Extract configurations from config.json -TF_WORKSPACE=$(get_json_value ".branches.$ENVIRONMENT.TF_WORKSPACE" "$CONFIG_FILE") -TG_WORKDIR=$(get_json_value ".branches.$ENVIRONMENT.TG_WORKDIR" "$CONFIG_FILE") +TF_WORKSPACE=$(get_json_value ".branches.\"$ENVIRONMENT\".TF_WORKSPACE" "$CONFIG_FILE") +TG_WORKDIR=$(get_json_value ".branches.\"$ENVIRONMENT\".TG_WORKDIR" "$CONFIG_FILE") TF_VERSION=$(get_json_value ".terraform_version" "$CONFIG_FILE") TG_VERSION=$(get_json_value ".terragrunt_version" "$CONFIG_FILE") @@ -67,21 +67,13 @@ echo -e "\n=== Changing to Terragrunt Working Directory ===" echo "Navigating to: $TG_WORKDIR" cd "$TG_WORKDIR" || { echo "Error: Directory '$TG_WORKDIR' does not exist. Please check your configuration."; exit 1; } +export THE_TF_WORKSPACE="$TF_WORKSPACE" + # Initialize Terraform with Terragrunt echo -e "\n=== Initializing Terragrunt ===" echo "Running: terragrunt init --terragrunt-non-interactive" terragrunt init --terragrunt-non-interactive -# Select the Terragrunt workspace -echo -e "\n=== Selecting Terragrunt Workspace ===" -echo "Attempting to select workspace: $TF_WORKSPACE" -if terragrunt workspace select "$TF_WORKSPACE"; then - echo "Workspace '$TF_WORKSPACE' selected successfully." -else - echo "Workspace '$TF_WORKSPACE' not found. Creating new workspace..." - terragrunt workspace new "$TF_WORKSPACE" - echo "New workspace '$TF_WORKSPACE' created and selected." -fi # Run the selected Terragrunt action echo -e "\n=== Executing Terragrunt $ACTION ===" @@ -107,4 +99,4 @@ esac echo -e "\n=== Operation Complete ===" echo "Terragrunt $ACTION completed successfully!" -echo "Thank you for using the Terragrunt Management Script." \ No newline at end of file +echo "Thank you for using the Terragrunt Management Script."