name: Delete Image Tags on Merge

on:
  pull_request:
    types:
      - closed

jobs:
  delete-docker-tag:
    if: startsWith(github.event.pull_request.head.ref, 'fix/') || startsWith(github.event.pull_request.head.ref, 'release/')
    runs-on: ubuntu-latest
    name: Cleanup Image Tags from GHCR
    permissions:
      packages: write
    steps:
      - name: Extract tag from branch name
        id: extract_branch
        run: |
          BRANCH="${{ github.event.pull_request.head.ref }}"
          if [[ "$BRANCH" == fix/* ]]; then
            TAG=${BRANCH#fix/}
          elif [[ "$BRANCH" == release/* ]]; then
            TAG="${BRANCH#release/}-preview"
          else
            TAG=""
          fi
          echo "tag=$TAG" >> $GITHUB_OUTPUT
        shell: bash

      - name: Delete tags
        if: steps.extract_branch.outputs.tag != ''
        uses: dataaxiom/ghcr-cleanup-action@v1
        with:
          tags: ${{ steps.extract_branch.outputs.tag }},${{ steps.extract_branch.outputs.tag }}-ubuntu
