Skip to content

Commit 2dc9a8f

Browse files
committed
(fix) Revise teardown workflow logic
- Reconstitute backend state
1 parent 91041a8 commit 2dc9a8f

File tree

1 file changed

+103
-30
lines changed

1 file changed

+103
-30
lines changed

.github/workflows/teardown-environment.yml

Lines changed: 103 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,21 @@ jobs:
225225
name: kubeconfig-${{ github.event.inputs.environment }}
226226
path: ./
227227

228-
validate-backend-state:
229-
name: Validate Backend State
228+
ensure-backend-access:
229+
name: Ensure Backend Access
230230
runs-on: ubuntu-latest
231231
needs: validate-request
232232
if: needs.validate-request.outputs.confirmed == 'true'
233233
permissions:
234234
contents: read # Read repository code
235+
actions: read # Download artifacts from previous workflows
235236
outputs:
236237
backend_exists: ${{ steps.validate.outputs.backend_exists }}
237238
state_exists: ${{ steps.validate.outputs.state_exists }}
238239
bucket_name: ${{ steps.validate.outputs.bucket_name }}
239240
resource_count: ${{ steps.validate.outputs.resource_count }}
240241
state_valid: ${{ steps.validate.outputs.state_valid }}
242+
backend_source: ${{ steps.validate.outputs.backend_source }}
241243

242244
steps:
243245
- name: Checkout Code
@@ -249,8 +251,8 @@ jobs:
249251
terraform_version: "~1.12.0"
250252
terraform_wrapper: false
251253

