From 0f1c754631053322e3e3480591ae02684ef630c9 Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sun, 8 Nov 2026 18:15:53 +0100 Subject: [PATCH 1/3] chore(tofu): add version file --- .opentofu-version | 1 + 1 file changed, 1 insertion(+) create mode 100644 .opentofu-version diff --git a/.opentofu-version b/.opentofu-version new file mode 100644 index 0000000..6d2ee2d --- /dev/null +++ b/.opentofu-version @@ -0,0 +1 @@ +1.12.6 \ No newline at end of file From 9ddea21e3821758d62fa0ad8b2391ba5b61e83f2 Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sun, 8 Nov 2026 18:18:13 +0100 Subject: [PATCH 2/3] ci(cd): add TEST destruction workflow --- .github/workflows/destroy.yml | 205 ++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 .github/workflows/destroy.yml diff --git a/.github/workflows/destroy.yml b/.github/workflows/destroy.yml new file mode 100644 index 0000000..6b53856 --- /dev/null +++ b/.github/workflows/destroy.yml @@ -0,0 +1,205 @@ +name: Destroy Infrastructure (TEST) + +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 + + - 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" + + - 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 TEST 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" + + # Verify that the backend belongs to TEST. + grep -Eq "secret_suffix[[:space:]]*=[[:space:]]*\"assignment2-test-$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 + + - 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 From 090d8fc78c0097156263d85386d8507f9914a0fb Mon Sep 17 00:00:00 2001 From: eili1s <183522504+eili1s@users.noreply.github.com> Date: Sun, 8 Nov 2026 18:29:31 +0100 Subject: [PATCH 3/3] ci: allow TEST deployment from empty state --- .github/workflows/deploy.yml | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index ab01621..c16a6b0 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -198,8 +198,9 @@ jobs: echo "Kubernetes state backend access verified." - # Verify OpenTofu can access existing TEST remote state. - - name: Verify TEST remote state + # Initialize TEST backends, allowing initial provisioning + # and recreation after infrastructure destruction. + - name: Initialize TEST remote state shell: bash run: | set -euo pipefail @@ -207,7 +208,7 @@ jobs: for STACK in network frontend; do STACK_DIR="environments/test/$STACK" - echo "Initializing TEST $STACK backend..." + echo "::group::Initialize TEST $STACK backend" tofu -chdir="$STACK_DIR" init \ -reconfigure \ @@ -215,20 +216,11 @@ jobs: -lockfile=readonly \ -no-color - echo "Reading TEST $STACK state..." - - RESOURCES="$(tofu -chdir="$STACK_DIR" state list)" - - if [ -z "$RESOURCES" ]; then - echo "ERROR: No managed resources found in TEST $STACK state." - exit 1 - fi - - RESOURCE_COUNT="$(printf '%s\n' "$RESOURCES" | wc -l)" - echo "TEST $STACK: $RESOURCE_COUNT managed resources found." + echo "TEST $STACK backend initialized successfully." + echo "::endgroup::" done - echo "TEST remote state access verified." + echo "TEST remote state initialization completed." # Plan and automatically apply safe TEST infrastructure changes. - name: Deploy TEST infrastructure @@ -638,8 +630,8 @@ jobs: name: Deploy PROD needs: deploy-stage - # Only deploy PROD when manually triggered from main - # with the deploy_prod checkbox selected. + # Only deploy PROD when manually triggered from main. + # GitHub environment protection rules control approval. if: >- github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main'