fix cleanup
All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 1m20s
All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 1m20s
This commit is contained in:
43
.github/workflows/cleanup.yaml
vendored
43
.github/workflows/cleanup.yaml
vendored
@@ -4,34 +4,65 @@ on: [delete]
|
||||
jobs:
|
||||
cleanup:
|
||||
runs-on: ubuntu-latest
|
||||
# Nur ausführen, wenn ein Branch gelöscht wurde (keine Tags)
|
||||
if: github.event.ref_type == 'branch'
|
||||
|
||||
steps:
|
||||
# 1. Variablen berechnen (MIT FIX FÜR REFS/HEADS & MAIN-CHECK)
|
||||
- name: Prepare Variables
|
||||
run: |
|
||||
# Repo Name klein (z.B. "it232abschied")
|
||||
REPO_LOWER=$(echo "${{ gitea.repository }}" | cut -d'/' -f2 | tr '[:upper:]' '[:lower:]')
|
||||
BRANCH_NAME=${{ github.event.ref }}
|
||||
BRANCH_LOWER=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g')
|
||||
TARGET_NS="${REPO_LOWER}-${BRANCH_LOWER}"
|
||||
echo "TARGET_NS=$TARGET_NS" >> $GITHUB_ENV
|
||||
|
||||
# Branch Name aus Event (z.B. "refs/heads/feature-x")
|
||||
RAW_REF="${{ github.event.ref }}"
|
||||
# "refs/heads/" entfernen
|
||||
BRANCH_CLEAN=${RAW_REF#refs/heads/}
|
||||
# Kleinschreiben & Sonderzeichen
|
||||
BRANCH_LOWER=$(echo "$BRANCH_CLEAN" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g')
|
||||
|
||||
# Logik synchron zum Deploy:
|
||||
# Main/Master -> Namespace ist nur der Repo-Name
|
||||
# Anderes -> Namespace ist Repo-Branch
|
||||
if [ "$BRANCH_LOWER" = "main" ] || [ "$BRANCH_LOWER" = "master" ]; then
|
||||
TARGET_NS="${REPO_LOWER}"
|
||||
IS_MAIN="true"
|
||||
else
|
||||
TARGET_NS="${REPO_LOWER}-${BRANCH_LOWER}"
|
||||
IS_MAIN="false"
|
||||
fi
|
||||
|
||||
echo "DEBUG: Clean Branch: $BRANCH_LOWER"
|
||||
echo "DEBUG: Target NS: $TARGET_NS"
|
||||
|
||||
echo "TARGET_NS=$TARGET_NS" >> $GITHUB_ENV
|
||||
echo "IS_MAIN=$IS_MAIN" >> $GITHUB_ENV
|
||||
|
||||
# 2. Sicherheits-Check: Niemals Main/Master löschen!
|
||||
# Wir prüfen jetzt die Variable IS_MAIN, statt den Namen hart zu codieren
|
||||
- name: Protect Main
|
||||
if: env.TARGET_NS == 'escape-teacher-main' || env.TARGET_NS == 'escape-teacher-master'
|
||||
if: env.IS_MAIN == 'true'
|
||||
run: |
|
||||
echo "❌ Main darf nicht gelöscht werden!"
|
||||
echo "❌ ABBRUCH: Der Produktions-Namespace ${{ env.TARGET_NS }} darf nicht gelöscht werden!"
|
||||
exit 1
|
||||
|
||||
# 3. Kubectl einrichten
|
||||
- name: Setup Kubectl
|
||||
run: |
|
||||
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
|
||||
chmod +x kubectl
|
||||
mv kubectl /usr/local/bin/
|
||||
|
||||
mkdir -p $HOME/.kube
|
||||
echo "${{ secrets.KUBE_CONFIG }}" > $HOME/.kube/config
|
||||
chmod 600 $HOME/.kube/config
|
||||
|
||||
# Der Trick für interne Kommunikation
|
||||
sed -i 's|server: https://.*:6443|server: https://kubernetes.default.svc:443|g' $HOME/.kube/config
|
||||
|
||||
# 4. Namespace löschen
|
||||
- name: Delete Namespace
|
||||
run: |
|
||||
echo "🗑️ Lösche Namespace: ${{ env.TARGET_NS }}"
|
||||
# Wir löschen den Namespace ohne zu warten (async), das geht schneller
|
||||
kubectl delete namespace ${{ env.TARGET_NS }} --ignore-not-found --wait=false
|
||||
Reference in New Issue
Block a user