Cleanup Container Images #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |