Skip to content

Commit 09bea71

Browse files
committed
Added doc and modify set-workspace dynamic
1 parent ab38816 commit 09bea71

File tree

2 files changed

+93
-18
lines changed

2 files changed

+93
-18
lines changed

.github/workflows/terraform-terragrunt.yml

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,25 @@ jobs:
1212
terraform:
1313
runs-on: ubuntu-latest
1414
env:
15-
THE_TG_VERSION: 0.57.0
16-
THE_TF_VERSION: 1.8.0
15+
THE_TG_VERSION: ${{ secrets.THE_TG_VERSION }}
16+
THE_TF_VERSION: ${{ secrets.THE_TF_VERSION }}
1717
steps:
1818
- name: Checkout repository
1919
uses: actions/checkout@v4
2020

21+
- name: Print Terraform and Terragrunt versions
22+
run: |
23+
echo "Terraform Version: ${{ env.THE_TF_VERSION:0:1 }}***"
24+
echo "Terragrunt Version: ${{ env.THE_TG_VERSION:0:1 }}***"
25+
2126
- name: Set Terraform workspace based on branch
2227
id: set-workspace
2328
run: |
2429
BRANCH_NAME=${GITHUB_REF#refs/heads/}
25-
if [[ "$BRANCH_NAME" == "main" ]]; then
26-
echo "TF_WORKSPACE=prod-project" >> $GITHUB_ENV
27-
echo "TG_WORKDIR=environment/stage" >> $GITHUB_ENV
28-
elif [[ "$BRANCH_NAME" == "stage" ]]; then
29-
echo "TF_WORKSPACE=stage-project" >> $GITHUB_ENV
30-
echo "TG_WORKDIR=environment/stage" >> $GITHUB_ENV
31-
elif [[ "$BRANCH_NAME" == "dev" ]]; then
32-
echo "TF_WORKSPACE=dev-project" >> $GITHUB_ENV
33-
echo "TG_WORKDIR=environment/dev" >> $GITHUB_ENV
34-
else
35-
echo "TF_WORKSPACE=default" >> $GITHUB_ENV
36-
echo "TG_WORKDIR=environment/default" >> $GITHUB_ENV
37-
fi
30+
UPPER_BRANCH_NAME=${BRANCH_NAME^^}
31+
32+
echo "TF_WORKSPACE=${{ secrets[UPPER_BRANCH_NAME + '_TF_WORKSPACE'] }}" >> $GITHUB_ENV
33+
echo "TG_WORKDIR=${{ secrets[UPPER_BRANCH_NAME + '_TG_WORKDIR'] }}" >> $GITHUB_ENV
3834
3935
- name: Set up Terraform CLI
4036
uses: hashicorp/setup-terraform@v3
@@ -57,13 +53,13 @@ jobs:
5753
5854
- name: Terragrunt init
5955
run: |
60-
cd ${{ github.workspace }}/$TG_WORKDIR
56+
cd ${{ github.workspace }}/${{ env.TG_WORKDIR }}
6157
terragrunt init --terragrunt-non-interactive
6258
echo "🔧 Terragrunt init completed!"
6359
6460
- name: Terragrunt plan
6561
run: |
66-
cd ${{ github.workspace }}/$TG_WORKDIR
62+
cd ${{ github.workspace }}/${{ env.TG_WORKDIR }}
6763
terragrunt plan --terragrunt-non-interactive
6864
echo "📝 Terragrunt plan completed!"
6965

README.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,83 @@ terrafrom-terragrunt-aws-project
3434
│ └── terragrunt.hcl
3535
└── prod
3636
└── terragrunt.hcl
37-
```
37+
```
38+
39+
---
40+
41+
# Terraform Terragrunt GitHub Actions Workflow
42+
43+
This repository contains a GitHub Actions workflow to automate Terraform and Terragrunt operations for different environments. The workflow requires certain GitHub secrets to be configured.
44+
45+
## Setting Up GitHub Secrets
46+
47+
To use this workflow, you'll need to configure the following secrets in your GitHub repository:
48+
49+
### 1. Terraform and Terragrunt Versions
50+
51+
- **`THE_TG_VERSION`**: The version of Terragrunt to use (e.g., `0.57.0`).
52+
- **`THE_TF_VERSION`**: The version of Terraform to use (e.g., `1.8.0`).
53+
54+
### 2. Terraform Cloud API Token
55+
56+
- **`TF_API_TOKEN`**: A Terraform Cloud API token used for authentication.
57+
58+
### 3. Environment-Specific Secrets
59+
60+
These secrets should be named according to the branch they correspond to (`main`, `stage`, or `dev`):
61+
62+
- **`MAIN_TF_WORKSPACE`**: The Terraform workspace for the `main` branch.
63+
- **`MAIN_TG_WORKDIR`**: The Terragrunt working directory for the `main` branch.
64+
65+
- **`STAGE_TF_WORKSPACE`**: The Terraform workspace for the `stage` branch.
66+
- **`STAGE_TG_WORKDIR`**: The Terragrunt working directory for the `stage` branch.
67+
68+
- **`DEV_TF_WORKSPACE`**: The Terraform workspace for the `dev` branch.
69+
- **`DEV_TG_WORKDIR`**: The Terragrunt working directory for the `dev` branch.
70+
71+
### How to Set Up Secrets in GitHub
72+
73+
1. Navigate to your repository on GitHub.
74+
2. Click on the **Settings** tab.
75+
3. In the left sidebar, click on **Secrets and variables** > **Actions**.
76+
4. Click the **New repository secret** button.
77+
5. Add each secret by specifying its name and value, then click **Add secret**.
78+
79+
### Example Secret Configuration
80+
81+
For a repository that uses Terraform and Terragrunt, you would configure secrets as follows:
82+
83+
- **Secret Name**: `THE_TG_VERSION`
84+
- **Value**: `0.57.0`
85+
86+
- **Secret Name**: `THE_TF_VERSION`
87+
- **Value**: `1.8.0`
88+
89+
- **Secret Name**: `TF_API_TOKEN`
90+
- **Value**: `<your-terraform-cloud-api-token>`
91+
92+
- **Secret Name**: `MAIN_TF_WORKSPACE`
93+
- **Value**: `prod-project`
94+
95+
- **Secret Name**: `MAIN_TG_WORKDIR`
96+
- **Value**: `environment/prod`
97+
98+
- **Secret Name**: `STAGE_TF_WORKSPACE`
99+
- **Value**: `stage-project`
100+
101+
- **Secret Name**: `STAGE_TG_WORKDIR`
102+
- **Value**: `environment/stage`
103+
104+
- **Secret Name**: `DEV_TF_WORKSPACE`
105+
- **Value**: `dev-project`
106+
107+
- **Secret Name**: `DEV_TG_WORKDIR`
108+
- **Value**: `environment/dev`
109+
110+
## Usage
111+
112+
Once the secrets are configured, the workflow will automatically pick them up and run Terraform and Terragrunt operations when changes are pushed to the `main`, `stage`, or `dev` branches.
113+
114+
## License
115+
116+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)