Skip to content

Commit 9e8a70f

Browse files
committed
refactor(ci): move terragrunt output parsing into reusable script
Replaces inline fragile terragrunt output extraction in deploy workflow with a dedicated script (scripts/read_terragrunt_outputs.sh). This improves safety, adds validation, supports local execution, and reduces workflow complexity. Also removes outdated comments and adds a TODO for future multi-stack support.
1 parent f319a85 commit 9e8a70f

2 files changed

Lines changed: 50 additions & 12 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ on:
1313
branch:
1414
description: Branch to deploy
1515
required: true
16-
# user must select a branch when triggering workflow manually
1716

1817
env:
1918
working_dir: terragrunt
@@ -29,8 +28,6 @@ jobs:
2928
- name: Checkout selected branch
3029
uses: actions/checkout@v6
3130
with:
32-
# If workflow_dispatch, use the selected branch.
33-
# If push event, use the branch that triggered the workflow.
3431
ref: ${{ github.event.inputs.branch || github.ref_name }}
3532

3633
- name: Configure AWS Credentials
@@ -41,10 +38,6 @@ jobs:
4138
aws-region: us-east-1
4239

4340
- name: Initialize Terragrunt (backend + providers)
44-
# terragrunt init:
45-
# - Configures the remote backend (S3 + DynamoDB)
46-
# - Downloads provider versions pinned in the lockfile
47-
# - Prepares the working directory for plan/apply
4841
uses: gruntwork-io/terragrunt-action@v3
4942
env:
5043
TERRAGRUNT_DISABLE_COPY: true
@@ -60,7 +53,6 @@ jobs:
6053
tg_dir: ${{ env.working_dir }}
6154
tg_command: "run-all plan"
6255

63-
6456
- name: Terragrunt Apply
6557
uses: gruntwork-io/terragrunt-action@v3
6658
env:
@@ -69,12 +61,11 @@ jobs:
6961
tg_dir: ${{ env.working_dir }}
7062
tg_command: "run-all apply"
7163

64+
# TODO(j4y): Replace hard-coded path once multiple stacks exist.
7265
- name: Read Values
7366
id: terragrunt_output
7467
run: |
75-
cd terragrunt/live/website
76-
printf "distribution_id=%s\n" $(terragrunt output distribution_id) >> "$GITHUB_OUTPUT"
77-
printf "bucket_name=%s\n" $(terragrunt output bucket_name) >> "$GITHUB_OUTPUT"
68+
./scripts/read_terragrunt_outputs.sh terragrunt/live/website
7869
7970
- name: Build Jekyll project
8071
run: |
@@ -93,4 +84,3 @@ jobs:
9384

9485
- name: Invalidate Cloudfront
9586
run: aws cloudfront create-invalidation --distribution-id ${{ steps.terragrunt_output.outputs.distribution_id }} --paths "/*"
96-

scripts/read_terragrunt_outputs.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Usage:
5+
# read_terragrunt_outputs.sh <terragrunt_module_path>
6+
#
7+
# Example:
8+
# read_terragrunt_outputs.sh terragrunt/live/website
9+
#
10+
# Outputs:
11+
# Writes distribution_id and bucket_name to $GITHUB_OUTPUT
12+
13+
MODULE_PATH="${1:-}"
14+
15+
if [[ -z "$MODULE_PATH" ]]; then
16+
echo "ERROR: Missing module path argument" >&2
17+
exit 1
18+
fi
19+
20+
cd "$MODULE_PATH"
21+
22+
# Extract JSON outputs
23+
OUT_JSON=$(terragrunt output --json)
24+
25+
# Parse values safely
26+
DISTRIBUTION_ID=$(echo "$OUT_JSON" | jq -r '.distribution_id.value // empty')
27+
BUCKET_NAME=$(echo "$OUT_JSON" | jq -r '.bucket_name.value // empty')
28+
29+
# Validate
30+
if [[ -z "$DISTRIBUTION_ID" ]]; then
31+
echo "ERROR: distribution_id output is missing or empty" >&2
32+
exit 1
33+
fi
34+
35+
if [[ -z "$BUCKET_NAME" ]]; then
36+
echo "ERROR: bucket_name output is missing or empty" >&2
37+
exit 1
38+
fi
39+
40+
# Export to GitHub Actions if available
41+
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
42+
echo "distribution_id=$DISTRIBUTION_ID" >> "$GITHUB_OUTPUT"
43+
echo "bucket_name=$BUCKET_NAME" >> "$GITHUB_OUTPUT"
44+
else
45+
echo "GITHUB_OUTPUT not set; printing values for local use"
46+
echo "distribution_id=$DISTRIBUTION_ID"
47+
echo "bucket_name=$BUCKET_NAME"
48+
fi

0 commit comments

Comments
 (0)