7
7
jobs :
8
8
delete-ghcr :
9
9
runs-on : ubuntu-latest
10
- permissions :
11
- packages : admin
12
10
13
11
strategy :
14
12
matrix :
@@ -17,47 +15,50 @@ jobs:
17
15
steps :
18
16
- name : Delete GHCR image
19
17
run : |
18
+ # Convert the repository name to lowercase for GHCR compatibility
20
19
REPO=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
20
+
21
+ # Our PR image tag is pr-<number>-<flink_profile>
21
22
TAG="pr-${{ github.event.number }}-${{ matrix.FLINK_PROFILE }}"
22
23
IMAGE="ghcr.io/$REPO/flink-jar-runner:$TAG"
24
+
23
25
echo "Attempting to delete image: $IMAGE"
24
26
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)"
26
29
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" \
33
34
-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" \
34
35
"https://ghcr.io/v2/$REPO/flink-jar-runner/manifests/$TAG")
35
36
36
37
# Separate headers from body
37
38
HEADERS=$(echo "$RAW_RESPONSE" | sed -n '/^HTTP/,$p' | sed '/^$/q')
38
- BODY=$(echo "$RAW_RESPONSE" | sed -e '1,/^$/d')
39
39
40
- # Parse the Docker-Content-Digest from the headers
40
+ # Extract the Docker-Content-Digest from headers
41
41
DIGEST=$(echo "$HEADERS" | awk '/docker-content-digest:/ { print $2 }' | tr -d $'\r')
42
42
43
43
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 ==== "
46
46
echo "$HEADERS"
47
+ echo "==== HEADERS END ===="
47
48
exit 1
48
49
fi
49
50
50
51
echo "Found digest: $DIGEST"
51
52
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}" \
54
55
-X DELETE \
55
- -H "Authorization: Basic $TOKEN " \
56
+ -H "Authorization: Basic $TOKEN_BASE64 " \
56
57
"https://ghcr.io/v2/$REPO/flink-jar-runner/manifests/$DIGEST")
57
58
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 )."
60
61
else
61
- echo "ERROR: Failed to delete image. HTTP status $HTTP_STATUS "
62
+ echo "ERROR: Failed to delete image. HTTP status $DELETE_STATUS "
62
63
exit 1
63
64
fi
0 commit comments