Skip to content

Commit b686309

Browse files
committed
Re-organized project directory structure
1 parent eaa0107 commit b686309

File tree

25 files changed

+438
-194
lines changed

25 files changed

+438
-194
lines changed

.github/workflows/terraform-apply.yml

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,35 @@ jobs:
1212
- name: Checkout
1313
uses: actions/checkout@v3
1414

15-
- name: Set Environment Folder
16-
id: env
15+
- name: Detect Changed Environments
16+
id: changes
1717
run: |
18-
if git diff-tree --no-commit-id --name-only -r ${{ github.sha }} | grep '^environments/dev/'; then
19-
echo "env_folder=environments/dev" >> $GITHUB_OUTPUT
20-
elif git diff-tree --no-commit-id --name-only -r ${{ github.sha }} | grep '^environments/prod/'; then
21-
echo "env_folder=environments/prod" >> $GITHUB_OUTPUT
22-
else
23-
echo "env_folder=" >> $GITHUB_OUTPUT
24-
fi
18+
changed_dirs=$(git diff-tree --no-commit-id --name-only -r ${{ github.sha }} \
19+
| grep -E '^projects/[^/]+/environments/(dev|prod)/' \
20+
| cut -d '/' -f1-4 \
21+
| sort -u)
22+
23+
echo "changed=${changed_dirs}" >> $GITHUB_OUTPUT
2524
2625
- name: Exit if No Matching Env
27-
if: steps.env.outputs.env_folder == ''
28-
run: echo "No changes to dev or prod environments. Skipping apply."
26+
if: steps.changes.outputs.changed == ''
27+
run: echo "No dev/prod environment changes detected. Skipping."
2928

3029
- name: Decode GCP SA
31-
if: steps.env.outputs.env_folder != ''
30+
if: steps.changes.outputs.changed != ''
3231
run: echo "${{ secrets.GCP_SA_KEY_BASE64 }}" | base64 -d > gcp-creds.json
3332

3433
- name: Set up Terraform
35-
if: steps.env.outputs.env_folder != ''
34+
if: steps.changes.outputs.changed != ''
3635
uses: hashicorp/setup-terraform@v2
3736
with:
3837
terraform_version: 1.11.4
3938

40-
- name: Terraform Init & Apply
41-
if: steps.env.outputs.env_folder != ''
42-
working-directory: ${{ steps.env.outputs.env_folder }}
39+
- name: Terraform Init & Apply (per env)
40+
if: steps.changes.outputs.changed != ''
4341
run: |
44-
terraform init
45-
terraform apply -auto-approve
42+
for env_dir in ${{ steps.changes.outputs.changed }}; do
43+
echo "==> Applying in $env_dir"
44+
terraform -chdir=$env_dir init
45+
terraform -chdir=$env_dir apply -auto-approve
46+
done

.github/workflows/terraform-plan-dev.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ on:
44
push:
55
branches: [dev]
66
paths:
7-
- "environments/dev/**"
7+
- "projects/learning/environments/dev/**"
8+
- "projects/learning2/environments/dev/**"
89

910
jobs:
10-
call-plan:
11+
plan-learning:
1112
uses: ./.github/workflows/terraform-plan.yml
1213
with:
13-
environment: dev
14+
environment: projects/learning/environments/dev
15+
16+
plan-learning2:
17+
uses: ./.github/workflows/terraform-plan.yml
18+
with:
19+
environment: projects/learning2/environments/dev

.github/workflows/terraform-plan-prod.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ name: Terraform Plan (Prod)
22

33
on:
44
push:
5-
branches: [prod]
5+
branches: [main]
66
paths:
7-
- "environments/prod/**"
7+
- "projects/learning/environments/prod/**"
8+
- "projects/learning2/environments/prod/**"
89

910
jobs:
10-
call-plan:
11+
plan-learning:
1112
uses: ./.github/workflows/terraform-plan.yml
1213
with:
13-
environment: prod
14+
environment: projects/learning/environments/prod
15+
16+
plan-learning2:
17+
uses: ./.github/workflows/terraform-plan.yml
18+
with:
19+
environment: projects/learning2/environments/prod
Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
1-
name: Terraform Plan (Reusable)
1+
name: Terraform Plan
22

