Skip to content

Commit

Permalink
Added doc and modify set-workspace dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa6765 committed Sep 2, 2024
1 parent ab38816 commit 09bea71
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 18 deletions.
30 changes: 13 additions & 17 deletions .github/workflows/terraform-terragrunt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,25 @@ jobs:
terraform:
runs-on: ubuntu-latest
env:
THE_TG_VERSION: 0.57.0
THE_TF_VERSION: 1.8.0
THE_TG_VERSION: ${{ secrets.THE_TG_VERSION }}
THE_TF_VERSION: ${{ secrets.THE_TF_VERSION }}
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Print Terraform and Terragrunt versions
run: |
echo "Terraform Version: ${{ env.THE_TF_VERSION:0:1 }}***"
echo "Terragrunt Version: ${{ env.THE_TG_VERSION:0:1 }}***"
- name: Set Terraform workspace based on branch
id: set-workspace
run: |
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
UPPER_BRANCH_NAME=${BRANCH_NAME^^}
echo "TF_WORKSPACE=${{ secrets[UPPER_BRANCH_NAME + '_TF_WORKSPACE'] }}" >> $GITHUB_ENV
echo "TG_WORKDIR=${{ secrets[UPPER_BRANCH_NAME + '_TG_WORKDIR'] }}" >> $GITHUB_ENV
- name: Set up Terraform CLI
uses: hashicorp/setup-terraform@v3
Expand All @@ -57,13 +53,13 @@ jobs:
- name: Terragrunt init
run: |
cd ${{ github.workspace }}/$TG_WORKDIR
cd ${{ github.workspace }}/${{ env.TG_WORKDIR }}
terragrunt init --terragrunt-non-interactive
echo "🔧 Terragrunt init completed!"
- name: Terragrunt plan
run: |
cd ${{ github.workspace }}/$TG_WORKDIR
cd ${{ github.workspace }}/${{ env.TG_WORKDIR }}
terragrunt plan --terragrunt-non-interactive
echo "📝 Terragrunt plan completed!"
Expand Down
81 changes: 80 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,83 @@ terrafrom-terragrunt-aws-project
│ └── terragrunt.hcl
└── prod
└── terragrunt.hcl
```
```

---

# Terraform Terragrunt GitHub Actions Workflow

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.

## Setting Up GitHub Secrets

To use this workflow, you'll need to configure the following secrets in your GitHub repository:

### 1. Terraform and Terragrunt Versions

- **`THE_TG_VERSION`**: The version of Terragrunt to use (e.g., `0.57.0`).
- **`THE_TF_VERSION`**: The version of Terraform to use (e.g., `1.8.0`).

### 2. Terraform Cloud API Token

- **`TF_API_TOKEN`**: A Terraform Cloud API token used for authentication.

### 3. Environment-Specific Secrets

These secrets should be named according to the branch they correspond to (`main`, `stage`, or `dev`):

- **`MAIN_TF_WORKSPACE`**: The Terraform workspace for the `main` branch.
- **`MAIN_TG_WORKDIR`**: The Terragrunt working directory for the `main` branch.

- **`STAGE_TF_WORKSPACE`**: The Terraform workspace for the `stage` branch.
- **`STAGE_TG_WORKDIR`**: The Terragrunt working directory for the `stage` branch.

- **`DEV_TF_WORKSPACE`**: The Terraform workspace for the `dev` branch.
- **`DEV_TG_WORKDIR`**: The Terragrunt working directory for the `dev` branch.

### How to Set Up Secrets in GitHub

1. Navigate to your repository on GitHub.
2. Click on the **Settings** tab.
3. In the left sidebar, click on **Secrets and variables** > **Actions**.
4. Click the **New repository secret** button.
5. Add each secret by specifying its name and value, then click **Add secret**.

### Example Secret Configuration

For a repository that uses Terraform and Terragrunt, you would configure secrets as follows:

- **Secret Name**: `THE_TG_VERSION`
- **Value**: `0.57.0`

- **Secret Name**: `THE_TF_VERSION`
- **Value**: `1.8.0`

- **Secret Name**: `TF_API_TOKEN`
- **Value**: `<your-terraform-cloud-api-token>`

- **Secret Name**: `MAIN_TF_WORKSPACE`
- **Value**: `prod-project`

- **Secret Name**: `MAIN_TG_WORKDIR`
- **Value**: `environment/prod`

- **Secret Name**: `STAGE_TF_WORKSPACE`
- **Value**: `stage-project`

- **Secret Name**: `STAGE_TG_WORKDIR`
- **Value**: `environment/stage`

- **Secret Name**: `DEV_TF_WORKSPACE`
- **Value**: `dev-project`

- **Secret Name**: `DEV_TG_WORKDIR`
- **Value**: `environment/dev`

## Usage

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.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

0 comments on commit 09bea71

Please sign in to comment.