Skip to content

Commit 474d8fd

Browse files
authored
Add workflow for clean docker pr (#35)
1 parent 9774fb5 commit 474d8fd

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Delete All GHCR PR Tags
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
workflow_dispatch:
8+
9+
jobs:
10+
delete-pr-tags:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Delete all GHCR image tags starting with "pr-"
15+
env:
16+
GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
17+
REPO: ${{ github.event.repository.name }}
18+
OWNER: ${{ github.repository_owner }}
19+
run: |
20+
echo "Fetching GHCR container versions for ghcr.io/${OWNER}/${REPO}..."
21+
22+
response=$(curl -s -H "Authorization: Bearer ${GHCR_TOKEN}" \
23+
"https://api.github.com/orgs/${OWNER}/packages/container/${REPO}/versions")
24+
25+
# Check that the response is a JSON array
26+
if ! echo "$response" | jq -e 'type == "array"' > /dev/null; then
27+
echo "❌ Unexpected response from GitHub API:"
28+
echo "$response"
29+
exit 1
30+
fi
31+
32+
echo "$response" | jq -c '.[]' | while read -r version; do
33+
version_id=$(echo "$version" | jq -r '.id')
34+
tag_names=$(echo "$version" | jq -r '.metadata.container.tags[]?')
35+
36+
for tag in $tag_names; do
37+
if [[ "$tag" == pr-* ]]; then
38+
echo "🗑️ Deleting tag: $tag (version ID: $version_id)"
39+
curl -s -X DELETE -H "Authorization: Bearer ${GHCR_TOKEN}" \
40+
"https://api.github.com/orgs/${OWNER}/packages/container/${REPO}/versions/${version_id}"
41+
break # One deletion per version is sufficient
42+
fi
43+
done
44+
done

.github/workflows/docker-image.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ jobs:
3737
3838
if [[ "${{ github.event_name }}" == "release" ]]; then
3939
VERSION_TAG=${{ github.event.release.tag_name }}
40-
echo "tags=${REPO}:${VERSION_TAG},${REPO}:latest" >> $GITHUB_OUTPUT
40+
echo "tags<<EOF" >> $GITHUB_OUTPUT
41+
echo "${REPO}:${VERSION_TAG}" >> $GITHUB_OUTPUT
42+
echo "${REPO}:latest" >> $GITHUB_OUTPUT
43+
echo "EOF" >> $GITHUB_OUTPUT
4144
4245
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
4346
PR_NUMBER=${{ github.event.pull_request.number }}
@@ -54,5 +57,6 @@ jobs:
5457
platforms: linux/amd64,linux/arm64
5558
push: true
5659
tags: ${{ steps.meta.outputs.tags }}
60+
provenance: false
5761
cache-from: type=gha
5862
cache-to: type=gha,mode=max

0 commit comments

Comments
 (0)