252-
- name: Validate Backend Configuration
253-
id: validate
254+
- name: Setup Backend Infrastructure
255+
id: setup-backend
254256
run: |
255257
environment="${{ github.event.inputs.environment }}"
256258
region="${{ github.event.inputs.region }}"
@@ -261,14 +263,85 @@ jobs:
261263
bucket_name="terraform-state-coder-${environment}"
262264
fi
263265
264-
echo "🔍 Validating backend for environment: $environment"
266+
echo "🔧 Setting up backend infrastructure for environment: $environment"
265267
echo "Expected bucket: $bucket_name"
266268
269+
# Change to backend-setup directory
270+
cd backend-setup
271+
272+
# Create terraform.tfvars for this environment
273+
cat > terraform.tfvars << EOF
274+
environment = "$environment"
275+
region = "$region"
276+
project_id = "$SCW_DEFAULT_PROJECT_ID"
277+
generate_backend_config = true
278+
managed_by = "github-actions"
279+
EOF
280+
281+
if [[ -n "$bucket_name" ]]; then
282+
echo "bucket_name = \"$bucket_name\"" >> terraform.tfvars
283+
fi
284+
285+
# Initialize and apply Terraform
286+
echo "📦 Initializing Terraform..."
287+
if ! terraform init; then
288+
echo "❌ Failed to initialize Terraform for backend setup"
289+
echo "backend_setup_success=false" >> $GITHUB_OUTPUT
290+
exit 1
291+
fi
292+
293+
echo "🚀 Applying backend infrastructure..."
294+
if terraform apply -auto-approve; then
295+
echo "✅ Backend infrastructure created/verified for $environment"
296+
297+
# Extract outputs
298+
actual_bucket_name=$(terraform output -raw bucket_name)
299+
s3_endpoint=$(terraform output -raw s3_endpoint)
300+
301+
echo "📊 Backend setup outputs:"
302+
echo " Bucket: $actual_bucket_name"
303+
echo " Endpoint: $s3_endpoint"
304+
305+
echo "backend_setup_success=true" >> $GITHUB_OUTPUT
306+
echo "bucket_name=$actual_bucket_name" >> $GITHUB_OUTPUT
307+
echo "backend_endpoint=$s3_endpoint" >> $GITHUB_OUTPUT
308+
else
309+
echo "❌ Failed to apply backend infrastructure for $environment"
310+
echo "backend_setup_success=false" >> $GITHUB_OUTPUT
311+
exit 1
312+
fi
313+
314+
- name: Validate Backend Access
315+
id: validate
316+
run: |
317+
environment="${{ github.event.inputs.environment }}"
318+
region="${{ github.event.inputs.region }}"
319+
bucket_name="${{ github.event.inputs.bucket_name }}"
320+
321+
# Determine expected bucket name if not provided
322+
if [[ -z "$bucket_name" ]]; then
323+
bucket_name="terraform-state-coder-${environment}"
324+
fi
325+
326+
# Use bucket name from setup-backend if available
327+
if [[ "${{ steps.setup-backend.result }}" == "success" ]]; then
328+
bucket_name="${{ steps.setup-backend.outputs.bucket_name || '' }}"
329+
if [[ -z "$bucket_name" ]]; then
330+
bucket_name="terraform-state-coder-${environment}"
331+
fi
332+
echo "backend_source=generated" >> $GITHUB_OUTPUT
333+
else
334+
echo "backend_source=artifact" >> $GITHUB_OUTPUT
335+
fi
336+
337+
echo "🔍 Validating backend access for environment: $environment"
338+
echo "Backend bucket: $bucket_name"
339+
267340
# Check if backend configuration exists
268341
backend_file="environments/$environment/backend.tf"
269342
if [[ ! -f "$backend_file" ]]; then
270-
echo "❌ Backend configuration not found: $backend_file"
271-
echo "This environment may not have been deployed with remote state backend."
343+
echo "❌ Backend configuration still not found: $backend_file"
344+
echo "Failed to obtain backend configuration from artifacts or generation"
272345
echo "backend_exists=false" >> $GITHUB_OUTPUT
273346
echo "state_exists=false" >> $GITHUB_OUTPUT
274347
echo "bucket_name=$bucket_name" >> $GITHUB_OUTPUT
@@ -277,7 +350,7 @@ jobs:
277350
exit 1
278351
fi
279352
280-
echo "✅ Backend configuration found: $backend_file"
353+
echo "✅ Backend configuration available: $backend_file"
281354
echo "backend_exists=true" >> $GITHUB_OUTPUT
282355
283356
# Initialize Terraform with backend
@@ -346,11 +419,11 @@ jobs:
346419
prepare-teardown:
347420
name: Prepare Teardown
348421
runs-on: ubuntu-latest
349-
needs: [validate-request, validate-backend-state]
422+
needs: [validate-request, ensure-backend-access]
350423
if: |
351424
needs.validate-request.outputs.confirmed == 'true' &&
352-
needs.validate-backend-state.outputs.state_valid == 'true' &&
353-
needs.validate-backend-state.outputs.resource_count != '0'
425+
needs.ensure-backend-access.outputs.state_valid == 'true' &&
426+
needs.ensure-backend-access.outputs.resource_count != '0'
354427
permissions:
355428
contents: read # Read repository code
356429
outputs:
@@ -469,7 +542,7 @@ jobs:
469542
470543
# Verify resource count hasn't changed significantly
471544
current_count=$(terraform state list | wc -l)
472-
expected_count="${{ needs.validate-backend-state.outputs.resource_count }}"
545+
expected_count="${{ needs.ensure-backend-access.outputs.resource_count }}"
473546
474547
if [[ "$current_count" != "$expected_count" ]]; then
475548
difference=$((current_count - expected_count))
@@ -509,12 +582,12 @@ jobs:
509582
teardown:
510583
name: Execute Teardown
511584
runs-on: ubuntu-latest
512-
needs: [validate-request, validate-backend-state, prepare-teardown, analyze-impact]
585+
needs: [validate-request, ensure-backend-access, prepare-teardown, analyze-impact]
513586
if: |
514587
needs.validate-request.outputs.confirmed == 'true' &&
515-
needs.validate-backend-state.outputs.state_valid == 'true' &&
588+
needs.ensure-backend-access.outputs.state_valid == 'true' &&
516589
(needs.prepare-teardown.outputs.ready_for_teardown == 'true' ||
517-
needs.validate-backend-state.outputs.resource_count == '0') &&
590+
needs.ensure-backend-access.outputs.resource_count == '0') &&
518591
(needs.pre-teardown-backup.result == 'success' ||
519592
needs.pre-teardown-backup.result == 'skipped' ||
520593
github.event.inputs.backup_before_destroy == 'false')
@@ -558,13 +631,13 @@ jobs:
558631
run: |
559632
echo "🔴 FINAL PRODUCTION TEARDOWN CONFIRMATION 🔴"
560633
echo "About to PERMANENTLY DELETE the production environment!"
561-
echo "Resources in state: ${{ needs.validate-backend-state.outputs.resource_count }}"
634+
echo "Resources in state: ${{ needs.ensure-backend-access.outputs.resource_count }}"
562635
echo "Active workspaces: ${{ needs.analyze-impact.outputs.active_workspaces }}"
563636
echo "Monthly cost savings: €${{ needs.analyze-impact.outputs.cost_savings }}"
564637
echo ""
565638
echo "This action is IRREVERSIBLE!"
566639
567-
if [[ "${{ needs.validate-backend-state.outputs.resource_count }}" -eq 0 ]]; then
640+
if [[ "${{ needs.ensure-backend-access.outputs.resource_count }}" -eq 0 ]]; then
568641
echo "⚠️ WARNING: No resources found in state."
569642
echo "Environment may already be destroyed or was never deployed."
570643
fi
@@ -599,13 +672,13 @@ jobs:
599672
cd environments/${{ github.event.inputs.environment }}
600673
601674
echo "🔄 Initializing with remote backend..."
602-
echo "Backend bucket: ${{ needs.validate-backend-state.outputs.bucket_name }}"
603-
echo "Resources to destroy: ${{ needs.validate-backend-state.outputs.resource_count }}"
675+
echo "Backend bucket: ${{ needs.ensure-backend-access.outputs.bucket_name }}"
676+
echo "Resources to destroy: ${{ needs.ensure-backend-access.outputs.resource_count }}"
604677
terraform init
605678
606679
# Verify state matches what we validated earlier
607680
current_resources=$(terraform state list 2>/dev/null | wc -l || echo "0")
608-
expected_resources="${{ needs.validate-backend-state.outputs.resource_count }}"
681+
expected_resources="${{ needs.ensure-backend-access.outputs.resource_count }}"
609682
610683
if [[ "$current_resources" != "$expected_resources" ]]; then
611684
echo "⚠️ WARNING: Resource count mismatch!"
@@ -766,7 +839,7 @@ jobs:
766839
# Note: We don't delete the bucket itself as it may be used for other environments
767840
# But we could clean up the specific state file for this environment
768841
echo "State backend cleanup would be implemented here"
769-
echo "Bucket: ${{ needs.validate-backend-state.outputs.bucket_name }}"
842+
echo "Bucket: ${{ needs.ensure-backend-access.outputs.bucket_name }}"
770843
echo "State key: ${{ github.event.inputs.environment }}/terraform.tfstate"
771844
772845
# For now, just log what we would clean up
@@ -775,7 +848,7 @@ jobs:
775848
post-teardown:
776849
name: Post-Teardown Actions
777850
runs-on: ubuntu-latest
778-
needs: [validate-request, validate-backend-state, prepare-teardown, analyze-impact, teardown]
851+
needs: [validate-request, ensure-backend-access, prepare-teardown, analyze-impact, teardown]
779852
if: always() && needs.validate-request.outputs.confirmed == 'true'
780853
permissions:
781854
contents: read # Read repository information
@@ -810,9 +883,9 @@ jobs:
810883
**Verification Status:** $verification_status
811884
812885
## Pre-Teardown Analysis
813-
- **Backend Validation:** ${{ needs.validate-backend-state.result }}
814-
- **Resources Found:** ${{ needs.validate-backend-state.outputs.resource_count }}
815-
- **State Backend:** ${{ needs.validate-backend-state.outputs.bucket_name }}
886+
- **Backend Validation:** ${{ needs.ensure-backend-access.result }}
887+
- **Resources Found:** ${{ needs.ensure-backend-access.outputs.resource_count }}
888+
- **State Backend:** ${{ needs.ensure-backend-access.outputs.bucket_name }}
816889
- **Preparation Status:** ${{ needs.prepare-teardown.result }}
817890
- **Drift Detected:** ${{ needs.prepare-teardown.outputs.drift_detected || 'N/A' }}
818891
- **Active Workspaces:** ${{ needs.analyze-impact.outputs.active_workspaces }}
@@ -876,12 +949,12 @@ jobs:
876949
- **Workflow Run ID:** ${{ github.run_id }}
877950
- **Repository:** ${{ github.repository }}
878951
- **Commit SHA:** ${{ github.sha }}
879-
- **Backend State Bucket:** ${{ needs.validate-backend-state.outputs.bucket_name }}
952+
- **Backend State Bucket:** ${{ needs.ensure-backend-access.outputs.bucket_name }}
880953
- **State Key:** ${{ github.event.inputs.environment }}/terraform.tfstate
881954
882955
## Workflow Job Results
883956
- **Validation:** ${{ needs.validate-request.result }}
884-
- **Backend State Check:** ${{ needs.validate-backend-state.result }}
957+
- **Backend State Check:** ${{ needs.ensure-backend-access.result }}
885958
- **Preparation:** ${{ needs.prepare-teardown.result }}
886959
- **Impact Analysis:** ${{ needs.analyze-impact.result }}
887960
- **Teardown Execution:** ${{ needs.teardown.result }}
@@ -948,8 +1021,8 @@ jobs:
9481021
const teardownCompleted = '${{ needs.teardown.outputs.teardown_completed }}';
9491022
const verificationStatus = '${{ needs.teardown.outputs.verification_status }}';
9501023
const remainingResources = '${{ needs.teardown.outputs.remaining_resources }}';
951-
const backendValidation = '${{ needs.validate-backend-state.result }}';
952-
const resourcesFound = '${{ needs.validate-backend-state.outputs.resource_count }}';
1024+
const backendValidation = '${{ needs.ensure-backend-access.result }}';
1025+
const resourcesFound = '${{ needs.ensure-backend-access.outputs.resource_count }}';
9531026
9541027
const body = `## Teardown Failure - Manual Cleanup Required
9551028
@@ -980,7 +1053,7 @@ jobs:
9801053
4. **🔄 Consider re-running** the teardown workflow after manual cleanup
9811054
9821055
## State Backend Information
983-
- **Bucket:** ${{ needs.validate-backend-state.outputs.bucket_name }}
1056+
- **Bucket:** ${{ needs.ensure-backend-access.outputs.bucket_name }}
9841057
- **State Key:** ${{ github.event.inputs.environment }}/terraform.tfstate
9851058
- **Region:** ${{ github.event.inputs.region }}
9861059

0 commit comments

Comments
 (0)