Skip to content

Commit 56aa24b

Browse files
Feature/matrix (#4)
* Add default tagging * Fix workflows * Fix workflows * Fix outputs * Fix outputs * Add matrixed environment support * Pass env var * Checkout first * Always deploy sandbox on PR * Echo matrix contents to assist in debugging * Try to resolve duplicate runs * Try to resolve duplicate runs * Rip out dupe detection, alter deploy trigger --------- Signed-off-by: Chris Taylor <132399041+chris11-taylor-nttd@users.noreply.github.com>
1 parent cc450d0 commit 56aa24b

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

.github/workflows/deploy-sandbox.yml

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,81 @@ name: Deploy Sandbox Environment
22

33
on:
44
pull_request:
5-
branches: [ main ]
6-
push:
7-
branches: [ main ]
5+
branches: [ "**" ]
6+
87
permissions:
98
id-token: write
109
contents: read
1110

1211
jobs:
12+
build-matrix:
13+
runs-on: ubuntu-latest
14+
outputs:
15+
matrix: ${{ steps.build-matrix.outputs.matrix }}
16+
steps:
17+
- uses: actions/checkout@09d2acae674a48949e3602304ab46fd20ae0c42f
18+
- name: Build Environment Matrix
19+
id: build-matrix
20+
shell: python
21+
env:
22+
PLATFORM_ENVIRONMENT: "sandbox"
23+
run: |
24+
from pathlib import Path
25+
import os
26+
import json
27+
28+
def discover_environments(platform_path: Path = Path("platform")) -> list[Path]:
29+
return [d for d in platform_path.iterdir() if d.is_dir()]
30+
31+
def discover_regions(environment_path: Path) -> list[Path]:
32+
return [d for d in environment_path.iterdir() if d.is_dir()]
33+
34+
def discover_instances(region_path: Path) -> list[Path]:
35+
return [d for d in region_path.iterdir() if d.is_dir()]
36+
37+
try:
38+
ENVIRONMENT = os.environ['PLATFORM_ENVIRONMENT']
39+
except KeyError as ke:
40+
raise ValueError("Environment variable named PLATFORM_ENVIRONMENT was not found. This variable must be supplied so that a matrix of environments can be built!")
41+
42+
if len(ENVIRONMENT) == 0:
43+
raise ValueError("Environment variable PLATFORM_ENVIRONMENT was empty. This variable must be supplied so that a matrix of environments can be built!")
44+
45+
all_environments = discover_environments()
46+
matrix = {"terragrunt_environment": []}
47+
try:
48+
selected_environment = list(filter(lambda x: x.name == ENVIRONMENT, all_environments))[0]
49+
except Exception:
50+
raise ValueError(f"Expected environment '{ENVIRONMENT}' not found in {all_environments}")
51+
52+
regions = discover_regions(environment_path=selected_environment)
53+
54+
for region_path in regions:
55+
region_instances = discover_instances(region_path=region_path)
56+
for instance in region_instances:
57+
matrix["terragrunt_environment"].append({"environment": selected_environment.name, "region": region_path.name, "instance": instance.name})
58+
59+
print("Generated the following environment matrix:")
60+
print(json.dumps(matrix, indent=4))
61+
62+
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
63+
f.write(f"matrix={json.dumps(matrix, separators=(',', ':'))}")
64+
1365
call-terragrunt-deploy:
66+
needs: build-matrix
1467
permissions:
1568
contents: read
1669
id-token: write
70+
strategy:
71+
fail-fast: false
72+
matrix: ${{ fromJson(needs.build-matrix.outputs.matrix) }}
73+
1774
uses: ./.github/workflows/terragrunt-deploy.yml
1875
with:
1976
tf_version: '1.5.5'
2077
tg_version: '0.54.11'
21-
environment: sandbox
22-
region: us-east-2
23-
env_id: '000'
78+
environment: ${{ matrix.terragrunt_environment.environment }}
79+
region: ${{ matrix.terragrunt_environment.region }}
80+
env_id: ${{ matrix.terragrunt_environment.instance }}
81+
2482
secrets: inherit

.github/workflows/terragrunt-deploy.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ jobs:
8181
uses: gruntwork-io/terragrunt-action@aee21a7df999be8b471c2a8564c6cd853cb674e1
8282
env:
8383
AWS_REGION: ${{ inputs.region }}
84-
TERRAFORM_PLAN: ${{ steps.plan.outputs.TERRAFORM_PLAN }}
8584
INPUT_PRE_EXEC_0: |
8685
sudo apt update -yqq && sudo apt install python3 -yqq
8786
TF_VAR_organization_tag: ${{ steps.set-tags.outputs.TF_VAR_organization_tag }}

0 commit comments

Comments
 (0)