Skip to content

Cleanup Container Images #14

Cleanup Container Images

Cleanup Container Images #14

name: Cleanup Container Images
on:
delete:
concurrency:
group: ${{ github.workflow }}-${{ github.event.ref }}
cancel-in-progress: false
jobs:
cleanup-images:
name: Delete branch container images
runs-on: ubuntu-24.04
permissions:
packages: write
steps:
- name: Generate image tag to delete
id: tag
env:
EVENT_REF: ${{ github.event.ref }}
run: |
# Sanitize deleted branch name to match build workflow tag generation
BRANCH_NAME="${EVENT_REF}"
IMAGE_TAG=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9._-]+/-/g; s/-+/-/g; s/^-+|-+$//g' | cut -c1-128 | sed -E 's/[.-]$//')
echo "tag=$IMAGE_TAG" >> "$GITHUB_OUTPUT"
- name: Delete container image version
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
IMAGE_TAG: ${{ steps.tag.outputs.tag }}
run: |
# Get the version ID for this specific tag
VERSION_ID=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/orgs/bitwarden/packages/container/key-connector/versions" \
--jq ".[] | select(.metadata.container.tags[] | contains(\"$IMAGE_TAG\")) | .id" \
| head -1)
if [[ -n "$VERSION_ID" ]]; then
echo "Deleting image with tag: $IMAGE_TAG (version ID: $VERSION_ID)"
gh api \
--method DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/orgs/bitwarden/packages/container/key-connector/versions/$VERSION_ID"
echo "Successfully deleted image"
else
echo "No image found with tag: $IMAGE_TAG"
fi