113 lines
3.8 KiB
YAML
113 lines
3.8 KiB
YAML
name: Destroy
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
inputs:
|
|
confirm:
|
|
description: 'Type "destroy" to confirm'
|
|
required: true
|
|
default: ''
|
|
|
|
concurrency:
|
|
group: prod-cluster
|
|
cancel-in-progress: false
|
|
|
|
env:
|
|
TF_VERSION: "1.7.0"
|
|
TF_VAR_s3_access_key: ${{ secrets.S3_ACCESS_KEY }}
|
|
TF_VAR_s3_secret_key: ${{ secrets.S3_SECRET_KEY }}
|
|
TF_VAR_s3_endpoint: ${{ secrets.S3_ENDPOINT }}
|
|
TF_VAR_s3_bucket: ${{ secrets.S3_BUCKET }}
|
|
TF_VAR_tailscale_tailnet: ${{ secrets.TAILSCALE_TAILNET }}
|
|
TF_VAR_proxmox_endpoint: ${{ secrets.PROXMOX_ENDPOINT }}
|
|
TF_VAR_proxmox_api_token_id: ${{ secrets.PROXMOX_API_TOKEN_ID }}
|
|
TF_VAR_proxmox_api_token_secret: ${{ secrets.PROXMOX_API_TOKEN_SECRET }}
|
|
TF_VAR_proxmox_insecure: "true"
|
|
|
|
jobs:
|
|
destroy:
|
|
name: Destroy Cluster
|
|
runs-on: ubuntu-22.04
|
|
if: github.event.inputs.confirm == 'destroy'
|
|
environment: destroy
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Setup Terraform
|
|
uses: hashicorp/setup-terraform@v3
|
|
with:
|
|
terraform_version: ${{ env.TF_VERSION }}
|
|
|
|
- name: Setup SSH Keys
|
|
run: |
|
|
mkdir -p ~/.ssh
|
|
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
|
|
chmod 600 ~/.ssh/id_ed25519
|
|
echo "${{ secrets.SSH_PUBLIC_KEY }}" > ~/.ssh/id_ed25519.pub
|
|
chmod 644 ~/.ssh/id_ed25519.pub
|
|
|
|
- name: Terraform Init
|
|
working-directory: terraform
|
|
run: |
|
|
terraform init \
|
|
-lockfile=readonly \
|
|
-backend-config="endpoint=${{ secrets.S3_ENDPOINT }}" \
|
|
-backend-config="bucket=${{ secrets.S3_BUCKET }}" \
|
|
-backend-config="region=auto" \
|
|
-backend-config="access_key=${{ secrets.S3_ACCESS_KEY }}" \
|
|
-backend-config="secret_key=${{ secrets.S3_SECRET_KEY }}" \
|
|
-backend-config="skip_requesting_account_id=true"
|
|
|
|
- name: Save Proxmox target list
|
|
run: |
|
|
mkdir -p outputs
|
|
if ! terraform -chdir=terraform output -json proxmox_target_vms > outputs/proxmox_target_vms.json; then
|
|
terraform -chdir=terraform plan \
|
|
-refresh=false \
|
|
-var="ssh_public_key=$HOME/.ssh/id_ed25519.pub" \
|
|
-var="ssh_private_key=$HOME/.ssh/id_ed25519" \
|
|
-out=cleanup.tfplan \
|
|
-no-color || true
|
|
printf '[]' > outputs/proxmox_target_vms.json
|
|
fi
|
|
|
|
- name: Terraform Destroy
|
|
id: destroy
|
|
working-directory: terraform
|
|
run: |
|
|
set +e
|
|
for attempt in 1 2 3; do
|
|
echo "Terraform destroy attempt ${attempt}/3"
|
|
terraform destroy \
|
|
-parallelism=2 \
|
|
-var="ssh_public_key=$HOME/.ssh/id_ed25519.pub" \
|
|
-var="ssh_private_key=$HOME/.ssh/id_ed25519" \
|
|
-auto-approve
|
|
rc=$?
|
|
if [ "$rc" -eq 0 ]; then
|
|
exit 0
|
|
fi
|
|
if [ "$attempt" -lt 3 ]; then
|
|
echo "Terraform destroy failed with exit code ${rc}; retrying in 30s"
|
|
sleep 30
|
|
terraform refresh \
|
|
-var="ssh_public_key=$HOME/.ssh/id_ed25519.pub" \
|
|
-var="ssh_private_key=$HOME/.ssh/id_ed25519" || true
|
|
fi
|
|
done
|
|
exit "$rc"
|
|
|
|
- name: Verify Proxmox target VMs removed
|
|
if: success()
|
|
run: |
|
|
python3 scripts/proxmox-rebuild-cleanup.py --mode post-destroy --targets-file outputs/proxmox_target_vms.json
|
|
if [ -f terraform/cleanup.tfplan ]; then
|
|
python3 scripts/proxmox-rebuild-cleanup.py --mode post-destroy --terraform-dir terraform --plan cleanup.tfplan
|
|
fi
|
|
|
|
- name: Terraform state diagnostics
|
|
if: failure() && steps.destroy.outcome == 'failure'
|
|
run: |
|
|
terraform -chdir=terraform state list || true
|