Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,37 +198,29 @@ 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
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 \
-input=false \
-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
Expand Down Expand Up @@ -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'
Expand Down
205 changes: 205 additions & 0 deletions .github/workflows/destroy.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .opentofu-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.12.6