Skip to content

Commit

Permalink
refactor action
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa6765 committed Sep 2, 2024
1 parent 4e6a627 commit 77a27cd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
40 changes: 20 additions & 20 deletions .github/workflows/terraform-terragrunt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down
34 changes: 19 additions & 15 deletions config.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

0 comments on commit 77a27cd

Please sign in to comment.