-
Notifications
You must be signed in to change notification settings - Fork 14
81 lines (68 loc) · 3.31 KB
/
Copy pathcleanup-snapshot.yml
File metadata and controls
81 lines (68 loc) · 3.31 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
name: Clean up PR snapshot
on:
pull_request:
types: [closed]
jobs:
cleanup-snapshot:
name: Delete PR snapshot from GitHub Packages
runs-on: ubuntu-latest
# Only run when the PR was actually merged, not just closed.
if: github.event.pull_request.merged == true
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Get base version
id: get-version
run: |
BASE_VERSION=$(grep -iE "version([ ]*)=" gradle.properties | cut -f 2 -d "=" | tr -d '[:space:]')
# Strip any existing -SNAPSHOT suffix so we can re-apply it cleanly
BASE_VERSION="${BASE_VERSION%-SNAPSHOT}"
echo "base_version=${BASE_VERSION}" >> $GITHUB_OUTPUT
- name: Delete PR snapshot from GitHub Packages
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
PACKAGE_NAME="com.uploadcare.uploadcare"
SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-PR-${{ github.event.pull_request.number }}-SNAPSHOT"
echo "Looking for package version: ${SNAPSHOT_VERSION}"
# Fetch ALL versions so we can detect whether the snapshot is the last remaining version.
ALL_VERSION_IDS=$(gh api \
--paginate \
-H "Accept: application/vnd.github+json" \
"/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME}/versions" \
--jq ".[].id" 2>/dev/null || true)
# Retrieve the numeric ID(s) of any published version matching this snapshot.
VERSION_IDS=$(gh api \
--paginate \
-H "Accept: application/vnd.github+json" \
"/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME}/versions" \
--jq ".[] | select(.name == \"${SNAPSHOT_VERSION}\") | .id" 2>/dev/null || true)
if [ -z "${VERSION_IDS}" ]; then
echo "No published version found for ${SNAPSHOT_VERSION} — nothing to delete."
exit 0
fi
TOTAL_VERSIONS=$(printf "%s\n" "${ALL_VERSION_IDS}" | sed '/^$/d' | wc -l | tr -d ' ')
echo "Total versions in package ${PACKAGE_NAME}: ${TOTAL_VERSIONS}"
# GitHub Packages does not allow deleting the LAST version of a package (HTTP 400).
# If this is the last remaining version, delete the whole package instead.
if [ "${TOTAL_VERSIONS}" -le 1 ]; then
echo "Snapshot is the last version; deleting the whole package ${PACKAGE_NAME} …"
gh api --method DELETE \
-H "Accept: application/vnd.github+json" \
"/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME}"
echo "Deleted package ${PACKAGE_NAME}."
exit 0
fi
while IFS= read -r VERSION_ID; do
[ -z "${VERSION_ID}" ] && continue
echo "Deleting version ID ${VERSION_ID} (${SNAPSHOT_VERSION}) …"
gh api --method DELETE \
-H "Accept: application/vnd.github+json" \
"/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME}/versions/${VERSION_ID}"
echo "Deleted version ID ${VERSION_ID}."
done <<< "${VERSION_IDS}"
echo "Done cleaning up ${SNAPSHOT_VERSION}."