Destroy Infrastructure (PROD) #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 (PROD) | |
| # Manually destroys PROD infrastructure from main after environment approval. | |
| # Uses PROD-specific credentials and verifies the expected Kubernetes state. | |
| # Removes frontend before network using saved destruction plans. | |
| # Shares the deployment concurrency lock to prevent overlapping operations. | |
| on: | |
| workflow_dispatch: | |
| concurrency: | |
| group: infrastructure-deployment | |
| cancel-in-progress: false | |
| permissions: | |
| contents: read | |
| jobs: | |
| destroy: | |
| name: Destroy PROD | |
| if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main' | |
| runs-on: [self-hosted, linux, assignment2] | |
| timeout-minutes: 60 | |
| # Reuse the existing PROD environment's required reviewer approval and secrets. | |
| environment: PROD | |
| 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: prod | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v5 | |
| with: | |
| persist-credentials: false | |
| - 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 | |
| # Fail closed if the target is ever changed to a non-PROD environment. | |
| test "$TARGET_ENV" = "prod" | |
| : "${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" | |
| - 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: | |
| 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 | |
| - 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}" | |
| # Preserve reverse dependency order: frontend must be removed before network. | |
| 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 if the backend is not the expected PROD Kubernetes state. | |
| grep -Eq "secret_suffix[[:space:]]*=[[:space:]]*\"assignment2-$TARGET_ENV-$STACK\"" \ | |
| "$STACK_DIR/providers.tf" | |
| echo "::group::Destroy $TARGET_ENV $STACK" | |
| # Initialize the existing Kubernetes backend. | |
| tofu -chdir="$STACK_DIR" init \ | |
| -reconfigure \ | |
| -input=false \ | |
| -lockfile=readonly \ | |
| -no-color | |
| # Generate a destruction plan. | |
| tofu -chdir="$STACK_DIR" plan \ | |
| -destroy \ | |
| -var-file="deployment.tfvars" \ | |
| -out="$PLAN_FILE" \ | |
| -lock-timeout=5m \ | |
| -input=false \ | |
| -no-color | |
| # Display resource addresses being deleted. | |
| tofu -chdir="$STACK_DIR" show -json "$PLAN_FILE" > "$PLAN_JSON" | |
| jq -r ' | |
| .resource_changes[]? | |
| | select(.change.actions | index("delete")) | |
| | "Destroy: \(.address)" | |
| ' "$PLAN_JSON" | |
| # Apply the saved destruction plan. | |
| 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 |