Skip to content

Commit 6f28e9d

Browse files
committed
Fix error the terraform output -json command is being called incorrectly
1 parent ff52459 commit 6f28e9d

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

.github/workflows/terraform.yml

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,36 +201,38 @@ jobs:
201201
run: |
202202
cd infra
203203
204-
# Get raw JSON output (with error handling)
205-
if ! terraform output -json > outputs.json; then
206-
echo "::error::Terraform output command failed"
204+
# Get the full path to terraform binary
205+
TF_PATH=$(which terraform)
206+
echo "Using Terraform at: $TF_PATH"
207+
208+
# Execute terraform output properly
209+
$TF_PATH output -json > outputs.json
210+
211+
# Verify JSON file exists and is valid
212+
if [[ ! -f outputs.json ]]; then
213+
echo "::error::Terraform failed to create outputs.json"
207214
exit 1
208215
fi
209216
210-
# Verify JSON is valid
211217
if ! jq empty outputs.json; then
212-
echo "::error::Invalid JSON output from Terraform"
218+
echo "::error::Invalid JSON in outputs.json"
213219
cat outputs.json
214220
exit 1
215221
fi
216222
217-
# Extract values with strict validation
218-
APP_NAME=$(jq -r '.app_service_name.value // empty' outputs.json)
219-
RG_NAME=$(jq -r '.resource_group_name.value // empty' outputs.json)
220-
API_URL=$(jq -r '.lb_api_url.value // empty' outputs.json)
223+
# Extract values safely
224+
APP_NAME=$(jq -r '.app_service_name.value' outputs.json)
225+
RG_NAME=$(jq -r '.resource_group_name.value' outputs.json)
226+
API_URL=$(jq -r '.lb_api_url.value' outputs.json)
221227
222-
# Verify all values exist
228+
# Verify values
223229
if [[ -z "$APP_NAME" || -z "$RG_NAME" || -z "$API_URL" ]]; then
224-
echo "::error::Missing required outputs from Terraform"
230+
echo "::error::Missing required values in outputs"
225231
jq . outputs.json
226232
exit 1
227233
fi
228234
229-
# Sanitize values (remove any special characters)
230-
APP_NAME=$(echo "$APP_NAME" | tr -cd '[:alnum:]-_')
231-
RG_NAME=$(echo "$RG_NAME" | tr -cd '[:alnum:]-_')
232-
API_URL=$(echo "$API_URL" | tr -cd '[:alnum:]:/.')
233-
235+
# Set outputs
234236
echo "webapp_name=${APP_NAME}" >> $GITHUB_OUTPUT
235237
echo "resource_group=${RG_NAME}" >> $GITHUB_OUTPUT
236238
echo "api_url=${API_URL}" >> $GITHUB_OUTPUT

ansible/deploy-frontend.yml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,17 @@
33
hosts: localhost
44
connection: local
55
vars:
6-
app_service_name: "{{ lookup('env', 'APP_SERVICE_NAME') }}"
7-
resource_group_name: "{{ lookup('env', 'RESOURCE_GROUP_NAME') }}"
8-
lb_api_url: "{{ lookup('env', 'LB_API_URL') }}"
96
node_version: "16-lts"
107

118
tasks:
12-
- name: Validate environment variables
13-
fail:
14-
msg: |
15-
Missing required environment variables!
16-
- APP_SERVICE_NAME: {{ lookup('env', 'APP_SERVICE_NAME') }}
17-
- RESOURCE_GROUP_NAME: {{ lookup('env', 'RESOURCE_GROUP_NAME') }}
18-
- LB_API_URL: {{ lookup('env', 'LB_API_URL') }}
19-
when: >
20-
not lookup('env', 'APP_SERVICE_NAME') or
21-
not lookup('env', 'RESOURCE_GROUP_NAME') or
22-
not lookup('env', 'LB_API_URL')
9+
- name: Validate required variables
10+
assert:
11+
that:
12+
- app_service_name is defined
13+
- resource_group_name is defined
14+
- lb_api_url is defined
15+
fail_msg: "Missing required variables from GitHub Actions"
16+
success_msg: "All required variables present"
2317

2418
- name: Show deployment configuration
2519
debug:

0 commit comments

Comments
 (0)