From ae9af3f06d068ac4fe0c955018e4799602178f04 Mon Sep 17 00:00:00 2001 From: Sergei Poljanski Date: Fri, 26 Dec 2025 16:16:01 +0200 Subject: [PATCH] automatic cleanup --- .forgejo/workflows/cleanup.yaml | 35 +++++++++++++++++++++++++++++++++ README.md | 2 ++ 2 files changed, 37 insertions(+) create mode 100644 .forgejo/workflows/cleanup.yaml diff --git a/.forgejo/workflows/cleanup.yaml b/.forgejo/workflows/cleanup.yaml new file mode 100644 index 0000000..e98292f --- /dev/null +++ b/.forgejo/workflows/cleanup.yaml @@ -0,0 +1,35 @@ +name: Cleanup Staging + +on: + delete: + branches: + - '**' + +jobs: + cleanup: + if: github.event.ref_type == 'branch' && github.event.ref != 'main' + runs-on: arch-latest + steps: + - name: Remove staging environment + uses: appleboy/ssh-action@v1.2.3 + env: + BRANCH_NAME: ${{ github.event.ref }} + with: + host: ${{ secrets.DEPLOY_IP }} + username: ${{ secrets.DEPLOY_USER }} + key: ${{ secrets.DEPLOY_SSH_KEY }} + envs: BRANCH_NAME + script: | + BRANCH_SLUG=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]//g') + DEPLOY_DIR="/opt/czsk-${BRANCH_SLUG}" + CONTAINER_NAME="czsk-${BRANCH_SLUG}" + + if [ -d "$DEPLOY_DIR" ]; then + cd $DEPLOY_DIR + docker compose down --rmi all 2>/dev/null || true + cd / + rm -rf $DEPLOY_DIR + echo "Cleaned up staging: ${BRANCH_SLUG}" + else + echo "No staging environment found for: ${BRANCH_SLUG}" + fi diff --git a/README.md b/README.md index 2a9a741..09396f2 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,5 @@ Deployments are automatic on push via Forgejo Actions. Branch names are sanitized to `a-z0-9` only (lowercase, symbols removed). **Example:** `fix1/blabla` deploys to `fix1blabla.staging.czsk.it` + +Staging environments are automatically removed when the branch is deleted.