-
Notifications
You must be signed in to change notification settings - Fork 4
61 lines (49 loc) · 2.1 KB
/
cleanup.yml
File metadata and controls
61 lines (49 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
name: Cleanup
on:
delete:
# Triggered when a branch or tag is deleted
permissions:
contents: read
packages: write
jobs:
cleanup-docker-tag:
# Only run for branch deletions, not tag deletions
if: github.event.ref_type == 'branch'
runs-on: ubuntu-latest
steps:
- name: Sanitize branch name to Docker tag
id: sanitize
run: |
# Match docker/metadata-action sanitization:
# - Replace invalid chars with dashes
# - Remove leading/trailing dashes
# - Lowercase
BRANCH="${{ github.event.ref }}"
TAG=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9._-]/-/g' | sed 's/^-*//' | sed 's/-*$//' | tr '[:upper:]' '[:lower:]')
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
echo "Sanitized branch '$BRANCH' to tag '$TAG'"
- name: Delete Docker tag from GHCR
run: |
TAG="${{ steps.sanitize.outputs.tag }}"
REPO="ghcr.io/${{ github.repository }}"
echo "Attempting to delete tag '$TAG' from $REPO"
skopeo delete \
--creds "${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}" \
"docker://${REPO}:${TAG}" || {
echo "::warning::Failed to delete tag '$TAG' from GHCR - it may not exist or credentials lack permissions"
exit 0 # Don't fail the workflow if tag doesn't exist
}
echo "Successfully deleted tag '$TAG' from $REPO"
- name: Delete Docker tag from alternate registry
if: vars.DOCKER_ALT_REGISTRY != ''
run: |
TAG="${{ steps.sanitize.outputs.tag }}"
REPO="${{ vars.DOCKER_ALT_REGISTRY }}/${{ github.repository }}"
echo "Attempting to delete tag '$TAG' from $REPO"
skopeo delete \
--creds "${{ secrets.DOCKER_USERNAME }}:${{ secrets.DOCKER_PASSWORD }}" \
"docker://${REPO}:${TAG}" || {
echo "::warning::Failed to delete tag '$TAG' - it may not exist or credentials lack permissions"
exit 0 # Don't fail the workflow if tag doesn't exist
}
echo "Successfully deleted tag '$TAG' from $REPO"