Skip to content

Commit

Permalink
added hypen jq config and action organize
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa6765 committed Sep 9, 2024
1 parent 90ea7cb commit 63e299d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/terraform-terragrunt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions projects/stage/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
# }
22 changes: 7 additions & 15 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand Down Expand Up @@ -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 ==="
Expand All @@ -107,4 +99,4 @@ esac

echo -e "\n=== Operation Complete ==="
echo "Terragrunt $ACTION completed successfully!"
echo "Thank you for using the Terragrunt Management Script."
echo "Thank you for using the Terragrunt Management Script."

0 comments on commit 63e299d

Please sign in to comment.