Skip to content

Commit 11b7c41

Browse files
committed
#8 add Terraform plan debugging and timeout
1 parent 165e049 commit 11b7c41

1 file changed

Lines changed: 86 additions & 2 deletions

File tree

.github/workflows/pr-checks.yml

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,39 @@ jobs:
6767
6868
- name: Terraform Plan
6969
working-directory: ./infra/terraform/dev
70+
timeout-minutes: 5
7071
run: |
71-
terraform plan -no-color > plan_full.txt || { exit_code=$?; cat plan_full.txt; echo "plan<<EOF" >> $GITHUB_OUTPUT; cat plan_full.txt >> $GITHUB_OUTPUT; echo "EOF" >> $GITHUB_OUTPUT; echo "has_changes=true" >> $GITHUB_OUTPUT; exit $exit_code; }
72+
echo "Starting Terraform plan at $(date)"
73+
echo "Current working directory: $(pwd)"
74+
echo "Terraform version: $(terraform version)"
75+
echo "AWS region: $AWS_REGION"
76+
echo "Checking state lock status..."
77+
78+
# Add debugging for state backend
79+
terraform show -json 2>/dev/null | jq -r '.backend // "No backend info"' || echo "Could not query backend info"
80+
81+
# Enable verbose logging
82+
export TF_LOG=DEBUG
83+
export TF_LOG_PATH=/tmp/terraform.log
84+
85+
echo "Running terraform plan with timeout and debugging..."
86+
timeout 240 terraform plan -no-color -lock-timeout=60s > plan_full.txt 2>&1 || {
87+
exit_code=$?
88+
echo "Terraform plan failed with exit code: $exit_code"
89+
echo "=== Plan output ==="
90+
cat plan_full.txt
91+
echo "=== Terraform debug logs (last 50 lines) ==="
92+
tail -n 50 /tmp/terraform.log 2>/dev/null || echo "No debug logs available"
93+
echo "=== Current processes ==="
94+
ps aux | grep -E "(terraform|aws)" | grep -v grep || echo "No terraform/aws processes found"
95+
echo "plan<<EOF" >> $GITHUB_OUTPUT
96+
cat plan_full.txt >> $GITHUB_OUTPUT
97+
echo "EOF" >> $GITHUB_OUTPUT
98+
echo "has_changes=true" >> $GITHUB_OUTPUT
99+
exit $exit_code
100+
}
101+
102+
echo "Terraform plan completed successfully at $(date)"
72103
cat plan_full.txt
73104
74105
# Check if there are any changes planned
@@ -133,14 +164,67 @@ jobs:
133164
working-directory: ./infra/terraform/prod
134165
run: terraform init
135166

167+
- name: Check and Clear State Locks
168+
working-directory: ./infra/terraform/prod
169+
run: |
170+
echo "Checking for existing state locks in prod account..."
171+
# Try to acquire a lock briefly to see if one exists
172+
if ! timeout 10 terraform plan -detailed-exitcode >/dev/null 2>&1; then
173+
echo "Terraform operation failed, checking if it's a lock issue..."
174+
175+
# Check the state file directly for lock info
176+
if aws s3 cp s3://jaildata-tf-state/terraform.tfstate - 2>/dev/null | jq -e '.lineage' >/dev/null 2>&1; then
177+
echo "State file exists and is readable"
178+
else
179+
echo "State file may have issues"
180+
fi
181+
182+
# Try to force unlock any existing locks
183+
echo "Attempting to clear any existing locks..."
184+
terraform force-unlock -force $(aws s3 cp s3://jaildata-tf-state/.terraform/terraform.tfstate.lock.info - 2>/dev/null | jq -r '.ID // empty') 2>/dev/null || echo "No lock file found or unable to unlock"
185+
else
186+
echo "No lock detected"
187+
fi
188+
136189
- name: Set Terraform environment variables
137190
run: |
138191
echo "TF_VAR_alert_email=${{ vars.ALERT_EMAIL }}" >> $GITHUB_ENV
139192
140193
- name: Terraform Plan
141194
working-directory: ./infra/terraform/prod
195+
timeout-minutes: 5
142196
run: |
143-
terraform plan -no-color > plan_full.txt || { exit_code=$?; cat plan_full.txt; echo "plan<<EOF" >> $GITHUB_OUTPUT; cat plan_full.txt >> $GITHUB_OUTPUT; echo "EOF" >> $GITHUB_OUTPUT; echo "has_changes=true" >> $GITHUB_OUTPUT; exit $exit_code; }
197+
echo "Starting Terraform plan at $(date)"
198+
echo "Current working directory: $(pwd)"
199+
echo "Terraform version: $(terraform version)"
200+
echo "AWS region: $AWS_REGION"
201+
echo "Checking state lock status..."
202+
203+
# Add debugging for state backend
204+
terraform show -json 2>/dev/null | jq -r '.backend // "No backend info"' || echo "Could not query backend info"
205+
206+
# Enable verbose logging
207+
export TF_LOG=DEBUG
208+
export TF_LOG_PATH=/tmp/terraform.log
209+
210+
echo "Running terraform plan with timeout and debugging..."
211+
timeout 240 terraform plan -no-color -lock-timeout=60s > plan_full.txt 2>&1 || {
212+
exit_code=$?
213+
echo "Terraform plan failed with exit code: $exit_code"
214+
echo "=== Plan output ==="
215+
cat plan_full.txt
216+
echo "=== Terraform debug logs (last 50 lines) ==="
217+
tail -n 50 /tmp/terraform.log 2>/dev/null || echo "No debug logs available"
218+
echo "=== Current processes ==="
219+
ps aux | grep -E "(terraform|aws)" | grep -v grep || echo "No terraform/aws processes found"
220+
echo "plan<<EOF" >> $GITHUB_OUTPUT
221+
cat plan_full.txt >> $GITHUB_OUTPUT
222+
echo "EOF" >> $GITHUB_OUTPUT
223+
echo "has_changes=true" >> $GITHUB_OUTPUT
224+
exit $exit_code
225+
}
226+
227+
echo "Terraform plan completed successfully at $(date)"
144228
cat plan_full.txt
145229
146230
# Check if there are any changes planned

0 commit comments

Comments
 (0)