diff --git a/.github/workflows/undeploy.yml b/.github/workflows/undeploy.yml new file mode 100644 index 0000000..b81a892 --- /dev/null +++ b/.github/workflows/undeploy.yml @@ -0,0 +1,66 @@ +name: Undeploy (Terraform destroy) + +# Nur manuell startbar: Forgejo -> Actions -> "Undeploy" -> Run workflow +on: + workflow_dispatch: + inputs: + environment: + description: "Welche Umgebung entfernen?" + type: choice + options: [ "test", "prod" ] + default: "test" + confirm: + description: "Zur Bestätigung den Umgebungsnamen nochmals eintippen" + type: string + required: true + delete_data: + description: "Auch die Datenbank (PVC) löschen? ACHTUNG: unwiderruflich" + type: boolean + default: false + +jobs: + undeploy: + runs-on: ubuntu-latest + container: catthehacker/ubuntu:act-latest + steps: + - name: Check Confirmation + run: | + if [ "${{ inputs.confirm }}" != "${{ inputs.environment }}" ]; then + echo "Bestätigung stimmt nicht mit '${{ inputs.environment }}' überein, Abbruch." + exit 1 + fi + + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Prepare Kubernetes Access + run: | + if [ "${{ inputs.environment }}" = "prod" ]; then + echo "${{ secrets.KUBECONFIG_TALOS }}" > ./talos_kubeconfig + chmod 600 ./talos_kubeconfig + test -s ./talos_kubeconfig || { echo "KUBECONFIG_TALOS ist leer oder fehlt"; exit 1; } + else + echo "${{ secrets.KUBECONFIG_DATA }}" > ./kind_kubeconfig + chmod 600 ./kind_kubeconfig + test -s ./kind_kubeconfig || { echo "KUBECONFIG_DATA ist leer oder fehlt"; exit 1; } + fi + + # Entfernt den Helm-Release und räumt den Terraform-State auf + - name: Terraform Destroy + run: | + ENV=${{ inputs.environment }} + terraform init -reconfigure -backend-config=envs/${ENV}.backend.hcl + terraform destroy -auto-approve -var-file=envs/${ENV}.tfvars + + # Helm löscht PVCs aus StatefulSets nicht – die DB bleibt sonst für den nächsten Deploy erhalten + - name: Delete Database Volume + if: inputs.delete_data + run: | + if [ "${{ inputs.environment }}" = "prod" ]; then + KC=./talos_kubeconfig; NS=boday-it + else + KC=./kind_kubeconfig; NS=default + fi + KARGS="--kubeconfig $KC" + [ "${{ inputs.environment }}" = "test" ] && KARGS="$KARGS --server https://local-k3s-platform:6443 --insecure-skip-tls-verify" + kubectl $KARGS delete pvc -n $NS db-data-boday-it-db-0 --ignore-not-found \ No newline at end of file