Skip to content

Commit 632daca

Browse files
committed
Using Personal Auth Token with delete:packages permission
Signed-off-by: Marvin Froeder <marvin@datasqrl.com>
1 parent ef646c8 commit 632daca

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

.github/workflows/delete-pr-images.yml

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ on:
77
jobs:
88
delete-ghcr:
99
runs-on: ubuntu-latest
10-
permissions:
11-
packages: admin
1210

1311
strategy:
1412
matrix:
@@ -17,47 +15,50 @@ jobs:
1715
steps:
1816
- name: Delete GHCR image
1917
run: |
18+
# Convert the repository name to lowercase for GHCR compatibility
2019
REPO=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
20+
21+
# Our PR image tag is pr-<number>-<flink_profile>
2122
TAG="pr-${{ github.event.number }}-${{ matrix.FLINK_PROFILE }}"
2223
IMAGE="ghcr.io/$REPO/flink-jar-runner:$TAG"
24+
2325
echo "Attempting to delete image: $IMAGE"
2426
25-
TOKEN="$(echo -n "${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}" | base64)"
27+
# Build the Basic Auth header from your PAT credentials
28+
TOKEN_BASE64="$(echo -n "${{ secrets.PAT_USERNAME }}:${{ secrets.PAT_PASSWORD }}" | base64)"
2629
27-
# We'll do a GET request with -D -, capturing both body + headers
28-
# but we only parse the headers for 'docker-content-digest'.
29-
# We include multiple Accept types so that GHCR returns the correct manifest for any single or multi-arch image.
30-
RAW_RESPONSE=$(curl -s \
31-
-D - \
32-
-H "Authorization: Basic $TOKEN" \
30+
# Make a GET request (including multiple Accept types) to retrieve the Docker-Content-Digest header
31+
# We use '-sD -' to capture both headers and body in RAW_RESPONSE
32+
RAW_RESPONSE=$(curl -sD - \
33+
-H "Authorization: Basic $TOKEN_BASE64" \
3334
-H "Accept: application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.docker.distribution.manifest.v2+json, application/vnd.oci.image.manifest.v1+json, application/vnd.oci.image.index.v1+json" \
3435
"https://ghcr.io/v2/$REPO/flink-jar-runner/manifests/$TAG")
3536
3637
# Separate headers from body
3738
HEADERS=$(echo "$RAW_RESPONSE" | sed -n '/^HTTP/,$p' | sed '/^$/q')
38-
BODY=$(echo "$RAW_RESPONSE" | sed -e '1,/^$/d')
3939
40-
# Parse the Docker-Content-Digest from the headers
40+
# Extract the Docker-Content-Digest from headers
4141
DIGEST=$(echo "$HEADERS" | awk '/docker-content-digest:/ { print $2 }' | tr -d $'\r')
4242
4343
if [[ -z "$DIGEST" ]]; then
44-
echo "ERROR: No digest found in GHCR response headers. Image may not exist or GHCR did not return the digest."
45-
echo "Response Headers:"
44+
echo "ERROR: No digest found in GHCR response. The image might not exist, or GHCR didn’t return the digest."
45+
echo "==== HEADERS START ===="
4646
echo "$HEADERS"
47+
echo "==== HEADERS END ===="
4748
exit 1
4849
fi
4950
5051
echo "Found digest: $DIGEST"
5152
52-
# Attempt deletion, capture HTTP status
53-
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
53+
# DELETE the image by digest
54+
DELETE_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
5455
-X DELETE \
55-
-H "Authorization: Basic $TOKEN" \
56+
-H "Authorization: Basic $TOKEN_BASE64" \
5657
"https://ghcr.io/v2/$REPO/flink-jar-runner/manifests/$DIGEST")
5758
58-
if [[ "$HTTP_STATUS" -ge 200 && "$HTTP_STATUS" -lt 300 ]]; then
59-
echo "Image deleted successfully (HTTP $HTTP_STATUS)."
59+
if [[ "$DELETE_STATUS" -ge 200 && "$DELETE_STATUS" -lt 300 ]]; then
60+
echo "Image deleted successfully (HTTP $DELETE_STATUS)."
6061
else
61-
echo "ERROR: Failed to delete image. HTTP status $HTTP_STATUS"
62+
echo "ERROR: Failed to delete image. HTTP status $DELETE_STATUS"
6263
exit 1
6364
fi

0 commit comments

Comments
 (0)