name: "Build: Cleanup Residue"

permissions:
  contents: read
  packages: write

on:
  workflow_call:
  workflow_dispatch:
  workflow_run:
    workflows: ["Build Swarm Loadbalancer"]
    types:
      - completed

jobs:
  cleanup-delete-candidate-image:
    name: Delete candidate image
    runs-on: ${{ vars.RUNS_ON }}
    steps:
      - run: docker login ghcr.io -u ${{ github.repository_owner }} -p ${{ secrets.GITHUB_TOKEN }}
      - uses: vlaurin/action-ghcr-prune@v0.6.0
        continue-on-error: true
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          organization: ${{ github.repository_owner }}
          container: docker-swarm-loadbalancer
          dry-run: true
          prune-tags-regexes: |
            ^build-${{ github.sha }}$

  cleanup-untagged-images:
    name: Delete untagged images
    runs-on: ${{ vars.RUNS_ON }}
    steps:
      - run: docker login ghcr.io -u ${{ github.repository_owner }} -p ${{ secrets.GITHUB_TOKEN }}
      - uses: vlaurin/action-ghcr-prune@v0.6.0
        continue-on-error: true
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          organization: ${{ github.repository_owner }}
          container: docker-swarm-loadbalancer
          dry-run: true
          keep-younger-than: 90 # days
          keep-last: 2
          prune-untagged: true
          keep-tags-regexes: |
            ^v
            ^latest$
            ^old$