Skip to content

Commit 25683ee

Browse files
committed
Create infrastructure destroy workflow
- Add destroy.yml workflow for infrastructure destruction - Include validation step requiring 'destroy' confirmation - Support multiple environments (dev, staging, prod) - Automatically mark GitHub deployments as inactive after destroy
1 parent ad60943 commit 25683ee

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

.github/workflows/destroy.yml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
name: Destroy Infrastructure
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
environment:
7+
description: 'Environment to destroy'
8+
required: true
9+
default: 'dev'
10+
type: choice
11+
options:
12+
- dev
13+
- staging
14+
- prod
15+
confirm:
16+
description: 'Type "destroy" to confirm'
17+
required: true
18+
type: string
19+
20+
permissions:
21+
contents: read
22+
deployments: write
23+
24+
jobs:
25+
validate:
26+
name: Validate Destroy Request
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Validate confirmation
30+
run: |
31+
if [ "${{ inputs.confirm }}" != "destroy" ]; then
32+
echo "❌ Confirmation failed. You must type 'destroy' to proceed."
33+
exit 1
34+
fi
35+
echo "✅ Destroy confirmed"
36+
37+
set-variables:
38+
name: Set Variables
39+
runs-on: ubuntu-latest
40+
needs: validate
41+
outputs:
42+
aws_region: ${{ steps.vars.outputs.aws_region }}
43+
domain_name: ${{ steps.vars.outputs.domain_name }}
44+
subdomain: ${{ steps.vars.outputs.subdomain }}
45+
46+
steps:
47+
- id: vars
48+
run: |
49+
AWS_REGION="${{ vars.AWS_REGION || 'us-east-1' }}"
50+
DOMAIN_NAME="${{ vars.DOMAIN_NAME || 'suhailskhan.com' }}"
51+
SUBDOMAIN="${{ vars.SUBDOMAIN || 'ai-usage-log' }}"
52+
53+
echo "aws_region=$AWS_REGION" >> $GITHUB_OUTPUT
54+
echo "domain_name=$DOMAIN_NAME" >> $GITHUB_OUTPUT
55+
echo "subdomain=$SUBDOMAIN" >> $GITHUB_OUTPUT
56+
57+
destroy:
58+
name: Destroy Infrastructure
59+
needs: [validate, set-variables]
60+
runs-on: ubuntu-latest
61+
62+
steps:
63+
- name: Checkout code
64+
uses: actions/checkout@v4
65+
66+
- name: Set up OpenTofu
67+
uses: opentofu/setup-opentofu@v1
68+
69+
- name: Configure AWS credentials
70+
uses: aws-actions/configure-aws-credentials@v4
71+
with:
72+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
73+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
74+
aws-region: ${{ needs.set-variables.outputs.aws_region }}
75+
76+
- name: Initialize Tofu
77+
run: tofu -chdir=infra init
78+
79+
- name: Tofu Destroy
80+
run: |
81+
tofu -chdir=infra destroy -auto-approve \
82+
-var="aws_region=${{ needs.set-variables.outputs.aws_region }}" \
83+
-var="environment=${{ inputs.environment }}" \
84+
-var="cloudflare_api_token=${{ secrets.CLOUDFLARE_API_TOKEN }}" \
85+
-var="cloudflare_zone_id=${{ secrets.CLOUDFLARE_ZONE_ID }}" \
86+
-var="domain_name=${{ needs.set-variables.outputs.domain_name }}" \
87+
-var="subdomain=${{ needs.set-variables.outputs.subdomain }}"
88+
89+
- name: Mark deployments as inactive
90+
uses: actions/github-script@v7
91+
with:
92+
script: |
93+
const deployments = await github.rest.repos.listDeployments({
94+
owner: context.repo.owner,
95+
repo: context.repo.repo,
96+
environment: '${{ inputs.environment }}'
97+
});
98+
99+
for (const deployment of deployments.data) {
100+
await github.rest.repos.createDeploymentStatus({
101+
owner: context.repo.owner,
102+
repo: context.repo.repo,
103+
deployment_id: deployment.id,
104+
state: 'inactive',
105+
description: 'Infrastructure destroyed'
106+
});
107+
}

.github/workflows/release-build-and-deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ jobs:
130130
uses: actions/checkout@v4
131131

132132
- name: Set up OpenTofu
133-
uses: hashicorp/setup-opentofu@v1
133+
uses: opentofu/setup-opentofu@v1
134134

135135
- name: Configure AWS credentials
136136
uses: aws-actions/configure-aws-credentials@v4

0 commit comments

Comments
 (0)