Skip to content

Commit

Permalink
added run script
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa6765 committed Sep 2, 2024
1 parent 8d5e8ee commit 0052bfb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
terraform/*/modules
terraform/*/common-resources.tf
!terraform/common/common-resources.tf
projects/*/modules
projects/*/common-resources.tf
!projects/common/common-resources.tf
auto_generated_backend.tf

# Terragrunt specific
.terraform
Expand Down
4 changes: 0 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
"dev": {
"TF_WORKSPACE": "dev-project",
"TG_WORKDIR": "environment/dev"
},
"default": {
"TF_WORKSPACE": "default",
"TG_WORKDIR": "environment/default"
}
}
}
72 changes: 72 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

# Function to read JSON values
function get_json_value() {
local key="$1"
local json_file="$2"
echo $(jq -r "$key" "$json_file")
}

# File path to config.json
CONFIG_FILE="./config.json"

# Prompt user to pick an environment
echo "Available environments:"
environments=($(jq -r '.branches | keys | .[]' "$CONFIG_FILE"))
for i in "${!environments[@]}"; do
echo "$((i+1)). ${environments[$i]}"
done

read -p "Select an environment by number: " env_num

# Validate the selected environment
if ! [[ "$env_num" =~ ^[0-9]+$ ]] || (( env_num < 1 || env_num > ${#environments[@]} )); then
echo "Error: Invalid selection."
exit 1
fi

ENVIRONMENT="${environments[$((env_num-1))]}"

# 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_VERSION=$(get_json_value ".terraform_version" "$CONFIG_FILE")
TG_VERSION=$(get_json_value ".terragrunt_version" "$CONFIG_FILE")

echo "Selected environment: $ENVIRONMENT"
echo "Terraform Workspace: $TF_WORKSPACE"
echo "Terragrunt Working Directory: $TG_WORKDIR"

# Change directory to the Terragrunt working directory
cd "$TG_WORKDIR" || { echo "Error: Directory '$TG_WORKDIR' does not exist."; exit 1; }

# Initialize Terraform with Terragrunt
echo "Initializing Terragrunt..."
terragrunt init --terragrunt-non-interactive

# Prompt user to select a Terragrunt workspace
echo "Available Terragrunt workspaces:"
workspaces=($(terragrunt workspace list | sed 's/^[* ]*//'))
for i in "${!workspaces[@]}"; do
echo "$((i+1)). ${workspaces[$i]}"
done

read -p "Select a Terragrunt workspace by number: " ws_num

# Validate the selected Terragrunt workspace
if ! [[ "$ws_num" =~ ^[0-9]+$ ]] || (( ws_num < 1 || ws_num > ${#workspaces[@]} )); then
echo "Error: Invalid selection."
exit 1
fi

TG_WORKSPACE="${workspaces[$((ws_num-1))]}"

# Select the Terragrunt workspace
echo "Selecting Terragrunt workspace: $TG_WORKSPACE"
terragrunt workspace select "$TG_WORKSPACE"

# Run Terragrunt plan
echo "Running Terragrunt plan..."
terragrunt plan --terragrunt-non-interactive

echo "Terragrunt plan completed successfully!"

0 comments on commit 0052bfb

Please sign in to comment.