Skip to content

Commit cebfa08

Browse files
authored
Merge pull request #76 from uploadcare/issue/74/remove-snapshots-on-merge
Set up publishing of SNAPSHOT builds to GitHub Packages
2 parents bbee812 + df70f1c commit cebfa08

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
name: Publish snapshot to GitHub Packages
5454
runs-on: ubuntu-latest
5555
needs: build
56-
if: github.event_name == 'pull_request_target' || (github.event_name == 'push' && github.ref == 'refs/heads/master')
56+
if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/master')
5757
permissions:
5858
contents: read
5959
packages: write

.github/workflows/cleanup-snapshot.yml

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,52 @@ jobs:
3030
env:
3131
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3232
run: |
33+
set -euo pipefail
34+
35+
PACKAGE_NAME="com.uploadcare.uploadcare"
3336
SNAPSHOT_VERSION="${{ steps.get-version.outputs.base_version }}-PR-${{ github.event.pull_request.number }}-SNAPSHOT"
3437
echo "Looking for package version: ${SNAPSHOT_VERSION}"
3538
36-
# Retrieve the numeric ID of any published version matching this snapshot.
37-
# --paginate ensures all pages are scanned; --jq filters on each page.
39+
# Fetch ALL versions so we can detect whether the snapshot is the last remaining version.
40+
ALL_VERSION_IDS=$(gh api \
41+
--paginate \
42+
-H "Accept: application/vnd.github+json" \
43+
"/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME}/versions" \
44+
--jq ".[].id" 2>/dev/null || true)
45+
46+
# Retrieve the numeric ID(s) of any published version matching this snapshot.
3847
VERSION_IDS=$(gh api \
3948
--paginate \
4049
-H "Accept: application/vnd.github+json" \
41-
"/orgs/${{ github.repository_owner }}/packages/maven/com.uploadcare.uploadcare/versions" \
50+
"/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME}/versions" \
4251
--jq ".[] | select(.name == \"${SNAPSHOT_VERSION}\") | .id" 2>/dev/null || true)
4352
44-
if [ -z "$VERSION_IDS" ]; then
53+
if [ -z "${VERSION_IDS}" ]; then
4554
echo "No published version found for ${SNAPSHOT_VERSION} — nothing to delete."
4655
exit 0
4756
fi
4857
58+
TOTAL_VERSIONS=$(printf "%s\n" "${ALL_VERSION_IDS}" | sed '/^$/d' | wc -l | tr -d ' ')
59+
echo "Total versions in package ${PACKAGE_NAME}: ${TOTAL_VERSIONS}"
60+
61+
# GitHub Packages does not allow deleting the LAST version of a package (HTTP 400).
62+
# If this is the last remaining version, delete the whole package instead.
63+
if [ "${TOTAL_VERSIONS}" -le 1 ]; then
64+
echo "Snapshot is the last version; deleting the whole package ${PACKAGE_NAME} …"
65+
gh api --method DELETE \
66+
-H "Accept: application/vnd.github+json" \
67+
"/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME}"
68+
echo "Deleted package ${PACKAGE_NAME}."
69+
exit 0
70+
fi
71+
4972
while IFS= read -r VERSION_ID; do
73+
[ -z "${VERSION_ID}" ] && continue
5074
echo "Deleting version ID ${VERSION_ID} (${SNAPSHOT_VERSION}) …"
5175
gh api --method DELETE \
5276
-H "Accept: application/vnd.github+json" \
53-
"/orgs/${{ github.repository_owner }}/packages/maven/com.uploadcare.uploadcare/versions/${VERSION_ID}"
77+
"/orgs/${{ github.repository_owner }}/packages/maven/${PACKAGE_NAME}/versions/${VERSION_ID}"
5478
echo "Deleted version ID ${VERSION_ID}."
55-
done <<< "$VERSION_IDS"
79+
done <<< "${VERSION_IDS}"
80+
5681
echo "Done cleaning up ${SNAPSHOT_VERSION}."

0 commit comments

Comments
 (0)