Destroy Infrastructure (TEST) #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Destroy Infrastructure (TEST) | |
| # Manually destroys TEST infrastructure from main. | |
| # Removes frontend before network to respect dependencies. | |
| # Verifies the remote state target before applying a saved destroy plan. | |
| on: | |
| workflow_dispatch: | |
| concurrency: | |
| group: infrastructure-deployment | |
| cancel-in-progress: false | |
| permissions: | |
| contents: read | |
| jobs: | |
| destroy: | |
| name: Destroy TEST | |
| if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main' | |
| runs-on: [self-hosted, linux, assignment2] | |
| timeout-minutes: 60 | |
| environment: TEST | |
| env: | |
| OS_AUTH_TYPE: v3applicationcredential | |
| OS_AUTH_URL: ${{ vars.OS_AUTH_URL }} | |
| OS_REGION_NAME: ${{ vars.OS_REGION_NAME }} | |
| OS_IDENTITY_API_VERSION: "3" | |
| OS_INTERFACE: public | |
| OS_APPLICATION_CREDENTIAL_ID: ${{ secrets.OS_APPLICATION_CREDENTIAL_ID }} | |
| OS_APPLICATION_CREDENTIAL_SECRET: ${{ secrets.OS_APPLICATION_CREDENTIAL_SECRET }} | |
| KOPS_STATE_STORE: swift://demo-kops | |
| TARGET_ENV: test | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v5 | |
| with: | |
| persist-credentials: false | |
| # Use the same OpenTofu version as the CI workflow. | |
| - name: Setup OpenTofu | |
| uses: opentofu/setup-opentofu@v2 | |
| with: | |
| tofu_version_file: .opentofu-version | |
| tofu_wrapper: false | |
| - name: Verify tools and configuration | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| tofu version | |
| command -v jq | |
| test "$TARGET_ENV" = "test" | |
| : "${OS_AUTH_URL:?Missing OS_AUTH_URL}" | |
| : "${OS_REGION_NAME:?Missing OS_REGION_NAME}" | |
| : "${OS_APPLICATION_CREDENTIAL_ID:?Missing application credential ID}" | |
| : "${OS_APPLICATION_CREDENTIAL_SECRET:?Missing application credential secret}" | |
| test -f "environments/$TARGET_ENV/frontend/deployment.tfvars" | |
| test -f "environments/$TARGET_ENV/network/deployment.tfvars" | |
| # Reuse the pinned, checksum-verified installation approach from deploy.yml. | |
| - name: Install kOps and kubectl | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| BIN_DIR="$RUNNER_TEMP/tofu-destroy-tools" | |
| mkdir -p "$BIN_DIR" | |
| KUBECTL_VERSION="v1.36.4" | |
| curl -fsSL -o "$BIN_DIR/kubectl" \ | |
| "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl" | |
| curl -fsSL -o "$BIN_DIR/kubectl.sha256" \ | |
| "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl.sha256" | |
| (cd "$BIN_DIR" && echo "$(cat kubectl.sha256) kubectl" | sha256sum --check) | |
| KOPS_VERSION="v1.36.2" | |
| curl -fsSL -o "$BIN_DIR/kops" \ | |
| "https://github.com/kubernetes/kops/releases/download/${KOPS_VERSION}/kops-linux-amd64" | |
| curl -fsSL -o "$BIN_DIR/kops.sha256" \ | |
| "https://github.com/kubernetes/kops/releases/download/${KOPS_VERSION}/kops-linux-amd64.sha256" | |
| (cd "$BIN_DIR" && echo "$(cat kops.sha256) kops" | sha256sum --check) | |
| chmod +x "$BIN_DIR/kubectl" "$BIN_DIR/kops" | |
| echo "$BIN_DIR" >> "$GITHUB_PATH" | |
| - name: Export temporary Kubernetes kubeconfig | |
| shell: bash | |
| env: | |
| # kOps state in Swift uses different credentials from resource deletion. | |
| OS_APPLICATION_CREDENTIAL_ID: ${{ secrets.KOPS_APPLICATION_CREDENTIAL_ID }} | |
| OS_APPLICATION_CREDENTIAL_SECRET: ${{ secrets.KOPS_APPLICATION_CREDENTIAL_SECRET }} | |
| run: | | |
| set -euo pipefail | |
| : "${OS_APPLICATION_CREDENTIAL_ID:?Missing kOps application credential ID}" | |
| : "${OS_APPLICATION_CREDENTIAL_SECRET:?Missing kOps application credential secret}" | |
| KUBECONFIG_FILE="$RUNNER_TEMP/destroy-$TARGET_ENV-kubeconfig" | |
| umask 077 | |
| kops get cluster einalien.k8s.local --state="$KOPS_STATE_STORE" > /dev/null | |
| kops export kubeconfig einalien.k8s.local \ | |
| --admin=1h \ | |
| --state="$KOPS_STATE_STORE" \ | |
| --kubeconfig="$KUBECONFIG_FILE" | |
| test -s "$KUBECONFIG_FILE" | |
| chmod 600 "$KUBECONFIG_FILE" | |
| echo "KUBECONFIG=$KUBECONFIG_FILE" >> "$GITHUB_ENV" | |
| echo "KUBE_CONFIG_PATH=$KUBECONFIG_FILE" >> "$GITHUB_ENV" | |
| - name: Check Kubernetes backend access | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| kubectl --request-timeout=15s get namespace iac | |
| kubectl auth can-i get secrets -n iac --quiet | |
| kubectl auth can-i create leases.coordination.k8s.io -n iac --quiet | |
| # A saved destroy plan is more auditable than direct destroy -auto-approve. | |
| - name: Destroy frontend then network | |
| shell: bash | |
| env: | |
| TF_IN_AUTOMATION: "true" | |
| TF_VAR_admin_cidr: ${{ secrets.ADMIN_CIDR }} | |
| TF_VAR_ssh_key_name: ${{ vars.SSH_KEY_NAME }} | |
| run: | | |
| set -euo pipefail | |
| umask 077 | |
| : "${TF_VAR_admin_cidr:?Missing ADMIN_CIDR}" | |
| : "${TF_VAR_ssh_key_name:?Missing SSH_KEY_NAME}" | |
| for STACK in frontend network; do | |
| STACK_DIR="environments/$TARGET_ENV/$STACK" | |
| PLAN_FILE="$RUNNER_TEMP/destroy-$TARGET_ENV-$STACK.tfplan" | |
| PLAN_JSON="$RUNNER_TEMP/destroy-$TARGET_ENV-$STACK.json" | |
| # Fail closed if a stack's backend points outside TEST. | |
| grep -Eq "secret_suffix[[:space:]]*=[[:space:]]*\"assignment2-test-$STACK\"" \ | |
| "$STACK_DIR/providers.tf" | |
| echo "::group::Destroy $TARGET_ENV $STACK" | |
| tofu -chdir="$STACK_DIR" init \ | |
| -reconfigure -input=false -lockfile=readonly -no-color | |
| tofu -chdir="$STACK_DIR" plan \ | |
| -destroy \ | |
| -var-file="deployment.tfvars" \ | |
| -out="$PLAN_FILE" \ | |
| -lock-timeout=5m \ | |
| -input=false -no-color | |
| # Show only resource addresses/actions, not sensitive attribute values. | |
| tofu -chdir="$STACK_DIR" show -json "$PLAN_FILE" > "$PLAN_JSON" | |
| jq -r '.resource_changes[]? | select(.change.actions | index("delete")) | "Destroy: \(.address)"' "$PLAN_JSON" | |
| # Applying the saved plan is noninteractive, just like -auto-approve. | |
| tofu -chdir="$STACK_DIR" apply \ | |
| -input=false -no-color -lock-timeout=5m "$PLAN_FILE" | |
| rm -f "$PLAN_FILE" "$PLAN_JSON" | |
| echo "::endgroup::" | |
| done | |
| # Remove temporary Kubernetes credentials and saved destruction plans. | |
| - name: Clean up temporary files | |
| if: always() | |
| shell: bash | |
| run: | | |
| rm -f "$RUNNER_TEMP/destroy-$TARGET_ENV-kubeconfig" | |
| rm -f "$RUNNER_TEMP"/destroy-"$TARGET_ENV"-*.tfplan | |
| rm -f "$RUNNER_TEMP"/destroy-"$TARGET_ENV"-*.json |