diff --git a/.github/workflows/terraform-terragrunt.yml b/.github/workflows/terraform-terragrunt.yml index f1a1a80..118fa88 100644 --- a/.github/workflows/terraform-terragrunt.yml +++ b/.github/workflows/terraform-terragrunt.yml @@ -11,41 +11,41 @@ on: jobs: terraform: runs-on: ubuntu-latest - env: - THE_TG_VERSION: 0.57.0 - THE_TF_VERSION: 1.8.0 steps: - name: Checkout repository uses: actions/checkout@v4 - - name: Set Terraform workspace based on branch - id: set-workspace + - name: Set up jq run: | + sudo apt-get update + sudo apt-get install -y jq + + - name: Load configuration from config.json + id: load-config + run: | + 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/} - if [[ "$BRANCH_NAME" == "main" ]]; then - echo "TF_WORKSPACE=prod-project" >> $GITHUB_ENV - echo "TG_WORKDIR=environment/stage" >> $GITHUB_ENV - elif [[ "$BRANCH_NAME" == "stage" ]]; then - echo "TF_WORKSPACE=stage-project" >> $GITHUB_ENV - echo "TG_WORKDIR=environment/stage" >> $GITHUB_ENV - elif [[ "$BRANCH_NAME" == "dev" ]]; then - echo "TF_WORKSPACE=dev-project" >> $GITHUB_ENV - echo "TG_WORKDIR=environment/dev" >> $GITHUB_ENV - else - echo "TF_WORKSPACE=default" >> $GITHUB_ENV - echo "TG_WORKDIR=environment/default" >> $GITHUB_ENV - fi + 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 "TF_WORKSPACE=$TF_WORKSPACE" >> $GITHUB_ENV + echo "TG_WORKDIR=$TG_WORKDIR" >> $GITHUB_ENV + echo "TF_VERSION=$TF_VERSION" >> $GITHUB_ENV + echo "TG_VERSION=$TG_VERSION" >> $GITHUB_ENV - name: Set up Terraform CLI uses: hashicorp/setup-terraform@v3 with: - terraform_version: ${{ env.THE_TF_VERSION }} + terraform_version: ${{ env.TF_VERSION }} terraform_wrapper: true cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} - name: Set up Terragrunt CLI run: | - wget -qO /tmp/terragrunt https://github.com/gruntwork-io/terragrunt/releases/download/v${{ env.THE_TG_VERSION }}/terragrunt_linux_amd64 + wget -qO /tmp/terragrunt https://github.com/gruntwork-io/terragrunt/releases/download/v${{ env.TG_VERSION }}/terragrunt_linux_amd64 sudo chmod +x /tmp/terragrunt sudo mv /tmp/terragrunt /usr/local/bin/terragrunt echo "✅ Terragrunt CLI setup completed!" diff --git a/config.json b/config.json index dec0f38..f80c068 100644 --- a/config.json +++ b/config.json @@ -1,18 +1,22 @@ { - "main": { - "TF_WORKSPACE": "prod-project", - "TG_WORKDIR": "environment/stage" - }, - "stage": { - "TF_WORKSPACE": "stage-project", - "TG_WORKDIR": "environment/stage" - }, - "dev": { - "TF_WORKSPACE": "dev-project", - "TG_WORKDIR": "environment/dev" - }, - "default": { - "TF_WORKSPACE": "default", - "TG_WORKDIR": "environment/default" + "terraform_version": "1.8.0", + "terragrunt_version": "0.57.0", + "branches": { + "main": { + "TF_WORKSPACE": "prod-project", + "TG_WORKDIR": "environment/stage" + }, + "stage": { + "TF_WORKSPACE": "stage-project", + "TG_WORKDIR": "environment/stage" + }, + "dev": { + "TF_WORKSPACE": "dev-project", + "TG_WORKDIR": "environment/dev" + }, + "default": { + "TF_WORKSPACE": "default", + "TG_WORKDIR": "environment/default" + } } }