33
on:
4-
workflow_call:
5-
inputs:
6-
environment:
7-
required: true
8-
type: string
4+
pull_request:
5+
branches: [main]
96

107
jobs:
118
plan:
129
runs-on: ubuntu-latest
1310

14-
env:
15-
GOOGLE_APPLICATION_CREDENTIALS: ${{ github.workspace }}/gcp-creds.json
16-
1711
steps:
18-
- name: Checkout repo
12+
- name: Checkout code
1913
uses: actions/checkout@v3
2014

21-
- name: Decode GCP Service Account
22-
run: echo "${{ secrets.GCP_SA_KEY_BASE64 }}" | base64 -d > "$GOOGLE_APPLICATION_CREDENTIALS"
15+
- name: Detect Changed Environments
16+
id: changes
17+
run: |
18+
changed_dirs=$(git diff --name-only origin/main...HEAD \
19+
| grep -E '^projects/[^/]+/environments/(dev|prod)/' \
20+
| cut -d '/' -f1-4 \
21+
| sort -u)
2322
24-
- name: Setup Terraform
25-
uses: hashicorp/setup-terraform@v2
26-
with:
27-
terraform_version: 1.11.4
23+
echo "changed=${changed_dirs}" >> $GITHUB_OUTPUT
2824
29-
- name: Terraform Init
30-
working-directory: environments/${{ inputs.environment }}
31-
run: terraform init
25+
- name: Exit if No Matching Env
26+
if: steps.changes.outputs.changed == ''
27+
run: echo "No dev/prod environment changes detected. Skipping."
3228

33-
- name: Terraform Format
34-
working-directory: environments/${{ inputs.environment }}
35-
run: terraform fmt -check
29+
- name: Decode GCP SA
30+
if: steps.changes.outputs.changed != ''
31+
run: echo "${{ secrets.GCP_SA_KEY_BASE64 }}" | base64 -d > gcp-creds.json
3632

37-
- name: Terraform Validate
38-
working-directory: environments/${{ inputs.environment }}
39-
run: terraform validate
33+
- name: Set up Terraform
34+
if: steps.changes.outputs.changed != ''
35+
uses: hashicorp/setup-terraform@v2
36+
with:
37+
terraform_version: 1.11.4
4038

41-
- name: Terraform Plan
42-
working-directory: environments/${{ inputs.environment }}
43-
run: terraform plan
39+
- name: Terraform Init & Plan (per env)
40+
if: steps.changes.outputs.changed != ''
41+
run: |
42+
for env_dir in ${{ steps.changes.outputs.changed }}; do
43+
echo "==> Planning in $env_dir"
44+
terraform -chdir=$env_dir init
45+
terraform -chdir=$env_dir fmt -check
46+
terraform -chdir=$env_dir validate
47+
terraform -chdir=$env_dir plan
48+
done

environments/dev/main.tf

Lines changed: 0 additions & 111 deletions
This file was deleted.

environments/dev/variables.tf

Lines changed: 0 additions & 11 deletions
This file was deleted.

modules/vpc-peering/main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
resource "google_compute_network_peering" "this" {
2+
provider = google
3+
name = var.name
4+
network = var.network_self_link
5+
peer_network = var.peer_network_self_link
6+
peer_project = var.peer_project_id
7+
export_custom_routes = true
8+
import_custom_routes = true
9+
}

modules/vpc-peering/outputs.tf

Whitespace-only changes.

modules/vpc-peering/variables.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "name" {
2+
description = "Name of the peering connection"
3+
type = string
4+
}
5+
6+
variable "network_self_link" {
7+
description = "Self link of the network initiating the peering"
8+
type = string
9+
}
10+
11+
variable "peer_network_self_link" {
12+
description = "Self link of the peer network"
13+
type = string
14+
}
15+
16+
variable "peer_project_id" {
17+
description = "Project ID of the peer network"
18+
type = string
19+
}

modules/vpc-peering/versions.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.11.4"
3+
required_providers {
4+
google = {
5+
source = "hashicorp/google"
6+
version = ">= 5.29"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)